Skip to content

Instantly share code, notes, and snippets.

@kissarat
Created June 13, 2021 10:20
Show Gist options
  • Save kissarat/9f9760a68a20596606be8512f4c1072c to your computer and use it in GitHub Desktop.
Save kissarat/9f9760a68a20596606be8512f4c1072c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import signal
import sys
def _find_getch():
try:
import termios
except ImportError:
# Non-POSIX. Return msvcrt's (Windows') getch.
import msvcrt
return msvcrt.getch
# POSIX system. Create and return a getch that manipulates the tty.
import sys, tty
def _getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
return _getch
# def signal_handler(sig, frame):
# print('You pressed Ctrl+C!')
# sys.exit(0)
# signal.signal(signal.SIGINT, signal_handler)
getch = _find_getch()
while True:
char = getch()
char_ord = ord(char)
print('%s - %i (%s)' % (char, char_ord, hex(char_ord)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment