Reputation: 11011
I have a timer, and need to know if any of the keys is pressed on any cycle. How do I do it?
Upvotes: 0
Views: 526
Reputation: 123831
If you are using Windows, Use PyHook If you like to know system wide key press events.
import pythoncom, pyHook
def OnKeyboardEvent(event):
print 'Ascii:', event.Ascii, chr(event.Ascii)
print 'Key:', event.Key
print 'KeyID:', event.KeyID
print 'ScanCode:', event.ScanCode
print 'Extended:', event.Extended
return True #for pass through key events, False to eat Keys
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()
Upvotes: 0
Reputation: 5082
If you are using Linux it's found in the curses
module, if you use Windows it's in the msvcrt
module.
I found following article really helpful in describing this topic - Event Driven Programming
Upvotes: 1