-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu_keyboard_mapping.py
81 lines (68 loc) · 1.75 KB
/
cpu_keyboard_mapping.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
import os, sys
from pynput import keyboard
os.environ['TERM'] = 'xterm'
def cls():
os.system('cls' if os.name == 'nt' else 'clear')
for i in range(9):
print()
def on_release(key):
keyaction(key, False)
def keyaction(key, release):
try:
keychar = key.char
button = KeyPad.buttons[cpu_keyboard_mapping.key_mapper(keychar)]
try:
if button._state != release:
button.change_state(True)
else:
print(f"Redundant key action, already registered in {button._state}")
except (KeyError):
print("Unregistered keypress", key)
except (AttributeError):
pass
def key_mapper(key):
if key.char == '1':
return 0, 0
elif key.char == '2':
return 0, 1
elif key.char == '3':
return 0, 2
elif key.char == '4':
return 0, 3
## second row
elif key.char == 'q':
return 1, 0
elif key.char == 'w':
return 1, 1
elif key.char == 'e':
return 1, 2
elif key.char == 'r':
return 1, 3
# Third row
elif key.char == 'a':
return 2, 0
elif key.char == 's':
return 2, 1
elif key.char == 'd':
return 2, 2
elif key.char == 'f':
return 2, 3
## Fourth row
elif key.char == 'z':
return 3, 0
elif key.char == 'x':
return 3, 1
elif key.char == 'c':
return 3, 2
elif key.char == 'v':
return 3, 3
else:
return None
cls()
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
try:
listener.join()
except MyException as e:
print('Exception occurred when {0} was pressed '.format(e.args[0]))
def on_press(key):
keyaction(key, True)