-
Notifications
You must be signed in to change notification settings - Fork 1
/
keypress.py
executable file
·52 lines (41 loc) · 1.01 KB
/
keypress.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
# getKey():
# Waits for a keyboard input and determines which key was pressed, up, down, left or right.
# It supports windows, linux and OSX
# returns:
# 'up', 'down', 'left' or 'right' respectively if the up, down, left or right keys are pressed.
# None is returned, otherwise.
def getKey():
# if windows
if os.name == 'nt':
from msvcrt import getch
UP,DOWN,LEFT,RIGHT = 80,72,75,77
key = ord(getch())
if key == 224:
key = ord(getch())
if key == 80:
return 'down'
elif key == 72:
return 'up'
elif key == 75:
return 'left'
elif key == 77:
return 'right'
return None
# not windows
else:
import sys, tty, termios
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
tty.setraw(fd)
c = sys.stdin.read(3)
termios.tcsetattr(fd, termios.TCSADRAIN, old)
print c
if c=='\x1b[A':
return 'up'
elif c=='\x1b[B':
return 'down'
elif c=='\x1b[C':
return 'right'
elif c=='\x1b[D':
return 'left'