How to detect that a key is being pressed, not HAS been pressed earlier!??

Richie Hindle richie at
Thu Jan 29 13:39:13 CET 2004

> It's important to notice here that I'm not interested if the user has
> already pressed shift or ctrl. I'm only interested in knowing if he is
> currently holding down one of these keys.

> No idea about linux, but on windows you use win32api.GetKeyState().

You mean GetAsyncKeyState: "The GetAsyncKeyState function determines
whether a key is up or down at the time the function is called"

Not GetKeyState: "The key status returned from this function changes as a
thread reads key messages from its message queue. The status does not
reflect the interrupt-level state associated with the hardware."

...assuming, Rune, that you *really do* want the current state.  If your
program is event driven, and you're responding to Shift+Click or Ctrl+Drag
or similar, then you want GetKeyState.  Imagine the machine is running
very slowly.  I Shift+Click your app and release the Shift key before your
app responds.  You call GetAsyncKeyState and see that Shift is not
pressed.  Had you used GetKeyState, it would tell you that the Shift key
was pressed at the time of the click.

Richie Hindle
richie at

More information about the Python-list mailing list