Skip to content

Commit

Permalink
add raw input mode and null key for secondary input
Browse files Browse the repository at this point in the history
see #30
  • Loading branch information
zocker-160 committed May 7, 2022
1 parent 17299be commit 5d09052
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 33 deletions.
66 changes: 57 additions & 9 deletions src/gui/Ui_keypressWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ class Ui_KeyPressWidget(object):
def setupUi(self, KeyPressWidget):
KeyPressWidget.setObjectName("KeyPressWidget")
KeyPressWidget.resize(814, 100)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(KeyPressWidget.sizePolicy().hasHeightForWidth())
KeyPressWidget.setSizePolicy(sizePolicy)
KeyPressWidget.setMinimumSize(QtCore.QSize(0, 100))
KeyPressWidget.setMaximumSize(QtCore.QSize(16777215, 125))
KeyPressWidget.setAutoFillBackground(True)
Expand Down Expand Up @@ -84,8 +79,10 @@ def setupUi(self, KeyPressWidget):
self.customSequenceEdit.setObjectName("customSequenceEdit")
self.horizontalLayout_2.addWidget(self.customSequenceEdit)
self.clearButton2 = QtWidgets.QPushButton(self.customModBox)
self.clearButton2.setFocusPolicy(QtCore.Qt.NoFocus)
icon = QtGui.QIcon.fromTheme("edit-clear")
self.clearButton2.setIcon(icon)
self.clearButton2.setFlat(True)
self.clearButton2.setObjectName("clearButton2")
self.horizontalLayout_2.addWidget(self.clearButton2)
self.verticalLayout_2.addWidget(self.customModBox)
Expand All @@ -94,23 +91,70 @@ def setupUi(self, KeyPressWidget):
self.keyBox.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.keyBox.setFrameShadow(QtWidgets.QFrame.Raised)
self.keyBox.setObjectName("keyBox")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.keyBox)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.verticalLayout = QtWidgets.QVBoxLayout(self.keyBox)
self.verticalLayout.setContentsMargins(4, -1, 4, 1)
self.verticalLayout.setSpacing(1)
self.verticalLayout.setObjectName("verticalLayout")
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.keyBoxInput = QtWidgets.QHBoxLayout()
self.keyBoxInput.setObjectName("keyBoxInput")
self.keySequenceEdit = CKeySequenceEdit(self.keyBox)
self.keySequenceEdit.setMinimumSize(QtCore.QSize(0, 35))
self.keySequenceEdit.setObjectName("keySequenceEdit")
self.horizontalLayout_4.addWidget(self.keySequenceEdit)
self.keyBoxInput.addWidget(self.keySequenceEdit)
self.clearButton = QtWidgets.QPushButton(self.keyBox)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.clearButton.sizePolicy().hasHeightForWidth())
self.clearButton.setSizePolicy(sizePolicy)
self.clearButton.setFocusPolicy(QtCore.Qt.NoFocus)
icon = QtGui.QIcon.fromTheme("edit-clear")
self.clearButton.setIcon(icon)
self.clearButton.setFlat(True)
self.clearButton.setObjectName("clearButton")
self.horizontalLayout_4.addWidget(self.clearButton)
self.keyBoxInput.addWidget(self.clearButton)
self.verticalLayout.addLayout(self.keyBoxInput)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.rawInputDescr = QtWidgets.QLabel(self.keyBox)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.rawInputDescr.sizePolicy().hasHeightForWidth())
self.rawInputDescr.setSizePolicy(sizePolicy)
self.rawInputDescr.setObjectName("rawInputDescr")
self.horizontalLayout_3.addWidget(self.rawInputDescr)
self.rawInputLabel = QtWidgets.QLabel(self.keyBox)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.rawInputLabel.sizePolicy().hasHeightForWidth())
self.rawInputLabel.setSizePolicy(sizePolicy)
self.rawInputLabel.setAlignment(QtCore.Qt.AlignCenter)
self.rawInputLabel.setObjectName("rawInputLabel")
self.horizontalLayout_3.addWidget(self.rawInputLabel)
self.setNullKeyButton = QtWidgets.QPushButton(self.keyBox)
self.setNullKeyButton.setFocusPolicy(QtCore.Qt.NoFocus)
self.setNullKeyButton.setText("")
icon = QtGui.QIcon.fromTheme("application-exit")
self.setNullKeyButton.setIcon(icon)
self.setNullKeyButton.setFlat(True)
self.setNullKeyButton.setObjectName("setNullKeyButton")
self.horizontalLayout_3.addWidget(self.setNullKeyButton)
self.verticalLayout.addLayout(self.horizontalLayout_3)
spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem1)
self.horizontalLayout.addWidget(self.keyBox)
self.moveUp = QtWidgets.QPushButton(KeyPressWidget)
self.moveUp.setFocusPolicy(QtCore.Qt.NoFocus)
icon = QtGui.QIcon.fromTheme("go-up")
self.moveUp.setIcon(icon)
self.moveUp.setObjectName("moveUp")
self.horizontalLayout.addWidget(self.moveUp)
self.moveDown = QtWidgets.QPushButton(KeyPressWidget)
self.moveDown.setFocusPolicy(QtCore.Qt.NoFocus)
icon = QtGui.QIcon.fromTheme("go-down")
self.moveDown.setIcon(icon)
self.moveDown.setObjectName("moveDown")
Expand All @@ -121,6 +165,7 @@ def setupUi(self, KeyPressWidget):
self.line.setObjectName("line")
self.horizontalLayout.addWidget(self.line)
self.delButton = QtWidgets.QPushButton(KeyPressWidget)
self.delButton.setFocusPolicy(QtCore.Qt.NoFocus)
icon = QtGui.QIcon.fromTheme("list-remove")
self.delButton.setIcon(icon)
self.delButton.setObjectName("delButton")
Expand All @@ -145,4 +190,7 @@ def retranslateUi(self, KeyPressWidget):
self.metaMod.setText(_translate("KeyPressWidget", "Meta"))
self.customMod.setText(_translate("KeyPressWidget", "Custom..."))
self.label_2.setText(_translate("KeyPressWidget", "Custom Modifier: "))
self.rawInputDescr.setText(_translate("KeyPressWidget", "raw:"))
self.rawInputLabel.setText(_translate("KeyPressWidget", "--"))
self.setNullKeyButton.setToolTip(_translate("KeyPressWidget", "set NULLKEY"))
from gui.customwidgets import CKeySequenceEdit
59 changes: 52 additions & 7 deletions src/gui/customwidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@

