Skip to content

Instantly share code, notes, and snippets.

@PageotD
Forked from jasonrdsouza/key_detect.py
Created August 18, 2024 07:27
Show Gist options
  • Save PageotD/ddf7a87bf37b276585c3f547c499760f to your computer and use it in GitHub Desktop.
Save PageotD/ddf7a87bf37b276585c3f547c499760f to your computer and use it in GitHub Desktop.
Python function to get keypresses from the terminal
def getchar():
#Returns a single character from standard input
import tty, termios, sys
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
while 1:
ch = getchar()
print 'You pressed', ch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment