6

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.

Paul Tarjan
  • 48,968
  • 59
  • 172
  • 213
  • 3
    Have you checked already pykeylogger? – Vinko Vrsalovic Jun 28 '09 at 06:34
  • I tried using it and found it requires a lot of libraries that aren't present on my installation. – Paul Tarjan Jun 28 '09 at 06:56
  • What's wrong with downloading the needed libraries? – S.Lott Jun 28 '09 at 12:14
  • 1
    Are you running Windows? Lots of sites will infect your machine with a keylogger. Have you tried turning off your anti-virus? – S.Lott Jun 28 '09 at 12:15
  • I hope you were joking :) I want to collect general statistics on my typing and specifically not log everything since I'm sure to type passwords. – Paul Tarjan Jun 28 '09 at 19:06
  • If you were on Linux or Windows, you could use Workrave (http://www.workrave.org/welcome/) to track how long you spent at the computer and how many keystrokes / mouse clicks you've performed, among other things. It breaks down stats by day... you might be able to poll the log file of Workrave with Python as a temporary workaround until you find a keylogger library that suits your needs. – Mark Rushakoff Jun 28 '09 at 19:18

4 Answers4

4

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/

joeforker
  • 40,459
  • 37
  • 151
  • 246
2

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.

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141
2

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()
mzuther
  • 1,158
  • 1
  • 13
  • 24
0

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.

hlovdal
  • 26,565
  • 10
  • 94
  • 165
  • interesting, i'll check it out. I basically want to know if when I switch to dvorak that I'm actually getting some benefit. – Paul Tarjan Jun 29 '09 at 20:56
  • Er, how will changing a keyboard layout affect which keys you type or how frequently you type them? – Seth Johnson Jun 30 '09 at 02:27
  • 1
    I want to make sure that they keys I'm typing follow the general english distribution so that I will benefit from Dvorak. I program quite a bit, but my thesis is I still end up with a regular english distribution when you factor in my email writing, SO posting, etc. – Paul Tarjan Jul 01 '09 at 20:15