Chris Hengge wrote:
> I posted this in a similar thread a few days ago, and no replies so I 
> think it needs its own listing.
> 
> Anyone know of a way to capture special keys like "Print Screen"?
> I have a small script to grab all they keycodes, but it doesn't seem to 
> catch several keys on the keyboard. I've got a utility that I'd like to 
> be able to automagically get a screenshot when something goes wrong so I 
> dont have to hope the user can re-create the error. Universal support 
> would be best, but WinXP is the main OS
> 

I'm not exactly sure what you want here :-) but if you want to capture
when the 'Print Screen' key (or any other key) has actually been
pressed, try pyHook. Note: pyHook only works on Windows!

Here's some sample code:

<code>

"""
Captures a press of the 'Print Screen' key.
The following requires ctypes and pyHook.
ctypes is standard in Python2.5.

ctypes -> http://sourceforge.net/projects/ctypes/
pyHook -> http://www.cs.unc.edu/Research/assist/developer.shtml
"""
from ctypes import windll, byref
from ctypes.wintypes import MSG

import pyHook

user32 = windll.user32

def keyboardEvent(event):
     if event.KeyID == 44: print "You hit 'Print Screen'!"
     return True

hm = pyHook.HookManager()
hm.KeyDown = keyboardEvent
hm.HookKeyboard()
msg = MSG()
while user32.GetMessageA(byref(msg), None, 0, 0):
     user32.TranslateMessage(byref(msg))
     user32.DispatchMessageA(byref(msg))

</code>

HTH,

Bill




_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to