I have a Qt application where a user can set custom key combinations, e.g.
Zoom in -> ctrl+`+`
Zoom out -> Ctrl+`-`
When pressing the keys on an english US keyboard, the key containing the +
also contains the =
whereas the +
can be retrieved by pressing shift
.
This means when a user presses the keys ctrl
and =/+
then Qt will receive a QKeyEvent that looks like ctrl+=
example code (from How can I capture QKeySequence from QKeyEvent depending on current keyboard layout?)
def analyzekeyPressEvent(self, e):
if e.type() == QEvent.KeyPress:
key = e.key()
if key == Qt.Key_unknown:
print("Unknown key from a macro probably")
return
# the user have clicked just and only the special keys Ctrl, Shift, Alt, Meta.
if(key == Qt.Key_Control or
key == Qt.Key_Shift or
key == Qt.Key_Alt or
key == Qt.Key_Meta):
print("Single click of special key: Ctrl, Shift, Alt or Meta")
print("New KeySequence:", QKeySequence(key).toString(QKeySequence.NativeText))
return
# check for a combination of user clicks
modifiers = e.modifiers()
keyText = e.text()
# if the keyText is empty than it's a special key like F1, F5, ...
print("Pressed Key:", keyText)
if modifiers & Qt.ShiftModifier:
key += Qt.SHIFT
if modifiers & Qt.ControlModifier:
key += Qt.CTRL
if modifiers & Qt.AltModifier:
key += Qt.ALT
if modifiers & Qt.MetaModifier:
key += Qt.META
print("New KeySequence:", QKeySequence(key).toString(QKeySequence.NativeText))
A similar scenario happens when a custom key shortcut is set to shift+1
which when pressed will actually be shift+!
in Qt.
How would I now be able to map the pressed keys to the original defined custom shortcuts
ctr+= -> ctrl+`+`
shift+! -> shift+1