-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeypress.py
80 lines (59 loc) · 1.72 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: UTF-8 -*-
try:
import termios
except ImportError:
# Assume windows
import msvcrt
UP, UP1, DOWN, DOWN1, RIGHT, RIGHT1, LEFT, LEFT1 = 119, 72, 115, 80, 100, 77, 97,75
# arrow keys: 72,80,77,75
def getKey():
while True:
if msvcrt.kbhit():
a = ord(msvcrt.getch())
if a == 224:
a = ord(msvcrt.getch())
return a
else:
pass
# # refs:
# # http://bytes.com/topic/python/answers/630206-check-keypress-linux-xterm
# # http://stackoverflow.com/a/2521032/735926
# import sys
# import tty
# __fd = sys.stdin.fileno()
# __old = termios.tcgetattr(__fd)
# # Arrow keys
# # they are preceded by 27 and 91, hence the double 'if' in getKey.
# UP, DOWN, RIGHT, LEFT = 65, 66, 67, 68
# # Vim keys
# K, J, L, H = 107, 106, 108, 104
# __key_aliases = {
# K: UP,
# J: DOWN,
# L: RIGHT,
# H: LEFT,
# }
# def __getKey():
# """Return a key pressed by the user"""
# try:
# tty.setcbreak(sys.stdin.fileno())
# termios.tcflush(sys.stdin, termios.TCIOFLUSH)
# ch = sys.stdin.read(1)
# return ord(ch) if ch else None
# finally:
# termios.tcsetattr(__fd, termios.TCSADRAIN, __old)
# def getKey():
# """
# same as __getKey, but handle arrow keys
# """
# k = __getKey()
# if k == 27:
# k = __getKey()
# if k == 91:
# k = __getKey()
# return __key_aliases.get(k, k)
# # legacy support
# getArrowKey = getKey
if __name__ == "__main__":
while(True):
print getKey()