monitoring keyboard on win32

Alex Martelli aleax at aleax.it
Tue Jan 8 06:02:21 EST 2002


"Simo Salminen" <this at is.invalid> wrote in message
news:slrna3krph.k3u.this at korppi.cs.tut.fi...
> I'm trying to build script that monitors certain application, and if it
> performs clipboard-copy operation with shift-key pressed, it takes certain
> actions. I've got the monitoring clipboard part done, but how I could know
> if shift-key is pressed at given time?

You need the win32all extensions.  The win32api module exposes two
relevant functions: win32api.GetKeyState(keycode) gives you the state
of a key when your thread last read from its message queue (what you
typically want to know to check what modifier keys if any were pressed
together with such events as mouse moves), GetAsyncKeyState(keycode)
gives you the state of the key "right now" (as of the latest relevant
keyboard hardware interrupt).

As argument to either function, you can use win32con.VK_SHIFT if the
shiftkey is what you want to check.  The pressed/non-pressed information
is carried by the high bit, but that's only relevant for "toggling"
keys -- mostly you can just check for ==0 meaning not pressed, !=0
meaning pressed.  I suggest you experiment a little and check whether
it gives you results you can use.


Alex








More information about the Python-list mailing list