-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeymap.py
120 lines (101 loc) · 4.56 KB
/
keymap.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
from tkinter import messagebox, Toplevel, Button, PhotoImage
from functools import partial
class Keymap:
def __init__(self, root):
"""Represents the Chip-8 hexadecimal keypad"""
self.root = root
self.keymap_frame = None
self.width = 300
self.height = 300
# Whether keymap editor is open
self.editing = False
# Whether we are listening for keyboard input to map
self.listening = False
# The target hex value to map (0x0-0xF)
self.target_key = 0
# Keyboard input mapped to hex keypad
self.keyboard = {
'0': 0x0,
'1': 0x1,
'2': 0x2,
'3': 0x3,
'4': 0x4,
'5': 0x5,
'6': 0x6,
'7': 0x7,
'8': 0x8,
'9': 0x9,
'a': 0xA,
'b': 0xB,
'c': 0xC,
'd': 0xD,
'e': 0xE,
'f': 0xF}
# Inverse dictionary for removing old keys mapped to the target hex value
self.inverse_keyboard = {value: key for key, value in self.keyboard.items()}
# Current hex key that is down
self.key = 0
self.keydown = False
# Dictionary of tk buttons
self.button = {}
def add_button(self, hex_value, row, column):
self.button[hex_value] = Button(master=self.keymap_frame, text=hex_value,
compound='c', command=partial(self.add_listener, hex_value))
self.button[hex_value].grid(row=row, column=column)
def open_window(self):
"""Opens a new window for keymap editing"""
self.editing = True
self.keymap_frame = Toplevel(self.root, width=self.width, height=self.height)
self.keymap_frame.title("Keyboard Settings")
self.keymap_frame.protocol("WM_DELETE_WINDOW", self.close_window)
self.keymap_frame.bind('<Key>', self.add_key)
self.add_button(self.inverse_keyboard[0x1].upper(), 0, 0)
self.add_button(self.inverse_keyboard[0x2].upper(), 0, 1)
self.add_button(self.inverse_keyboard[0x3].upper(), 0, 2)
self.add_button(self.inverse_keyboard[0xC].upper(), 0, 3)
self.add_button(self.inverse_keyboard[0x4].upper(), 1, 0)
self.add_button(self.inverse_keyboard[0x5].upper(), 1, 1)
self.add_button(self.inverse_keyboard[0x6].upper(), 1, 2)
self.add_button(self.inverse_keyboard[0xD].upper(), 1, 3)
self.add_button(self.inverse_keyboard[0x7].upper(), 2, 0)
self.add_button(self.inverse_keyboard[0x8].upper(), 2, 1)
self.add_button(self.inverse_keyboard[0x9].upper(), 2, 2)
self.add_button(self.inverse_keyboard[0xE].upper(), 2, 3)
self.add_button(self.inverse_keyboard[0xA].upper(), 3, 0)
self.add_button(self.inverse_keyboard[0x0].upper(), 3, 1)
self.add_button(self.inverse_keyboard[0xB].upper(), 3, 2)
self.add_button(self.inverse_keyboard[0xF].upper(), 3, 3)
def save_keymap(self):
"""Save the current keymap layout"""
return
def load_keymap(self):
"""Load a saved keymap layout"""
return
def close_window(self):
self.editing = False
self.listening = False
self.target_key = 0
self.keymap_frame.destroy()
def add_listener(self, hex_value):
self.target_key = int(hex_value, 16)
self.listening = True
def process_keypress(self, event):
if event.char in self.keyboard:
print('Hex key down: ', self.keyboard[event.char])
self.key = self.keyboard[event.char]
self.keydown = True
def process_keyrelease(self, event):
print('Hex key released: ', self.keyboard[event.char])
self.keydown = False
def add_key(self, event):
"""Adds a key to the keymap"""
if self.listening:
# Remove the old entry
del self.keyboard[self.inverse_keyboard[self.target_key]]
# Update both dictionaries
self.inverse_keyboard[self.target_key] = event.char
self.keyboard[event.char] = self.target_key
# Update button text
self.button[str(hex(self.target_key)[2]).upper()]['text'] = event.char.upper()
print('Added key mapping: ' + repr(event.char) + ' to ' + hex(self.target_key))
self.listening = False