How can you read keystrokes when the python program isn't in the foreground?
Asked Answered
S

4

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.

Salty answered 28/6, 2009 at 6:20 Comment(6)
Have you checked already pykeylogger?Undue
I tried using it and found it requires a lot of libraries that aren't present on my installation.Salty
What's wrong with downloading the needed libraries?Amon
Are you running Windows? Lots of sites will infect your machine with a keylogger. Have you tried turning off your anti-virus?Amon
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.Salty
If you were on Linux or Windows, you could use Workrave (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.Diba
M
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/

Mauldin answered 12/8, 2009 at 17:19 Comment(1)
hmm, looks very interesting. So I should log my keystream then paste there? I'll give it a shot, thanks.Salty
T
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.

Twill answered 27/7, 2009 at 22:47 Comment(0)
C
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()
Carner answered 27/8, 2009 at 23:28 Comment(0)
A
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.

Angular answered 29/6, 2009 at 7:53 Comment(3)
interesting, i'll check it out. I basically want to know if when I switch to dvorak that I'm actually getting some benefit.Salty
Er, how will changing a keyboard layout affect which keys you type or how frequently you type them?Kathleenkathlene
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.Salty

© 2022 - 2024 — McMap. All rights reserved.