Question

I'm trying to analyze my keystrokes over the next month and would like to throw together a simple program to do so. I don't want to exactly log the commands but simply generate general statistics on my key presses.

I am the most comfortable coding this in python, but am open to other suggestions. Is this possible, and if so what python modules should I look at? Has this already been done?

I'm on OSX but would also be interested in doing this on an Ubuntu box and Windows XP.

Was it helpful?

Solution

It looks like you need http://patorjk.com/keyboard-layout-analyzer/

This handy program will analyze a block of text and tell you how far your fingers had to travel to type it, then recommend your optimal layout.

To answer your original question, on Linux you can read from /dev/event* for local keyboard, mouse and joystick events. I believe you could for example simply cat /dev/event0 > keylogger. The events are instances of struct input_event. See also http://www.linuxjournal.com/article/6429.

Python's struct module is a convenient way to parse binary data.

For OSX, take a look at the source code to logkext. http://code.google.com/p/logkext/

OTHER TIPS

Unless you are planning on writing the interfaces yourself, you are going to require some library, since as other posters have pointed out, you need to access low-level key press events managed by the desktop environment.

On Windows, the PyHook library would give you the functionality you need.

On Linux, you can use the Python X Library (assuming you are running a graphical desktop).

Both of these are used to good effect by pykeylogger. You'd be best off downloading the source (see e.g. pyxhook.py) to see specific examples of how key press events are captured. It should be trivial to modify this to sum the distribution of keys rather than recording the ordering.

As the current X server's Record extension seems to be broken, using pykeylogger for Linux doesn't really help. Take a look at evdev and its demo function, instead. The solution is nastier, but it does at least work.

It comes down to setting up a hook to the device

import evdev
keyboard_location = '/dev/input/event1'  # get the correct one from HAL or so
keyboard_device = evdev.Device(keyboard_location)

Then, regularly poll the device to get the status of keys and other information:

keyboard_device.poll()

Depending on what statistics you want to collect, maybe you do not have to write this yourself; the program Workrave is a program to remind you to take small breaks and does so by monitoring keyboard and mouse activity. It keeps statistics of this activity which you probably could use (unless you want very detailed/more specific statistics). In worst case you could look at the source (C++) to find how it is done.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top