Question

I am looking to create a shortcut for my application that will still look for input while my application does not have focus. QShortcut and QApplicationShortcut don't seem to do what I need.

This is what I have. I would like to be able to trigger self.onplaybutton() while a different application has focus.

self.shortcut = QtGui.QShortcut(self.mainWindow)
self.shortcut.setKey('A')
self.shortcut.activated.connect(lambda: self.onplaybutton())
Was it helpful?

Solution

At the moment, Qt doesn't provide an API for global shortcuts, so you can't do this directly with either PySide or PyQt.

Instead, you will have to use an external library, and the only thing available seems to be libqxt, which has a QxtGlobalShortcut class.

Fortunately, the pygs project provides a PyQt bindings for the QxtGlobalShortcut class, so that provides at least way to get what you want.


UPDATE:

For PyQt5 support, there's PyQxtGlobalShortcut, which is a fork of pygs. This project hasn't been updated since 2016, but libqxt still seems to be actively maintained, so it probably could be brought up to date without too much difficulty. (PS: in fact, there's a more recent fork here).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top