Global hotkeys in a cross-platform Qt application
Asked Answered
G

1

19

I'm creating a cross platform utility, in C++ using Qt, for which I need to have shortcut keys (or hotkeys, not really sure about the difference). Essentially the application will run and only be visible as an icon in the system tray, and do stuff when you press certain shortcut keys (eg, Ctrl-Shift-f4 or something).

I am under the impression that Qt doesn't provide a way to handle shortcut keys unless the application is in focus, which, in my case it won't be. So, that's out (if however that is a viable option, please clue me in).

I've found plenty of examples/documentation explaining how to do this using Xlib/Xcb for linux, win32 api for windows, and carbon for osx, but I'm having a hard time finding a way to do this that would be applicable within the scope of a Qt application.

What would be a way to accomplish what I need?

Grandma answered 5/8, 2012 at 3:51 Comment(2)
Have a look at github.com/hluk/qxtglobalshortcutRabassa
I'm using qxtglobalshortcut for windows and mac os and all works fineCytoplast
B
2

I'm digging up this old non-answered question because, using QML, I encountered the same issue. The Shortcut QML type allow you to specify a context property but you still need a focused application or window.

However, I found a library resolving this issue : QHotkey. Describing itself on Github as :

A global shortcut/hotkey for Desktop Qt-Applications.

The QHotkey is a class that can be used to create hotkeys/global shortcuts, aka shortcuts that work everywhere, independent of the application state. This means your application can be active, inactive, minimized or not visible at all and still receive the shortcuts.

QHockey is available as a package through qpm and can be used directly from C++.

Backman answered 26/2, 2020 at 12:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.