• Stars
    star
    549
  • Rank 80,438 (Top 2 %)
  • Language
    C++
  • License
    BSD 3-Clause "New...
  • Created over 8 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A global shortcut/hotkey for Desktop Qt-Applications

QHotkey

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.

Features

  • Works on Windows, Mac and X11
  • Easy to use, can use QKeySequence for easy shortcut input
  • Supports almost all common keys (Depends on OS & Keyboard-Layout)
  • Allows direct input of Key/Modifier-Combinations
  • Supports multiple QHotkey-instances for the same shortcut (with optimisations)
  • Thread-Safe - Can be used on all threads (See section Thread safety)
  • Allows usage of native keycodes and modifiers, if needed

Note: For now Wayland is not supported, as it is simply not possible to register a global shortcut with wayland. For more details, or possible Ideas on how to get Hotkeys working on wayland, see Issue #14.

Building

QHotkey supports both Qt6 and Qt5. When using Qt6, version 6.2.0 or later required. It can be built by either the CMake or qmake building systems.

CMake

The CMake QT_DEFAULT_MAJOR_VERSION variable controls which major version of Qt is used for building, and defaults to 5. For example, use the CMake command line option -DQT_DEFAULT_MAJOR_VERSION=6 for building with Qt6. To build the testing application QHotkeyTest, specify -DQHOTKEY_EXAMPLES=ON. CMake example usage:

$ cd QHotkey
$ cmake -B build -S . -DQT_DEFAULT_MAJOR_VERSION=6
$ cmake --build build
# cmake --install build

qmake

The major version of Qt is chosen by the qmake invocation itself, as the qmake executable is tied to a specific Qt version. The executable name can vary between operating systems. qmake example usage:

$ cd QHotkey
$ qmake
$ make
# make install

Installation

The package is providet as qpm package, de.skycoder42.qhotkey. You can install it either via qpmx (preferred) or directly via qpm.

Via qpmx

qpmx is a frontend for qpm (and other tools) with additional features, and is the preferred way to install packages. To use it:

  1. Install qpmx (See GitHub - Installation)
  2. Install qpm (See GitHub - Installing, for windows see below)
  3. In your projects root directory, run qpmx install de.skycoder42.qhotkey

Via qpm

  1. Install qpm (See GitHub - Installing, for windows see below)
  2. In your projects root directory, run qpm install de.skycoder42.qhotkey
  3. Include qpm to your project by adding include(vendor/vendor.pri) to your .pro file

Check their GitHub - Usage for App Developers to learn more about qpm.

Important for Windows users: QPM Version 0.10.0 (the one you can download on the website) is currently broken on windows! It's already fixed in master, but not released yet. Until a newer versions gets released, you can download the latest dev build from here:

Usage

The general usage is to create QHotkey instances for specific hotkeys, register them and then simply connect to the signal emitted once the hotkey is pressed.

Example

The following example shows a simple application that will run without a window in the background until you press the key-combination Ctrl+Alt+Q (⌘+⌥+Q on Mac). This will quit the application. The debug output will tell if the hotkey was successfully registered and that it was pressed.

#include <QHotkey>
#include <QApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
	QApplication app(argc, argv);

	QHotkey hotkey(QKeySequence("Ctrl+Alt+Q"), true, &app); //The hotkey will be automatically registered
	qDebug() << "Is segistered:" << hotkey.isRegistered();

	QObject::connect(&hotkey, &QHotkey::activated, qApp, [&](){
		qDebug() << "Hotkey Activated - the application will quit now";
		qApp->quit();
	});

	return app.exec();
}

Note: You need the .pri include for this to work.

Testing

By running the example in ./HotkeyTest you can test out the QHotkey class. There are 4 sections:

  • Playground: You can enter some sequences here and try it out with different key combinations.
  • Testings: A list of selected hotkeys. Activate it and try out which ones work for you (Hint: Depending on OS and keyboard layout, it's very possible that a few don't work).
  • Threading: Activate the checkbox to move 2 Hotkeys of the playground to separate threads. It should work without a difference.
  • Native Shortcut: Allows you to try out the direct usage of native shortcuts

Logging

By default, QHotkey prints some warning messages if something goes wrong (For example, a key that cannot be translated). All messages of QHotkey are grouped into the QLoggingCategory "QHotkey". If you want to simply disable the logging, call the following function somewhere in your code:

QLoggingCategory::setFilterRules(QStringLiteral("QHotkey.warning=false"));

