Skip to content

Instantly share code, notes, and snippets.

@foldfelis
Created February 7, 2022 15:18
Show Gist options
  • Save foldfelis/2ba09b44ac56c9c0cc371cf5522fa4a9 to your computer and use it in GitHub Desktop.
Save foldfelis/2ba09b44ac56c9c0cc371cf5522fa4a9 to your computer and use it in GitHub Desktop.
Raw mode in python
import sys
import tty
import termios
def raw():
# save old setting
file_desc = sys.stdin.fileno()
old_setting = termios.tcgetattr(file_desc)
# enable raw mode
tty.setraw(sys.stdin)
# read one key
k = str(sys.stdin.read(1))
while k != 'q':
# output
if k == 'w':
print("\r\033[0K up", end='')
elif k == 's':
print("\r\033[0K down", end='')
elif k == 'a':
print("\r\033[0K left", end='')
elif k == 'd':
print("\r\033[0K right", end='')
sys.stdout.flush()
# read one key
k = str(sys.stdin.read(1))
# reset raw mode
termios.tcsetattr(file_desc, termios.TCSADRAIN, old_setting)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment