A Python class implementing KBHIT, the standard keyboard-interrupt poller.
Works transparently on Windows and Posix (Linux, Mac OS X) - except for CTRL keys.
Doesn't work from REPL nor with IDLE.
from kbhit import KBHit
kb = KBHit()
print('Hit any key, or ESC to exit')
while True:
if kb.kbhit():
c = kb.getch()
if ord(c) == 27: # ESC
print('exiting...')
break
print(c, ord(c))
Unicode charset is supported provided that your system use the proper encoding (eg. utf-8).
See PYTHONIOENCODING
You can use getarrow()
to get arrow-key codes. The drawback is that you can't getch()
regular characters when your program is already expecting CTRL keys.
In other words, you can't use both functions in the same script.
You can still use signal
along with KBHit
to handle SIG keys. For example :
kb = KBHit()
import signal
import sys
def interrupt(sig, _):
print('SIGINT (Signal Interrupt) (2) : Interrupt from keyboard')
sys.exit(2)
# Register handler for interruption (CTRL + C).
# Alternatively you can catch KeyboardInterrupt exception in the while loop.
signal.signal(signal.SIGINT, interrupt)
print('Hit any key, or ESC to exit')
while True:
if kb.kbhit():
c = kb.getch()
if ord(c) == 27: # ESC
print('exiting...')
break
print(c, ord(c))
- Simon D. Levy (@simondlevy)
- Eric Lavault (@lvlte)