Examples of the use of a keyboard to make the detected QT
Scan code of the keyboard, virtual code:
When the user presses a key,
1. The keyboard detects this action, and the scan code (scan code) transmitted to the computer through a keyboard controller;
Keyboard scan codes with specific hardware related, different vendors may have to scan the code with a key difference.
2. After the scan code is received, to which the keyboard driver ;
3. The keyboard driver converts the virtual keyboard scan codes to the code;
Virtual hardware-independent code and specific, different vendors keyboard with a virtual key code is always the same.
3. Then, the driver passes the keyboard scan code and the virtual keyboard operation code and other information to the operating system;
4. Operating system information package obtained in a keyboard message, the keypad and the message into the message queue.
The message from the Windows system, the keyboard is sent to a message window;
6. After the application window where the message is received, can learn information about the operation of the keyboard, then decision in response to certain
Keyboard messages: WM_KEYDOWN
nVirtKey = (int) wParam; // dummy symbols
lKeyData = lParam; // keyboard operation information
WM_KEYDOWN message keyboard second parameter lParam complex
Its role is to specify the number of repetitions, scan code, extended key flag, context code, before the key state flag, and the like transition state flag
lParam: total length of 4 bytes, 32-bit
Bits 0-15: message that specifies the current number of iterations. When the user presses a key for a period of time,
This value is the number of a key is pressed the automatic counting; if the key is pressed long, a plurality of messages is issued,
And the number of repetitions is not cumulative
16-23: Specifies the scan code
24: Specifies whether the key is a key extension (e.g., Alt, Ctrl), if a value of 1 is extended key, otherwise its value is 0
25-28: Reserved
29: Specifies the context code. For WM_KEYDOWN, WM_KEYUP message, which is always zero,
Of WM_CHAR messages, while if the alt key is pressed, the value is 1, otherwise a value of 0.
30: Specifies the current key state. If the message is sent before the key is pressed, a value of 1, otherwise 0
31: Specifies the transition state for the WM_KEYDOWN, whose value is always 0;
For the WM_CHAR, if the key is released, its value is 1, and 0 otherwise.
Here is the code
1. Add the header file
#include <QDialog>
#include <QKeyEvent>
#include <QDebug>
2. Add a function of variable declarations
private:
void keyPressEvent(QKeyEvent* );
void keyReleaseEvent(QKeyEvent *);
//打印按键信息
void printKeyEvent(const QString& strAction,
QKeyEvent*)const;
3. Add the code .cpp
KeyboradDialog::KeyboradDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::KeyboradDialog)
{
ui->setupUi(this);
}
KeyboradDialog::~KeyboradDialog()
{
delete ui;
}
void KeyboradDialog::keyPressEvent(QKeyEvent *event){
printKeyEvent("按下",event);
}
void KeyboradDialog::keyReleaseEvent(QKeyEvent *event){
printKeyEvent("抬起",event);
}
void KeyboradDialog::printKeyEvent(
const QString &strAction,
QKeyEvent *event) const{
qDebug() << strAction;
qDebug() << event->text();
qDebug("按键代码:%d",event->key());
qDebug("扫描键码:%d",event->nativeScanCode());
qDebug("虚拟键码:%d",event->nativeVirtualKey());
qDebug("测试组合按键:");
Qt::KeyboardModifiers mod;
mod = event->modifiers();
if(mod & Qt::ShiftModifier){
qDebug("<Shift>被按下");
}
if(mod & Qt::ControlModifier){
qDebug("<Ctrl>被按下");
}
if(mod & Qt::AltModifier){
qDebug("<Alt>被按下");
}
}
4. Run
Starting G:\QTwork\build-Keyborad-Desktop_Qt_5_6_1_MinGW_32bit-Debug\debug\Keyborad.exe...
"按下"
""
按键代码:16777248
扫描键码:42
虚拟键码:16
测试组合按键:
<Shift>被按下
"抬起"
""
按键代码:16777248
扫描键码:42
虚拟键码:16
测试组合按键:
"按下"
"d"
按键代码:68
扫描键码:32
虚拟键码:68
测试组合按键:
"抬起"
"d"