Created
February 7, 2022 15:18
-
-
Save foldfelis/2ba09b44ac56c9c0cc371cf5522fa4a9 to your computer and use it in GitHub Desktop.
Raw mode in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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