-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyboard.py
More file actions
193 lines (147 loc) · 5.37 KB
/
keyboard.py
File metadata and controls
193 lines (147 loc) · 5.37 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python
'''
A Python class implementing KBHIT, the standard keyboard-interrupt poller.
Works transparently on Windows and Posix (Linux, Mac OS X). Doesn't work
with IDLE.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
'''
import os
import logging
LOG_NAME = "keyboard"
# Windows
if os.name == 'nt':
import msvcrt
# Posix (Linux, OS X)
else:
import sys
import termios
import atexit
from select import select
class KBHit:
def __init__(self):
'''Creates a KBHit object that you can call to do various keyboard things.
'''
if os.name == 'nt':
pass
else:
# Save the terminal settings
self.fd = sys.stdin.fileno()
self.new_term = termios.tcgetattr(self.fd)
self.old_term = termios.tcgetattr(self.fd)
# New terminal setting unbuffered
self.new_term[3] = (self.new_term[3] & ~termios.ICANON & ~termios.ECHO)
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.new_term)
# Support normal-terminal reset at exit
atexit.register(self.set_normal_term)
def set_normal_term(self):
''' Resets to normal terminal. On Windows this is a no-op.
'''
if os.name == 'nt':
pass
else:
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old_term)
def empty( self ):
while self.kbhit (): self.getch ()
def getch(self):
''' Returns a keyboard character after kbhit() has been called.
Should not be called in the same program as getarrow().
'''
s = ''
if os.name == 'nt':
log = logging.getLogger ( LOG_NAME )
c1 = msvcrt.getch ()
sc = False # sc = special char
arrow = False
#
if c1 == b'\x00':
c1 = msvcrt.getch ()
sc = True
elif c1 == b'\xE0':
c1 = msvcrt.getch ()
arrow = True
try:
c2 = c1.decode ( 'utf-8' )
#log.debug ( f"getch-{c1=}, {c2=}" )
if sc:
match c2:
case ';': return "F1"
case '<': return "F2"
case '=': return "F3"
case '>': return "F4"
case _: log.debug ( f"special char -> getch-{c1=}, {c2=}" )
elif arrow:
match c2:
case 'H': return "UP"
case 'K': return "LEFT"
case 'P': return "DOWN"
case 'M': return "RIGHT"
case _: log.debug ( f"arrow char -> getch-{c1=}, {c2=}" )
else:
match c2:
case '\x1B': return "ESC"
case '\x0D': return "ENTER"
case '\x08': return "BS"
case _: log.debug ( f"normal char -> getch-{c1=}, {c2=}" )
if ( c2 >= 'a' and c2 <= 'z' ) or \
( c2 >= 'A' and c2 <= 'Z' ) or \
( c2 >= '0' and c2 <= '9' ):
return c2
else:
return ""
except Exception as err:
log.error ( f"getch-{c1=}\n{err}" )
return None
else:
return sys.stdin.read(1)
def getarrow(self):
''' Returns an arrow-key code after kbhit() has been called. Codes are
0 : up
1 : right
2 : down
3 : left
Should not be called in the same program as getch().
'''
if os.name == 'nt':
msvcrt.getch() # skip 0xE0
c = msvcrt.getch()
log = logging.getLogger ( LOG_NAME )
log.debug ( f"getarrow {c=}" )
vals = [72, 77, 80, 75]
else:
c = sys.stdin.read(3)[2]
vals = [65, 67, 66, 68]
return vals.index(ord(c.decode('utf-8')))
def kbhit(self):
''' Returns True if keyboard character was hit, False otherwise.
'''
if os.name == 'nt':
return msvcrt.kbhit()
else:
dr,dw,de = select([sys.stdin], [], [], 0)
return dr != []
def flush( self ):
while self.kbhit():
self.getch ()
# Test
# if __name__ == "__main__":
#
# kb = KBHit()
#
# print('Hit any key, or ESC to exit')
#
# while True:
#
# if kb.kbhit():
# c = kb.getch()
# if c == "ESC":
# break
# print(c)
#
# kb.set_normal_term()