Unsolved Shortcuts for SysTrayIcon
-
Hi
How do I add shortcuts (e.g. CTRL+SHIFT+C) to an application running exclusively as a SysTrayIcon? The shortcut would call a function currently called through the SysTrayIcon's QMenu. This post https://forum.qt.io/topic/18108/systray-and-keyboard-shortcuts describes a similar use-case, though the answer is not immediately helpful.
Any assistance would be greatly appreciated!
Thanks!
-
Hi and welcome to devnet,
So you would like something like a global shortcut that can be called from anywhere and make your application react ?
-
@SGaist said in Shortcuts for SysTrayIcon:
Hi and welcome to devnet,
So you would like something like a global shortcut that can be called from anywhere and make your application react ?
Hi there, thank you, and yes!
The idea is to have the application sit as a tray icon, and then only react after the user calls a shortcut (or right-click the icon and choose something from the menu).
Thanks!
-
Qt currently does not provide global shortcuts like that. IIRC, there was libQxt that did at the time.
-
@SGaist said in Shortcuts for SysTrayIcon:
Qt currently does not provide global shortcuts like that. IIRC, there was libQxt that did at the time.
Thanks for your response.
It sounds like https://pypi.org/project/PyGlobalShortcut/ does what I need, so I will have a look.
Do you know if there are any plans to expand functionality to support something like this directly in Qt?
-
There's QTBUG-5183 tracking it. I would say: patches welcome :-)
-
@SGaist said in Shortcuts for SysTrayIcon:
There's QTBUG-5183 tracking it. I would say: patches welcome :-)
Hi again
Unfortunately, https://pypi.org/project/PyGlobalShortcut/ does not resolve the issue.
A possible solution is pynput's (https://pynput.readthedocs.io/en/latest/) keyboard which allows something like:
self.hotkey = keyboard.GlobalHotKeys({'<ctrl>+<alt>+h': self.shortcut_function}) self.hotkey.start()
The SysTrayIcon application properly catches this, but it gives the following exception:
QObject: Cannot create children for a parent that is in a different thread.
There are numerous ways in which I can support shortcuts in general, e.g. by using QShortCut and QKeySequence. The issue arises when the application is not in focus; indeed when it is a SysTrayIcon. I have not yet managed to resolve this.
-
The Qxt library was providing something for that. You could make a small library with that and the make python bindings for it.