class CKeySequenceEdit(QKeySequenceEdit):

onNewKeySet = pyqtSignal(int)

def __init__(self, parent=None):
super().__init__(parent)

self.rawEnabled = False

self.pressedKeycode = None
self.tmpKeycodes = list()

Expand All @@ -24,11 +28,21 @@ def getUinputKeycode(self) -> int:
def setKeycode(self, uinputKeycode: int) -> None:
# We need to +8 to convert uinput keycode -> X keycode
self.pressedKeycode = uinputKeycode + 8
self.onNewKeySet.emit(self.pressedKeycode)

def setKeySequence(self, keySequence: str):
if keySequence.startswith("0x") and self.rawEnabled:
keySequence = "nullkey"

return super().setKeySequence(keySequence)

def keyPressEvent(self, a0: QKeyEvent) -> None:
#print("keypress:", a0.nativeScanCode())
#print("keypress:", a0.key())

# only one single non modifier is allowed
if len(self.tmpKeycodes) > 0: return
if a0.modifiers():
if a0.modifiers() and not self.rawEnabled:
# for whatever the fuck reason, numpad keys get recognized
# as fucking modifiers (why????)
# so this is a dirty workaround
Expand All @@ -40,6 +54,8 @@ def keyPressEvent(self, a0: QKeyEvent) -> None:
self.pressedKeycode = a0.nativeScanCode()
self.tmpKeycodes.append(a0.key())

self.onNewKeySet.emit(self.pressedKeycode)

return super().keyPressEvent(a0)

def event(self, a0: QEvent) -> bool:
Expand All @@ -55,14 +71,29 @@ def event(self, a0: QEvent) -> bool:
def clear(self):
self.pressedKeycode = None
self.tmpKeycodes.clear()
print("")
return super().clear()

def finalize(self):
self.tmpKeycodes.clear()
#print(self.keySequence().toString())

#print("key:", self.toString())
#print(self.pressedKeycode)

def isSet(self):
return not (self.keySequence().count() == 0 and not self.pressedKeycode)

def setNull(self):
self.setKeySequence("0x0")
self.setKeycode(0)

def toString(self):
string = self.keySequence().toString()

if self.rawEnabled and not string:
return f"0x{self.pressedKeycode}"
else:
return string

class CListWidgetItem(QWidget):

onMoveUp = pyqtSignal(QWidget)
Expand Down Expand Up @@ -106,6 +137,11 @@ def __init__(self, parent=None,

self.setupUi(self)
self.customModBox.setVisible(False)
self.clearButton.pressed.connect(self._resetRawLabel)
self.setNullKeyButton.pressed.connect(self.keySequenceEdit.setNull)

self.keySequenceEdit.onNewKeySet.connect(self._setRawLabel)
self.keySequenceEdit.rawEnabled = True

self.ctrlMod.setChecked(bCtrl)
self.altMod.setChecked(bAlt)
Expand All @@ -123,8 +159,8 @@ def __init__(self, parent=None,
self.keySequenceEdit.setKeycode(key.keycode)

def getData(self) -> ConfigEntry:
# return nothing when there is no actual key
if self.keySequenceEdit.keySequence().count() == 0:
# raise error when there is no actual key
if not self.keySequenceEdit.isSet():
raise ValueError("No key assigned!")

keys = list()
Expand All @@ -147,13 +183,13 @@ def getData(self) -> ConfigEntry:
else:
keys.append( Key(
self.customSequenceEdit.getUinputKeycode(),
self.customSequenceEdit.keySequence().toString()
self.customSequenceEdit.toString()
) )

# finally add the key itself
keys.append( Key(
self.keySequenceEdit.getUinputKeycode(),
self.keySequenceEdit.keySequence().toString()
self.keySequenceEdit.toString()
) )

if len(keys) <= 0: return None
Expand All @@ -162,6 +198,15 @@ def getData(self) -> ConfigEntry:
else:
return Combo(keys)

def _setRawLabel(self, number: int):
if number == 8: # nullkey
number = "nullkey"

self.rawInputLabel.setText(str(number))

def _resetRawLabel(self):
self.rawInputLabel.setText("---")

def __setData(self, data: list):
"""
Expects data in the following form:
Expand Down
Loading

0 comments on commit 5d09052

Please sign in to comment.