0

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
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
wasp256
  • 5,943
  • 12
  • 72
  • 119
  • Why don't you use QShortcut? – eyllanesc Nov 21 '21 at 00:21
  • @eyllanesc I have a text editor that captures different keys by overriding `keyEvent(...)`, all examples I've found online used that approach – wasp256 Nov 21 '21 at 00:57
  • see this example: https://stackoverflow.com/questions/57713795/zoom-in-and-out-in-widget/57793707#57793707 – eyllanesc Nov 21 '21 at 01:38
  • 1
    You are implementing something that has already been done, you are complicating yourself unnecessarily – eyllanesc Nov 21 '21 at 01:41
  • The zoom was just an example, I have a dozen other key combinations as well to handle – wasp256 Nov 21 '21 at 02:47
  • You just have to build a QKeySequence with the format you want and pass it to QShortcut, the example linked it so you can see how to use it. – eyllanesc Nov 21 '21 at 02:52
  • So how would I receive a QShortcut object if I have a `QLineEdit` which captures the pressed keys and displays them? E.g. If I'm focused in the line edit and press `ctrl++` on the keyboard then I'd like to display `ctrl++` not `ctrl+=` in the lineedit. AFAIK there's no way to transform a `QKeyEvent` to a `QShortcut` or? – wasp256 Nov 21 '21 at 03:04
  • 1
    https://doc.qt.io/qt-5/qkeysequenceedit.html? – eyllanesc Nov 21 '21 at 03:08
  • Wow had no idea that existed, cheers! – wasp256 Nov 21 '21 at 03:09
  • @eyllanesc would you mind formulating an answer with the comments of yours then I'll mark it as complete – wasp256 Nov 21 '21 at 08:39

0 Answers0