keylogger in Python

François Pinard pinard at
Sun Jul 31 16:37:27 CEST 2005

[Michael Hoffman]

> Jay wrote:

> > yo, thanks for the great input. And the only reason i want to
> > create a python based keylogger is because there is none. Just a
> > fun project...  But im gonna do some more research on the keyboard
> > drivers and stuff like that and to learn how to attach my python
> > porgrams into the sub-processes so that it would instead log every
> > char instead of just char in the IDE.

> I think this is going to be much harder than you think, and I imagine
> this will only end in frustration for you. You will not be able to do
> it well with just Python. I would recommend a different fun project.

I'm just stumbling on that message, and did not follow the whole thread,
sorry if I repeat things already discussed.  My point is that Python is
able to do surprising things, given the `fcntl' and `ioctl' modules.

Surely on Linux, logging keys under X-Windows or under virtual terminals
are quite different matters.  Let me share a related experience for
virtual terminals.  I once had to rush the port on Linux a few QNX
applications, written in C, which were using the QNX term library
for input and display.  In console mode, the QNX keyboard is richer
than the Linux one.  As users wanted to retain the _exact_ keyboard
functionality, I saw no choice but reading raw scan codes on the Linux
side.  So, I wrote a term library emulator as a thin C layer, which was
itself using a Python module (Python was to be transparently embedded by
the emulated library) for doing the bulk of keyboard processing.

It was something pretty heretic to do, I know.  But Python was more than
fast enough for handling the low-level keyboard reading, for applications
otherwise all written in C.  But it allowed the port to be done quickly.

While debugging such a thing, you often loose the keyboard and with it,
the capability of switching terminals, so you have to devise some extra
machinery for restoring the keyboard into a usable state.

François Pinard

More information about the Python-list mailing list