kettlepot
kettlepot

Reputation: 11011

Knowing if any key is pressed, wxPython

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

Answers (3)

YOU
YOU

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

artdanil
artdanil

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

motto
motto

Reputation: 1306

Try:

import sys
c = sys.stdin.read(1)

Upvotes: 0

Related Questions