Monitor key presses in Python?

Dave Angel davea at davea.name
Sat Sep 14 18:03:56 EDT 2013


On 14/9/2013 14:10, eamonnrea at gmail.com wrote:

> I didnt wanna say that, in case people threw a fit and stuff.
>
> So yeah, how would I monitor the key presses?

There's a huge difference between monitoring key presses within your own
process, and intercepting them system-wide.  if you need to see
keystrokes even when your window does not have the focus, then you need
to call some system DLL function, (some kind of "system hook", which
you can probably do using the platform module, or the ctypes module, or
some module that I wouldn't have on Linux.  This type of code is not the
least bit portable, which just means I can't test things here to try to
help.

Note also that if you get one of the ActivePython implementations from
ActiveState, you'll get PyWin32, which may well have a function just for
the purpose.  See:

http://docs.activestate.com/activepython/2.5/pywin32/PyWin32.HTML

I'm sure it can be separately downloaded, but when I used to run
Windows, I just got the whole package.  The ActivePython also had the
best offline documentation I was able to find at the time.

See also Tim Golden's win32 web page:

http://timgolden.me.uk/python/win32_how_do_i.html

There is a separate win32 mailing list;  see:

https://mail.python.org/mailman/listinfo/python-win32

Possibly the most frequent poster there is Tim Golden, so search his
site first.


-- 
DaveA





More information about the Python-list mailing list