Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 384
Paul Tarjan Avatar asked Jun 28 '09 06:06

Paul Tarjan


People also ask

How do you capture keystrokes in Python?

Here, we are using three methods to detect keypress in Python read_key() , is_pressed() and on_press_key() . The read_key() will read which key a user has pressed on the keyboard, and if it's that key which you wanted, in this case, p , it will print the message You pressed p .


1 Answers

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/

like image 167
joeforker Avatar answered Oct 06 '22 00:10

joeforker