QKeySequence very different from the sequence of hotkeys in WinAPI . QKeySequence actually is a string in which the separator hotkeys are "+" sign, as well as the sequence ", " , if several sequences. Then, as in WinAPI there are modifiers Alt, Ctrl, Shift, which are represented by an unsigned integer type, and hot key, which is represented by the code keypad.
Let us research the option, when there is one sequence in QKeySequence and it is necessary to register as a global hotkey in WinAPI. To this end, we agree that QKeySequence as as a modifier will have only the Ctrl, Alt, Shift , and as hot keys one of the letters of the English alphabet.
Next to parse QKeySequence write two functions:
- unsigned int winKeyMod(QKeySequence) - which will return an integer combination of modifiers;
- char winHotKey(QKetSequence) -which will return the code of the hot key.
Modifiers Alt, Ctrl and Shift option in the bit values have 0b00000001 , 0b00000010 and 0b00000100 respectively, so the logical OR any number of data addition will give the required number of combinations. Hence it is necessary in the method winKeyMod , identify modifiers of the sequence and if any one of the modifier to be added that the method returns.
A hot winHotKey return code in a variable char key. A key code is allocated as UNICODE code.
In an article about global hotkeys in WinAPI already commented that the treatment is performed in nativeEvent method, so we will not proceed with the matter and proceed directly to the code listing.
Parsing QKeySequence
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QKeySequence> #include <windows.h> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); protected: bool nativeEvent(const QByteArray &eventType, void *message, long *result); private: Ui::MainWindow *ui; unsigned int winKeyMod(QKeySequence sequence); char winHotKey(QKeySequence sequence); }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // We define a sequence of keyboard shortcuts QKeySequence keySequence(Qt::CTRL + Qt::ALT + Qt::SHIFT + 'A'); /* Unregister sequence Id 100