Skip to content
/ kbhit Public

A Python class implementing KBHIT, the standard keyboard-interrupt poller.

License

Notifications You must be signed in to change notification settings

lvlte/kbhit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

kbhit

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.

Usage

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))

Non-ASCII characters

Unicode charset is supported provided that your system use the proper encoding (eg. utf-8).
See PYTHONIOENCODING

CTRL Keys

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.

SIG Keys

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))

Credits

About

A Python class implementing KBHIT, the standard keyboard-interrupt poller.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages