-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetkey.py
31 lines (28 loc) · 810 Bytes
/
getkey.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import sys
if sys.platform[:3] == 'win':
import msvcrt
def getkey():
key = msvcrt.getch()
if ord(key) == 224: #catch second event with arrow keys
key = msvcrt.getch()
return key
elif sys.platform[:3] == 'lin':
import termios, sys, os
TERMIOS = termios
def getkey():
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
new[6][TERMIOS.VMIN] = 1
new[6][TERMIOS.VTIME] = 0
termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
c = None
try:
c = os.read(fd, 1)
finally:
termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
return c
while True:
k = getkey().decode()
print(k)