Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-112381

Mac: Manually posted key events no longer trigger shortcuts

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 6.5.0 RC
    • QPA
    • None
    • macOS 13.2.1, Xcode 14.2, ARM (M1)
    • macOS

    Description

      When manually posting key events through QWindowSystemInterface::handleKeyEvent() shortcuts on macOS are no longer triggered in Qt 6.5.0 RC. Still works fine when using Qt 6.4.3.

      Manual posting of key events is done by Squish for replaying key events during UI tests of Qt applications.

      I attached an example project (shortcut.zip), where there is a text field showing a counter that increases on the shortcut Ctrl+B (or rather Cmd+B on macOS). Pressing the key combination on a physical keyboard works fine in the two Qt versions mentioned above.

      import QtQuick
      import QtQuick.Controls
      
      Window {
          visible: true
          title: "Shortcut"
          id: view
          property int seenShortcuts
          Shortcut {
              sequence: "Ctrl+B"
              onActivated: view.seenShortcuts++
          }
          Text {
              text: view.seenShortcuts
          }
          Button {
              text: "Inject Shortcut"
              onClicked: injector.injectShortcut(view)
          }
      }
      

      There is also a button that programmatically posts the key events, as Squish currently would. This works on Linux and Windows in Qt 6.4.3 and Qt 6.5.0 RC and still works on macOS in Qt 6.4.3 but stops working on macOS in Qt 6.5.0 RC.

      #if QT_VERSION == QT_VERSION_CHECK(6, 5, 0)
      #include <6.5.0/QtGui/qpa/qwindowsysteminterface.h>
      #elif QT_VERSION == QT_VERSION_CHECK(6, 4, 0)
      #include <6.4.0/QtGui/qpa/qwindowsysteminterface.h>
      #elif QT_VERSION == QT_VERSION_CHECK(6, 4, 3)
      #include <6.4.3/QtGui/qpa/qwindowsysteminterface.h>
      #else
      #error Missing include
      #endif
      
      static bool postKeyEvent(QWindow *window, const QEvent::Type eventType, const Qt::Key key, const Qt::KeyboardModifiers modifiers, const QString &text)
      {
          const bool result = QWindowSystemInterface::handleKeyEvent<QWindowSystemInterface::AsynchronousDelivery>(window, eventType, (int)key, modifiers, text, false, 1);
          qDebug() << eventType << key << modifiers << text << (result ? "SUCCESS" : "FAILURE");
          return result;
      }
      
      void ShortcutInjector::injectShortcut(QWindow *window)
      {
          qDebug() << "Injecting <Ctrl+B>";
          postKeyEvent(window, QEvent::KeyPress, Qt::Key_Control, QFlags<Qt::KeyboardModifier>(Qt::KeyboardModifier::ControlModifier), "");
          postKeyEvent(window, QEvent::KeyPress, Qt::Key_B, QFlags<Qt::KeyboardModifier>(Qt::KeyboardModifier::ControlModifier), "");
          postKeyEvent(window, QEvent::KeyRelease, Qt::Key_B, QFlags<Qt::KeyboardModifier>(Qt::KeyboardModifier::ControlModifier), "");
          postKeyEvent(window, QEvent::KeyRelease, Qt::Key_Control, QFlags<Qt::KeyboardModifier>(Qt::KeyboardModifier::NoModifier), "");
      }
      

      Note that SUCCESS and FAILURE are just the return value of the QWindowSystemInterface::handleKeyEvent() function and should be SUCCESS independent of the reported issue. The issue can be confirmed, if after clicking on Inject Shortcut the text still reads 0.

      Attachments

        Activity

          People

            vestbo Tor Arne Vestbø
            blochberger.max Maximilian Blochberger
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: