Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can I log a keyboard hit via shell script? My aim is to log the number of keyboard hits of a login session on my Ubuntu system.

Note: I do not want to log the keystrokes itself, only the number of hits. So in the end it says:

94853 hits today.
share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

Monitoring in X11 (Graphical Desktop) Session Only

In you're using the XInput layer (you probably are, if you're running a modern X) then xinput test «keyboard-id» will give pey press and release events. You can get the keyboard ID by running xinput list. You can also use the name.

xinput test 'AT Translated Set 2 keyboard' | grep -c 'key press'

Note that when you pipe xinput, it has a fairly large buffer. So you may lose some keystrokes, unfortunately. You could use the XI2 API directly to avoid that, but that's not easy from shell.

You can fairly easily start your script as one of the login scripts in your desktop environment or from your .xsession file, depending. The xinput should exit when you log out, because it'll lose its X11 connection. So it's really easy to track when your session starts and ends.

Monitoring System-wide (All Sessions, Even Text Mode)

Alternatively, if you want to monitor all keystrokes on the system, not just those in your X11 session, you can use the input-events (part of the input-utils package on Debian, at least). This must run as root. Use lsinputs to find the right input device (happens to be 0 on my system) and then:

input-events 0 | grep -c 'EV_KEY.*pressed'

If you go this way, you'll have to figure out when your sessions start and end some way (e.g., peterph's dbus suggestion).

share|improve this answer
    
Thanks I will go with that. –  Indianer Nov 21 '12 at 8:05
add comment

As Derobert mentioned, xinput can be used (xinput --test-xi2 should work - you would need to count the lines containing RawKeyPress). However, you'd need to run your script as soon as your session starts and stop once it ends - so you'd probably want to watch a dbus session too.

Additionally, you would miss anything typed on virtual consoles - for that I'm afraid you'd need to write a kernel module, that would export its statistics somewhere in /proc or /sys - but it would include pretty much everything that goes into the computer.

share|improve this answer
    
Thanks for the addition info on virtual consoles. –  Indianer Nov 21 '12 at 8:05
    
FYI: In most desktops its fairly easy to know when the session starts—you can add log-in scripts that'll start with the session. When it stops is also easy, that's when the xinput will exit (from losing its X11 connection). I'll update my answer with this. –  derobert Nov 21 '12 at 15:30
1  
You shouldn't need a kernel module—see the input-utils package. Another thing to put in my answer :-P –  derobert Nov 21 '12 at 15:35
    
Also, xinput --test-xi2 pops up a window here, which is why I didn't use it. –  derobert Nov 21 '12 at 15:45
    
@derobert bah, I completely forgot about /dev/input (which is what input-utils seem to use) :) –  peterph Nov 21 '12 at 22:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.