Take the 2-minute tour ×

I have a timer, and need to know if any of the keys is pressed on any cycle. How do I do it?

share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

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
share|improve this answer
add comment

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

share|improve this answer
msvcrt.getch() seems to crash my wx application –  Gabriele Cirulli Nov 23 '09 at 21:57
Either post a traceback information or give more detail on what is going on in code. Even using msvcrt, you will need to write event handlers and call them from within your timer loop. However, considering your comment about background execution, msvcrt should be what you are looking for. You will just need to figure out how to use it in your particular case. –  artdanil Nov 23 '09 at 22:04
there's no traceback, my application just locks up. –  Gabriele Cirulli Nov 24 '09 at 17:16
add comment


import sys
c = sys.stdin.read(1)
share|improve this answer
Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> c = sys.stdin.read(1) AttributeError: read Also, i need a way to get information about keys pressed in the whole pc, even if my wx application is in the background. –  Gabriele Cirulli Nov 23 '09 at 21:47
add comment

Your Answer


