QML hotkeys interfere with Key OnPressed events

I have a problem with my QML program. In the top level main.qml file I have keyboard shortcuts like this:

Shortcut {
  sequence: "Up"
  onActivated: {
    // yadda yadda
  }
}

      

In another file, I have multiple calls to Keys.onPressed, for example:

Keys.onPressed: {
  if (event.key == Qt.Key_Up) {
    // yadda yadda
  }
}

      

Apparently the shortcuts prevent calls to OnPressed. When I comment out the shortcuts, OnPressed works fine. But when they are both active, the Shortcut seems to intercept the keyboard press and prevent OnPressed from firing.

I know that with mouse events, Qt has an "accepted" variable. If you want the event to continue propagating down the stack, you can simply set "accept = false" in the OnActivated function to accomplish this. However, I don't see an equivalent "accepted" variable in the shortcut API. Is there any other way to ensure that the event propagates correctly?

+3


source to share


1 answer


To act as a shortcut, Shortcut

must take precedence over key handlers on active focus objects. Otherwise it won't be any different from a normal key handler. However, sometimes it is necessary to override the shortcut, as you do.

In Qt 5.8 and earlier, you can disable a Shortcut

to prevent it from handling shortcut events under certain conditions. For example:

Shortcut {
    enabled: !someItem.activeFocus
}

      



In Qt 5.9, a more efficient mechanism was introduced for this. Focus active objects with key handlers can now override shortcuts by accepting shortcut override events using Keys.shortcutOverride

. For example:

Item {
    focus: true
    Keys.onShortcutOverride: {
        if (event.key == Qt.Key_Up)
            event.accepted = true
    }
    Keys.onUpPressed: ...
}

      

+2


source







All Articles