-
Notifications
You must be signed in to change notification settings - Fork 2
/
KeyboardTranslatorBasic.py
142 lines (142 loc) · 4.74 KB
/
KeyboardTranslatorBasic.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
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
import pygame
class KeyboardTranslatorBasic(object):
CSI = '\x1b[' #ESC[
def __init__(self, **kwargs):
return
def translate(self, key, mod):
if mod & pygame.KMOD_SHIFT: #FIXME: Hardcoded UK layout mess
if key==ord('`'):
return b'\xaa' #FIXME: hardcoded cp437
if key==ord('1'):
key = ord('!')
if key==ord('2'):
key = ord('"')
if key==ord('3'):
return b'\x9c' #FIXME: hardcoded cp437
if key==ord('4'):
key = ord('$')
if key==ord('5'):
key = ord('%')
if key==ord('6'):
key = ord('^')
if key==ord('7'):
key = ord('&')
if key==ord('8'):
key = ord('*')
if key==ord('9'):
key = ord('(')
if key==ord('0'):
key = ord(')')
if key==ord('-'):
key = ord('_')
if key==ord('='):
key = ord('+')
if key==ord('['):
key = ord('{')
if key==ord(']'):
key = ord('}')
if key==ord(';'):
key = ord(':')
if key==ord("'"):
key = ord('@')
if key==ord('#'):
key = ord('~')
if key==ord(','):
key = ord('<')
if key==ord('.'):
key = ord('>')
if key==ord('/'):
key = ord('?')
if key==ord('\\'):
key = ord('|')
elif key >= ord('a') and key <= ord('z'):
key = ord(chr(key).upper())
if key >= 0x20 and key <= 0x7e:
return key.to_bytes(1, byteorder='big')
elif key >= 0x20 and key <= 0x7b:
return key.to_bytes(1, byteorder='big')
elif key==pygame.K_RETURN or key==pygame.K_KP_ENTER:
return b'\r'
elif key==pygame.K_TAB:
return b'\t'
elif key==pygame.K_BACKSPACE:
return b'\b'
elif key==pygame.K_ESCAPE:
return b'\x1b'
elif key==pygame.K_LEFT:
return self.CSI.encode("cp437") + b'D'
elif key==pygame.K_RIGHT:
return self.CSI.encode("cp437") + b'C'
elif key==pygame.K_UP:
return self.CSI.encode("cp437") + b'A'
elif key==pygame.K_DOWN:
return self.CSI.encode("cp437") + b'B'
elif key==pygame.K_INSERT:
return self.CSI.encode("cp437") + b'@'
elif key==pygame.K_DELETE:
return b'\x7f'
elif key==pygame.K_HOME:
return self.CSI.encode("cp437") + b'H'
elif key==pygame.K_END:
return self.CSI.encode("cp437") + b'K'
elif key==pygame.K_PAGEUP:
return self.CSI.encode("cp437") + b'V'
elif key==pygame.K_PAGEDOWN:
return self.CSI.encode("cp437") + b'U'
elif key==pygame.K_F1:
return b'\x1bOP'
elif key==pygame.K_F2:
return b'\x1bOQ'
elif key==pygame.K_F3:
return b'\x1bOR'
elif key==pygame.K_F4:
return b'\x1bOS'
elif key==pygame.K_F5:
return b'\x1bOt'
elif key==pygame.K_F6:
return self.CSI.encode("cp437") + b'17~'
elif key==pygame.K_F7:
return self.CSI.encode("cp437") + b'18~'
elif key==pygame.K_F8:
return self.CSI.encode("cp437") + b'19~'
elif key==pygame.K_F9:
return self.CSI.encode("cp437") + b'20~'
elif key==pygame.K_F10:
return self.CSI.encode("cp437") + b'21~'
elif key==pygame.K_F11:
return self.CSI.encode("cp437") + b'23~'
elif key==pygame.K_F12:
return self.CSI.encode("cp437") + b'24~'
elif key==pygame.K_KP_DIVIDE:
return b'/'
elif key==pygame.K_KP_MULTIPLY:
return b'*'
elif key==pygame.K_KP_MINUS:
return b'-'
elif key==pygame.K_KP_PLUS:
return b'+'
elif key==pygame.K_KP_PERIOD:
return b'.'
elif key==pygame.K_KP0:
return b'0'
elif key==pygame.K_KP1:
return b'1'
elif key==pygame.K_KP2:
return b'2'
elif key==pygame.K_KP3:
return b'3'
elif key==pygame.K_KP4:
return b'4'
elif key==pygame.K_KP5:
return b'5'
elif key==pygame.K_KP6:
return b'6'
elif key==pygame.K_KP7:
return b'7'
elif key==pygame.K_KP8:
return b'8'
elif key==pygame.K_KP9:
return b'9'
else:
print(f"Not sure how to process keycode: {key}")
return