1

I currently have this, which functions as expected, running on keyboard button press.

# QWidget == widget shortcut is assigned to

shortcut = QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_M), QWidget)
shortcut.setContext(QtCore.Qt.ApplicationShortcut)
shortcut.activated.connect(self.script_to_run)

What I want is for this to instead run on keyboard button release.

Reason being, I want to run one script when the button is pressed and then a separate script when the same button is released.

I have tried a few different things, keyReleaseEvent(), and eventFilter(), but I think by using QShortcut these events become suppressed.

Adam Sirrelle
  • 357
  • 8
  • 18
  • Have you tried event-filtering `keyPressEvent` and `keyReleaseEvent` without setting `QShortcut`? – mugiseyebrows May 21 '21 at 11:10
  • Exactly *which* keyboard button release are you talking about? For an ApplicationShortcut, any widget could currently have the keyboard-focus and there's nothing to stop the user pressing and releasing other keys whilst the shortcut is still pressed. The user could also use the mouse to shift the focus to another widget whilst the shortcut is still pressed. You could install an event-filter on the QApplication to listen for all key-releases - but it will be tricky to link a particular key-release to a previous shortcut-press. – ekhumoro May 21 '21 at 11:15
  • @mugiseyebrows I want to use the QShortcut, because I am setting this up to work in Maya. Using QShortcut allows you to set shortcuts which prevent Maya's native hotkeys from firing regardless of which widget is selected. Sadly this does mean that the keyEvents won't work as they seem to get overloaded by the QShortcut. – Adam Sirrelle May 24 '21 at 01:18
  • @ekhumoro I feel that using the QShortcut to overload all inputs within my main widget is a cleaner way to go. The only problem is getting the on release to fire. I'm thinking I could use a double tap approach, if it comes to it, but if I can figure out an on release solution that would be better. I have so far resisted using an EventFilter as I have read this can cause some slow down, and it would require me to pass it into multiple widgets, where as the QShortcut only needs to be passed in to the one. – Adam Sirrelle May 24 '21 at 01:23
  • @AdamSirrelle When a shortcut is activated, it produces [Shortcut and ShortcutOverride events](https://doc.qt.io/qt-5/qevent.html#Type-enum), which are exclusively triggered by *key-presses*. If you set an event-filter on the QApplication, you will see that *key-release* events also still occur, but there is no property that tells you whether they are associated with a shortcut (let alone which one). Qt simply *does not keep track of that information*, so you will have to do it yourself. Thus, event-filtering of one kind or another is inescapable if you want to implement this functionality. – ekhumoro May 24 '21 at 12:45
  • @ekhumoro, ah yes. I think it was Maya specifically that wasn't returning the keyReleaseEvent. I have set up a couple eventFilters to catch the keypressrelease on my two widgets; it installs the event filter when the key is pressed (which then stores the script to run on release), and removes it on release. This seems to work well enough for now, but let me know if you have any thoughts. – Adam Sirrelle May 25 '21 at 01:43
  • @AdamSirrelle It sounds like that will lose the capability to have application-level shortcuts, since a specific widget would have to keep the keyboard-focus during the press/release events - in which case, the QShortcut becomes redundant. – ekhumoro May 25 '21 at 10:47

0 Answers0