This will turn all warnings of QHotkey of (It only uses warnings for now, that's why this is enough). For more information about all the things you can do with the logging categories, check the Qt-Documentation

Thread safety

The QHotkey class itself is reentrant - which means you can create as many instances as required on any thread. This allows you to use the QHotkey on all threads. But you should never use the QHotkey instance on a thread that is different from the one the instance belongs to! Internally the system uses a singleton instance that handles the hotkey events and distributes them to the QHotkey instances. This internal class is completely threadsafe.

However, this singleton instance only runs on the main thread. (One reason is that some of the OS-Functions are not thread safe). To make threaded hotkeys possible, the critical functions (registering/unregistering hotkeys and keytranslation) are all run on the mainthread too. The QHotkey instances on other threads use QMetaObject::invokeMethod with a Qt::BlockingQueuedConnection.

For you this means: QHotkey instances on other threads than the main thread may take a little longer to register/unregister/translate hotkeys, because they have to wait for the main thread to do this for them. Important: there is however, one additional limitation that comes with that feature: QHotkey instances on other threads but the main thread must be unregistered or destroyed before the main eventloop ends. Otherwise, your application will hangup on destruction of the hotkey. This limitation does not apply for instances on the main thread. Furthermore, the same happens if you change the shortcut or register/unregister before the loop started, until it actually starts.

Documentation

The documentation is available as release and on github pages.

The documentation was created using doxygen. It includes an HTML-documentation and Qt-Help files that can be included into QtCreator (QtAssistant) to show F1-Help (See Adding External Documentation for more details).

Technical

Requirements

  • Explicit support is only given down to the latest Qt LTS, but may work with earlier versions, too
  • At least the QtGui-Module (a QGuiApplication). Hotkeys on console based applications are not supported (By the operating systems). You can however create a gui application without any visible window.
  • C++11

Known Limitations

  • Only single key/modifier combinations are possible. If using QKeySequence, only the first key+modifier of the sequence will be used.
  • Qt::Key makes no difference between normal numbers and the Numpad numbers. Most keyboards however require this. Thus, you can't register shortcuts for the numpad, unless you use a native shortcut.
  • Supports not all keys, but most of the common ones. There are differences between platforms and it depends on the Keyboard-Layout. "Delete", for example, works on windows and mac, but not on X11 (At least on my test machines). I tried to use OS-Functions where possible, but since the Qt::Key values need to be converted into native keys, there are some limitations. I can use need such a key, try using the native shortcut.
  • The registered keys will be "taken" by QHotkey. This means after a hotkey was cosumend by your application, it will not be sent to the active application. This is done this way by the operating systems and cannot be changed.
  • If you get a QHotkey: Failed to register hotkey. Error: BadAccess (attempt to access private resource denied) error on X11, this means you are trying to register a hotkey that is private to X11. Those keys simply cannot be registered using the normal API

More Repositories

1

QtAutoUpdater

A Qt library to automatically check for updates and install them
C++
753
star
2

QtMvvm

A mvvm oriented library for Qt, to create Projects for Widgets and Quick in parallel
C++
332
star
3

QtService

A platform independent library to easily create system services and use some of their features
C++
154
star
4

QtJsonSerializer

A library to perform generic seralization and deserialization of QObjects from and to JSON and CBOR
C++
143
star
5

QtApng

An apng image plugin for Qt to support animated PNGs
C++
102
star
6

QtRestClient

A library for generic JSON-based REST-APIs, with a mechanism to map JSON to Qt objects
C++
88
star
7

QtDataSync

A simple offline-first synchronisation framework, to synchronize data of Qt applications between devices
C++
87
star
8

QtIFW-Advanced-Setup

Create "Qt Installer Framework" installers from your project via qmake
JavaScript
58
star
9

QTaskbarControl

A class to create a taskbar/launcher progress and more, for all desktop platforms
C++
43
star
10

libsodium_dart_bindings

Dart bindings for libsodium, supporting both the VM and JS without flutter dependencies.
Dart
28
star
11

AndroidUtils

Utils for easy c++ and qml integration of common android features
C++
23
star
12

repkg

A tool to manage rebuilding of AUR packages based on their dependencies
C++
19
star
13

QAutoStart

A class to easily register your application as autostart application
C++
19
star
14

qdep

A very basic yet simple to use dependency management tool for qmake based projects
Python
18
star
15

qpmx

A frontend for qpm, to provide source and build caching
C++
16
star
16

QSslServer

An extension of QTcpServer to support ssl. The counterpart to QSslSocket
C++
16
star
17

QuickExtras

Additional Stuff for Qt Quick Controls 2, to make it work event better
QML
14
star
18

KeepassTransfer

A collection of tools to securely transfer passwords to your browser
C++
14
star
19

firebase_auth_rest

A platform independent Dart/Flutter Wrapper for the Firebase Authentication API based on REST
Dart
14
star
20

QSingleInstance

A simple single instance application for Qt
C++
13
star
21

QPluginFactory

A factory class to easily load generic plugins
C++
12
star
22

QPathEdit

A Qt-Widget to get local file and folder-paths in an optimized and simple way
C++
12
star
23

QTinyAes

A Qt-Wrapper for the AES-implementation kokke/tiny-AES128-C
C++
11
star
24

QSettingsDialog

Has been replaced by https://github.com/Skycoder42/QtMvvm
C++
11
star
25

qsshfs

A gui wrapper around sshfs
C++
10
star
26

QtUtils

A collection of various Qt-Classes, branch-sorted
C++
10
star
27

QObjectListModel

A Collection of classes to easily create models with QObject and Q_GADGET classes for widgets and quick
C++
10
star
28

qt-json

A basic package for simple json serialization between json and qt classes
C++
10
star
29

QtBackgroundProcess

A Library to create background applications with simple, automated foreground control
C++
10
star
30

dolphin-seafile-plugin

A plugin for dolphin to show the seafile synchronization status
C++
9
star
31

QConsole

A non-blocking Qt Style console class to easily handle stdin/out/err
C++
9
star
32

dart_pre_commit

A small collection of pre commit hooks to format and lint dart code
Dart
9
star
33

QtModules

A collection of ci and deployment scripts to create and deploy Qt modules with travis/appveyor
Python
8
star
34

Syrem

A simple reminder application for desktop and mobile, with synchronized reminders
C++
8
star
35

action-setup-qt

A Github workflow action to install Qt from their online installer
JavaScript
7
star
36

QUrlValidator

A class that provides a QValidator to validate urls
C++
6
star
37

QSettingsGenerator

A qmake extension to generate a C++ settings wrapper for easier, stringless access
Python
5
star
38

keepassxc-client-library

A C++ library to access the browser-plugin-API of KeePassXC to retrieve or create entries.
C++
5
star
39

DialogMaster

A utility to create optimized dialogs with Qt
C++
4
star
40

QCliParser

An extension of QCommandlineParser to make context based CLIs easier
C++
4
star
41

QtRemoteObjectsSslBackend

A backend for QtRemoteObjects to connect via an encrypted SSL connection
C++
4
star
42

cryptopp-qt

A collection of pro/pri files to build and integrate Crypto++ via qmake
QMake
4
star
43

IconThemeBrowser

A small application to browse Qt icon themes
C++
4
star
44

QCtrlSignals

A library to catch and handle windows and unix signals, in a cross platform manner
C++
4
star
45

AndroidContentDevice

A QIODevice implementation to be able to open "content://" urls on android
C++
4
star
46

etebase-dart

Dart/Flutter bindings for libetebase
Dart
3
star
47

QGenericTree

A generic tree data structure for Qt
C++
3
star
48

QIpcPointer

A smart pointer for Qt to create any type on managed shared memory
C++
3
star
49

ApngProject

A collection of projects, all around making apng available in Qt
C++
3
star
50

AniRem

A tool to passivly check for updates on seasons, for proxer.me
C++
3
star
51

flutter_data_extensions

A collection of packages that extend functionally of flutter_data. Namely firebase, encryption and sync.
Dart
3
star
52

openssl-android

qdep package to compile openssl for android and add it to your app
Prolog
3
star
53

qtrng

Qt Random number generator wrapper around "secure" os RNGS
C++
2
star
54

QPipeDevice

A collection of QIODevice classes used for "piping" things between devices
C++
2
star
55

QtProFileParser

A small library to parse Qt Project Files and provide easy handling of those
C++
2
star
56

PWatcher

C++
1
star
57

ClipboardSync

A small tool for clipboard synchronization
C++
1
star
58

action-dart-release

An action to create a github release from a dart package
TypeScript
1
star
59

action-deploy-qt

A Github workflow action to upload qt modules to a server via sshfs
JavaScript
1
star
60

UniInsert

An application to easily insert unicode characters EVERYWHERE
C++
1
star
61

Conflip

A tool to synchronize settings/configurations across multiple machines
C++
1
star
62

qt-rest

A basic package for simple, generic, builder-based asynchronous rest calls
C++
1
star
63

MangaConvert

Download and convert mangas from single images to pdf
C++
1
star
64

deployment

Meta-Repo containing various scripts and files fo generic deployment
QMake
1
star
65

improved-initiative-interceptor

Dart
1
star
66

settings_builder

A dart builder that generates automatic accessors for reading and writing shared preferences.
Dart
1
star
67

dart_test_tools

Additional tools and helpers for writing dart unit tests
Dart
1
star
68

image_galery

Dart
1
star
69

QtMvvmQuick

Has been replaced by https://github.com/Skycoder42/QtMvvm
C++
1
star
70

paxchange

Simple dart script to passively synchronize installed pacman packages between systems
Dart
1
star
71

QtModules-LTS

Build-Repo to build my Qt modules as LTS independent of the primary modules
Shell
1
star
72

QColorEdit

A simple widget to inline-edit colors
C++
1
star
73

QtMvvmWidgets

Has been replaced by https://github.com/Skycoder42/QtMvvm
C++
1
star
74

QtMvvmSettingsWidgets

Has been replaced by https://github.com/Skycoder42/QtMvvm
C++
1
star
75

QtMvvmCore

Has been replaced by https://github.com/Skycoder42/QtMvvm
C++
1
star
76

QExceptionStream

A wrapper around QDebug that allows you to create exception messages like you would write to QDebug
C++
1
star
77

QXmlCodeGen

A simple python script to generate C++ bindings from an XML Schema definition, with support for bootstrapped Qt
Python
1
star
78

aha-dart

Dart client for the AVM Home Automation HTTP Interface + Login
Dart
1
star