Added custom keycode functionality (#27)
* Added custom keycode functionality * Update KEYCODE_USER instead of creating new functionality * removed unused code * Removed customkeycode class, and used json object. Also added check that adds normal user, keycodes if no custom keycodes are defined * used .get for getting custom keycodes from payloadmain
parent
5cdb10c8cd
commit
a22ea578fd
|
|
@ -138,7 +138,6 @@ def macro_deserialize_v2(data):
|
||||||
out.append(cls(args))
|
out.append(cls(args))
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
class Keyboard:
|
class Keyboard:
|
||||||
""" Low-level communication with a vial-enabled keyboard """
|
""" Low-level communication with a vial-enabled keyboard """
|
||||||
|
|
||||||
|
|
@ -161,6 +160,7 @@ class Keyboard:
|
||||||
self.macro_memory = 0
|
self.macro_memory = 0
|
||||||
self.macro = b""
|
self.macro = b""
|
||||||
self.vibl = False
|
self.vibl = False
|
||||||
|
self.custom_keycodes = None
|
||||||
|
|
||||||
self.via_protocol = self.vial_protocol = self.keyboard_id = -1
|
self.via_protocol = self.vial_protocol = self.keyboard_id = -1
|
||||||
|
|
||||||
|
|
@ -222,6 +222,8 @@ class Keyboard:
|
||||||
self.rows = payload["matrix"]["rows"]
|
self.rows = payload["matrix"]["rows"]
|
||||||
self.cols = payload["matrix"]["cols"]
|
self.cols = payload["matrix"]["cols"]
|
||||||
|
|
||||||
|
self.custom_keycodes = payload.get("customKeycodes", None)
|
||||||
|
|
||||||
serial = KleSerial()
|
serial = KleSerial()
|
||||||
kb = serial.deserialize(payload["layouts"]["keymap"])
|
kb = serial.deserialize(payload["layouts"]["keymap"])
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -488,24 +488,7 @@ KEYCODES_MEDIA = [
|
||||||
K(132, "KC_LSCR", "Locking\nScroll", "Locking Scroll Lock", alias=["KC_LOCKING_SCROLL"]),
|
K(132, "KC_LSCR", "Locking\nScroll", "Locking Scroll Lock", alias=["KC_LOCKING_SCROLL"]),
|
||||||
]
|
]
|
||||||
|
|
||||||
KEYCODES_USER = [
|
KEYCODES_USER = []
|
||||||
K(0x5F80, "USER00", "User 0", "User keycode 0"),
|
|
||||||
K(0x5F81, "USER01", "User 1", "User keycode 1"),
|
|
||||||
K(0x5F82, "USER02", "User 2", "User keycode 2"),
|
|
||||||
K(0x5F83, "USER03", "User 3", "User keycode 3"),
|
|
||||||
K(0x5F84, "USER04", "User 4", "User keycode 4"),
|
|
||||||
K(0x5F85, "USER05", "User 5", "User keycode 5"),
|
|
||||||
K(0x5F86, "USER06", "User 6", "User keycode 6"),
|
|
||||||
K(0x5F87, "USER07", "User 7", "User keycode 7"),
|
|
||||||
K(0x5F88, "USER08", "User 8", "User keycode 8"),
|
|
||||||
K(0x5F89, "USER09", "User 9", "User keycode 9"),
|
|
||||||
K(0x5F8A, "USER10", "User 10", "User keycode 10"),
|
|
||||||
K(0x5F8B, "USER11", "User 11", "User keycode 11"),
|
|
||||||
K(0x5F8C, "USER12", "User 12", "User keycode 12"),
|
|
||||||
K(0x5F8D, "USER13", "User 13", "User keycode 13"),
|
|
||||||
K(0x5F8E, "USER14", "User 14", "User keycode 14"),
|
|
||||||
K(0x5F8F, "USER15", "User 15", "User keycode 15"),
|
|
||||||
]
|
|
||||||
|
|
||||||
KEYCODES_MACRO = []
|
KEYCODES_MACRO = []
|
||||||
|
|
||||||
|
|
@ -521,6 +504,29 @@ def recreate_keycodes():
|
||||||
KEYCODES.extend(KEYCODES_SPECIAL + KEYCODES_BASIC + KEYCODES_SHIFTED + KEYCODES_ISO + KEYCODES_LAYERS +
|
KEYCODES.extend(KEYCODES_SPECIAL + KEYCODES_BASIC + KEYCODES_SHIFTED + KEYCODES_ISO + KEYCODES_LAYERS +
|
||||||
KEYCODES_QUANTUM + KEYCODES_BACKLIGHT + KEYCODES_MEDIA + KEYCODES_MACRO + KEYCODES_USER)
|
KEYCODES_QUANTUM + KEYCODES_BACKLIGHT + KEYCODES_MEDIA + KEYCODES_MACRO + KEYCODES_USER)
|
||||||
|
|
||||||
|
def create_user_keycodes():
|
||||||
|
KEYCODES_USER.clear()
|
||||||
|
for x in range(16):
|
||||||
|
KEYCODES_USER.append(
|
||||||
|
Keycode(
|
||||||
|
0x5F80 + x,
|
||||||
|
"USER{:02}".format(x),
|
||||||
|
"User {}".format(x),
|
||||||
|
"User keycode {}".format(x)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
def create_custom_user_keycodes(custom_keycodes):
|
||||||
|
KEYCODES_USER.clear()
|
||||||
|
for x, c_keycode in enumerate(custom_keycodes):
|
||||||
|
KEYCODES_USER.append(
|
||||||
|
Keycode(
|
||||||
|
0x5F80 + x,
|
||||||
|
c_keycode.get("shortName", "USER{:02}".format(x)),
|
||||||
|
c_keycode.get("name", "USER{:02}".format(x)),
|
||||||
|
c_keycode.get("title", "USER{:02}".format(x))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def recreate_keyboard_keycodes(keyboard):
|
def recreate_keyboard_keycodes(keyboard):
|
||||||
""" Generates keycodes based on information the keyboard provides (e.g. layer keycodes, macros) """
|
""" Generates keycodes based on information the keyboard provides (e.g. layer keycodes, macros) """
|
||||||
|
|
@ -556,7 +562,12 @@ def recreate_keyboard_keycodes(keyboard):
|
||||||
lbl = "M{}".format(x)
|
lbl = "M{}".format(x)
|
||||||
KEYCODES_MACRO.append(Keycode(0x5F12 + x, lbl, lbl))
|
KEYCODES_MACRO.append(Keycode(0x5F12 + x, lbl, lbl))
|
||||||
|
|
||||||
|
# Check if custom keycodes are defined in keyboard, and if so add them to user keycodes
|
||||||
|
if keyboard.custom_keycodes is not None and len(keyboard.custom_keycodes) > 0:
|
||||||
|
create_custom_user_keycodes(keyboard.custom_keycodes)
|
||||||
|
else:
|
||||||
|
create_user_keycodes()
|
||||||
|
|
||||||
recreate_keycodes()
|
recreate_keycodes()
|
||||||
|
|
||||||
|
|
||||||
recreate_keycodes()
|
recreate_keycodes()
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
from PyQt5.QtCore import QSize
|
from PyQt5.QtCore import QSize, Qt
|
||||||
from PyQt5.QtWidgets import QPushButton
|
from PyQt5.QtWidgets import QPushButton, QLabel, QHBoxLayout
|
||||||
|
|
||||||
class SquareButton(QPushButton):
|
class SquareButton(QPushButton):
|
||||||
|
|
||||||
|
|
@ -9,11 +9,37 @@ class SquareButton(QPushButton):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
|
|
||||||
self.scale = 1.2
|
self.scale = 1.2
|
||||||
|
self.label = None
|
||||||
|
self.word_wrap = False
|
||||||
|
self.text = ""
|
||||||
|
|
||||||
def setRelSize(self, ratio):
|
def setRelSize(self, ratio):
|
||||||
self.scale = ratio
|
self.scale = ratio
|
||||||
self.updateGeometry()
|
self.updateGeometry()
|
||||||
|
|
||||||
|
def setWordWrap(self, state):
|
||||||
|
self.word_wrap = state
|
||||||
|
self.setText(self.text)
|
||||||
|
|
||||||
def sizeHint(self):
|
def sizeHint(self):
|
||||||
size = int(round(self.fontMetrics().height() * self.scale))
|
size = int(round(self.fontMetrics().height() * self.scale))
|
||||||
return QSize(size, size)
|
return QSize(size, size)
|
||||||
|
|
||||||
|
# Override setText to facilitate automatic word wrapping
|
||||||
|
def setText(self, text):
|
||||||
|
self.text = text
|
||||||
|
if self.word_wrap:
|
||||||
|
super().setText("")
|
||||||
|
if self.label is None:
|
||||||
|
self.label = QLabel(text, self)
|
||||||
|
self.label.setWordWrap(True)
|
||||||
|
self.label.setAlignment(Qt.AlignCenter)
|
||||||
|
layout = QHBoxLayout(self)
|
||||||
|
layout.setContentsMargins(0, 0, 0, 0)
|
||||||
|
layout.addWidget(self.label,0,Qt.AlignCenter)
|
||||||
|
else:
|
||||||
|
self.label.setText(text)
|
||||||
|
else:
|
||||||
|
if self.label is not None:
|
||||||
|
self.label.deleteLater()
|
||||||
|
super().setText(text)
|
||||||
|
|
@ -49,6 +49,8 @@ class TabbedKeycodes(QTabWidget):
|
||||||
self.layout_layers = layout
|
self.layout_layers = layout
|
||||||
elif tab == self.tab_macro:
|
elif tab == self.tab_macro:
|
||||||
self.layout_macro = layout
|
self.layout_macro = layout
|
||||||
|
elif tab == self.tab_user:
|
||||||
|
self.layout_user = layout
|
||||||
elif tab == self.tab_basic:
|
elif tab == self.tab_basic:
|
||||||
# create the "Any" keycode button
|
# create the "Any" keycode button
|
||||||
btn = SquareButton()
|
btn = SquareButton()
|
||||||
|
|
@ -70,13 +72,15 @@ class TabbedKeycodes(QTabWidget):
|
||||||
|
|
||||||
self.layer_keycode_buttons = []
|
self.layer_keycode_buttons = []
|
||||||
self.macro_keycode_buttons = []
|
self.macro_keycode_buttons = []
|
||||||
|
self.user_keycode_buttons = []
|
||||||
self.set_keymap_override(KEYMAPS[0][1])
|
self.set_keymap_override(KEYMAPS[0][1])
|
||||||
|
|
||||||
def create_buttons(self, layout, keycodes):
|
def create_buttons(self, layout, keycodes, wordWrap = False):
|
||||||
buttons = []
|
buttons = []
|
||||||
|
|
||||||
for keycode in keycodes:
|
for keycode in keycodes:
|
||||||
btn = SquareButton()
|
btn = SquareButton()
|
||||||
|
btn.setWordWrap(wordWrap)
|
||||||
btn.setRelSize(KEYCODE_BTN_RATIO)
|
btn.setRelSize(KEYCODE_BTN_RATIO)
|
||||||
btn.setToolTip(Keycode.tooltip(keycode.code))
|
btn.setToolTip(Keycode.tooltip(keycode.code))
|
||||||
btn.clicked.connect(lambda st, k=keycode: self.keycode_changed.emit(k.code))
|
btn.clicked.connect(lambda st, k=keycode: self.keycode_changed.emit(k.code))
|
||||||
|
|
@ -87,13 +91,14 @@ class TabbedKeycodes(QTabWidget):
|
||||||
return buttons
|
return buttons
|
||||||
|
|
||||||
def recreate_keycode_buttons(self):
|
def recreate_keycode_buttons(self):
|
||||||
for btn in self.layer_keycode_buttons + self.macro_keycode_buttons:
|
for btn in self.layer_keycode_buttons + self.macro_keycode_buttons + self.user_keycode_buttons:
|
||||||
self.widgets.remove(btn)
|
self.widgets.remove(btn)
|
||||||
btn.hide()
|
btn.hide()
|
||||||
btn.deleteLater()
|
btn.deleteLater()
|
||||||
self.layer_keycode_buttons = self.create_buttons(self.layout_layers, KEYCODES_LAYERS)
|
self.layer_keycode_buttons = self.create_buttons(self.layout_layers, KEYCODES_LAYERS)
|
||||||
self.macro_keycode_buttons = self.create_buttons(self.layout_macro, KEYCODES_MACRO)
|
self.macro_keycode_buttons = self.create_buttons(self.layout_macro, KEYCODES_MACRO)
|
||||||
self.widgets += self.layer_keycode_buttons + self.macro_keycode_buttons
|
self.user_keycode_buttons = self.create_buttons(self.layout_user, KEYCODES_USER, wordWrap=True)
|
||||||
|
self.widgets += self.layer_keycode_buttons + self.macro_keycode_buttons + self.user_keycode_buttons
|
||||||
self.relabel_buttons()
|
self.relabel_buttons()
|
||||||
|
|
||||||
def set_keymap_override(self, override):
|
def set_keymap_override(self, override):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue