269 lines
7.7 KiB
Python
269 lines
7.7 KiB
Python
|
from typing import Set, Tuple, Any
|
||
|
|
||
|
# From Events.h
|
||
|
MAC_KEY_NAMES = {
|
||
|
0x00: "A",
|
||
|
0x01: "S",
|
||
|
0x02: "D",
|
||
|
0x03: "F",
|
||
|
0x04: "H",
|
||
|
0x05: "G",
|
||
|
0x06: "Z",
|
||
|
0x07: "X",
|
||
|
0x08: "C",
|
||
|
0x09: "V",
|
||
|
0x0A: "International",
|
||
|
0x0B: "B",
|
||
|
0x0C: "Q",
|
||
|
0x0D: "W",
|
||
|
0x0E: "E",
|
||
|
0x0F: "R",
|
||
|
0x10: "Y",
|
||
|
0x11: "T",
|
||
|
0x12: "1",
|
||
|
0x13: "2",
|
||
|
0x14: "3",
|
||
|
0x15: "4",
|
||
|
0x16: "6",
|
||
|
0x17: "5",
|
||
|
0x18: "Equal",
|
||
|
0x19: "9",
|
||
|
0x1A: "7",
|
||
|
0x1B: "Minus",
|
||
|
0x1C: "8",
|
||
|
0x1D: "0",
|
||
|
0x1E: "RightBracket",
|
||
|
0x1F: "O",
|
||
|
0x20: "U",
|
||
|
0x21: "LeftBracket",
|
||
|
0x22: "I",
|
||
|
0x23: "P",
|
||
|
0x25: "L",
|
||
|
0x26: "J",
|
||
|
0x27: "Quote",
|
||
|
0x28: "K",
|
||
|
0x29: "Semicolon",
|
||
|
0x2A: "Backslash",
|
||
|
0x2B: "Comma",
|
||
|
0x2C: "Slash",
|
||
|
0x2D: "N",
|
||
|
0x2E: "M",
|
||
|
0x2F: "Period",
|
||
|
0x32: "Grave",
|
||
|
0x41: "KPDecimal",
|
||
|
0x43: "KPMultiply",
|
||
|
0x45: "KPPlus",
|
||
|
0x47: "KPClear",
|
||
|
0x4B: "KPDivide",
|
||
|
0x4C: "KPEnter",
|
||
|
0x4E: "KPMinus",
|
||
|
0x51: "KPEquals",
|
||
|
0x52: "KP0",
|
||
|
0x53: "KP1",
|
||
|
0x54: "KP2",
|
||
|
0x55: "KP3",
|
||
|
0x56: "KP4",
|
||
|
0x57: "KP5",
|
||
|
0x58: "KP6",
|
||
|
0x59: "KP7",
|
||
|
0x5B: "KP8",
|
||
|
0x5C: "KP9",
|
||
|
0x24: "Return",
|
||
|
0x30: "Tab",
|
||
|
0x31: "Space",
|
||
|
0x33: "Del",
|
||
|
0x35: "Escape",
|
||
|
0x37: "Command",
|
||
|
0x38: "Shift",
|
||
|
0x39: "CapsLock",
|
||
|
0x3A: "Option",
|
||
|
0x3B: "Control",
|
||
|
0x3C: "RightShift",
|
||
|
0x3D: "RightOption",
|
||
|
0x3E: "RightControl",
|
||
|
0x3F: "Function",
|
||
|
0x40: "F17",
|
||
|
0x48: "VolumeUp",
|
||
|
0x49: "VolumeDown",
|
||
|
0x4A: "Mute",
|
||
|
0x4F: "F18",
|
||
|
0x50: "F19",
|
||
|
0x5A: "F20",
|
||
|
0x60: "F5",
|
||
|
0x61: "F6",
|
||
|
0x62: "F7",
|
||
|
0x63: "F3",
|
||
|
0x64: "F8",
|
||
|
0x65: "F9",
|
||
|
0x67: "F11",
|
||
|
0x69: "F13",
|
||
|
0x6A: "F16",
|
||
|
0x6B: "F14",
|
||
|
0x6D: "F10",
|
||
|
0x6F: "F12",
|
||
|
0x71: "F15",
|
||
|
0x72: "Help",
|
||
|
0x73: "Home",
|
||
|
0x74: "PageUp",
|
||
|
0x75: "FwdDel",
|
||
|
0x76: "F4",
|
||
|
0x77: "End",
|
||
|
0x78: "F2",
|
||
|
0x79: "PageDown",
|
||
|
0x7A: "F1",
|
||
|
# 0x7B: "LeftArrow",
|
||
|
# 0x7C: "RightArrow",
|
||
|
# 0x7D: "DownArrow",
|
||
|
# 0x7E: "UpArrow",
|
||
|
}
|
||
|
|
||
|
# From https://gist.github.com/rickyzhang82/8581a762c9f9fc6ddb8390872552c250
|
||
|
# Highly modified
|
||
|
LINUX_KEYCODE_NAMES = {
|
||
|
9: "Escape", # Esc
|
||
|
10: "1", # 1
|
||
|
11: "2", # 2
|
||
|
12: "3", # 3
|
||
|
13: "4", # 4
|
||
|
14: "5", # 5
|
||
|
15: "6", # 6
|
||
|
16: "7", # 7
|
||
|
17: "8", # 8
|
||
|
18: "9", # 9
|
||
|
19: "0", # 0
|
||
|
20: "Minus", # -
|
||
|
21: "Equal", # =
|
||
|
22: "Backspace", # Backspace
|
||
|
23: "Tab", # Tab
|
||
|
24: "Q", # Q
|
||
|
25: "W", # W
|
||
|
26: "E", # E
|
||
|
27: "R", # R
|
||
|
28: "T", # T
|
||
|
29: "Y", # Y
|
||
|
30: "U", # U
|
||
|
31: "I", # I
|
||
|
32: "O", # O
|
||
|
33: "P", # P
|
||
|
34: "LeftBracket", # [
|
||
|
35: "RightBracket", # ]
|
||
|
36: "Return", # Return
|
||
|
37: "Control", # Ctrl Left
|
||
|
38: "A", # A
|
||
|
39: "S", # S
|
||
|
40: "D", # D
|
||
|
41: "F", # F
|
||
|
42: "G", # G
|
||
|
43: "H", # H
|
||
|
44: "J", # J
|
||
|
45: "K", # K
|
||
|
46: "L", # L
|
||
|
47: "Semicolon", # ;
|
||
|
48: "Quote", # '
|
||
|
49: "International", # `
|
||
|
50: "Shift", # Shift Left
|
||
|
51: "Backslash", # \
|
||
|
52: "Z", # Z
|
||
|
53: "X", # X
|
||
|
54: "C", # C
|
||
|
55: "V", # V
|
||
|
56: "B", # B
|
||
|
57: "N", # N
|
||
|
58: "M", # M
|
||
|
59: "Comma", # ,
|
||
|
60: "Period", # .
|
||
|
61: "Slash", # /
|
||
|
62: "RightShift", # Shift Right
|
||
|
63: "KPMultiply", # KP *
|
||
|
64: "Option", # Alt Left (-> Command)
|
||
|
65: "Space", # Space
|
||
|
66: "CapsLock", # Caps Lock
|
||
|
67: "F1", # F1
|
||
|
68: "F2", # F2
|
||
|
69: "F3", # F3
|
||
|
70: "F4", # F4
|
||
|
71: "F5", # F5
|
||
|
72: "F6", # F6
|
||
|
73: "F7", # F7
|
||
|
74: "F8", # F8
|
||
|
75: "F9", # F9
|
||
|
76: "F10", # F10
|
||
|
# 77: "Num Lock", # Num Lock
|
||
|
# 78: "Scroll Lock", # Scroll Lock
|
||
|
79: "KP7", # KP 7
|
||
|
80: "KP8", # KP 8
|
||
|
81: "KP9", # KP 9
|
||
|
82: "KPMinus", # KP -
|
||
|
83: "KP4", # KP 4
|
||
|
84: "KP5", # KP 5
|
||
|
85: "KP6", # KP 6
|
||
|
86: "KPPlus", # KP +
|
||
|
87: "KP1", # KP 1
|
||
|
88: "KP2", # KP 2
|
||
|
89: "KP3", # KP 3
|
||
|
90: "KP0", # KP 0
|
||
|
91: "KPDecimal", # KP .
|
||
|
94: "Grave", # International
|
||
|
95: "F11", # F11
|
||
|
96: "F12", # F12
|
||
|
97: "Home", # Home
|
||
|
98: "UpArrow", # Cursor Up
|
||
|
99: "PageUp", # Page Up
|
||
|
100: "LeftArrow", # Cursor Left
|
||
|
102: "RightArrow", # Cursor Right
|
||
|
103: "End", # End
|
||
|
104: "DownArrow", # Cursor Down
|
||
|
# 105: "PageDown", # Page Down
|
||
|
# 106: "Insert", # Insert
|
||
|
107: "Del", # Delete
|
||
|
108: "KPEnter", # KP Enter
|
||
|
# 109: "RightControl", # Ctrl Right
|
||
|
# 110: "Pause", # Pause
|
||
|
# 111: "PrintScrn", # PrintScrn
|
||
|
112: "KPDivide", # KP /
|
||
|
113: "RightOption", # Alt Right (-> Command)
|
||
|
115: "Command", # Logo Left (-> Option)
|
||
|
116: "Command", # Logo Right (-> Option)
|
||
|
# 117: "Menu (-> International)", # Menu (-> International)
|
||
|
}
|
||
|
|
||
|
# Represenetd by the Mac virtual keycodes
|
||
|
class KeyCode:
|
||
|
def __init__(self, kc: int):
|
||
|
self.kc = kc
|
||
|
|
||
|
def __eq__(self, other: Any) -> bool:
|
||
|
if isinstance(other, KeyCode):
|
||
|
return self.kc == other.kc
|
||
|
return False
|
||
|
|
||
|
def __hash__(self) -> int:
|
||
|
return hash(self.kc) ^ 1
|
||
|
|
||
|
def __str__(self) -> str:
|
||
|
if self.kc in MAC_KEY_NAMES:
|
||
|
return MAC_KEY_NAMES[self.kc]
|
||
|
else:
|
||
|
return f"mac_{self.kc}"
|
||
|
|
||
|
__repr__ = __str__
|
||
|
|
||
|
def linux_keycodes(self) -> Set[Tuple[int, str]]:
|
||
|
if self.kc not in MAC_KEY_NAMES:
|
||
|
return set()
|
||
|
|
||
|
res = set()
|
||
|
for n, name in LINUX_KEYCODE_NAMES.items():
|
||
|
if name == MAC_KEY_NAMES[self.kc]:
|
||
|
res.add((n, f"<K{n}>"))
|
||
|
|
||
|
return res
|
||
|
|
||
|
KEYCODES = [KeyCode(x) for x in MAC_KEY_NAMES.keys()]
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
for n, name in MAC_KEY_NAMES.items():
|
||
|
linuxs = KeyCode(n).linux_keycodes()
|
||
|
if len(linuxs) == 0:
|
||
|
print(f"{name} has no linux variant...")
|