#include "QtWidgetsApplication2.h"
#include <windows.h>
#include <QDebug>
#include <QVBoxLayout>
QtWidgetsApplication2::QtWidgetsApplication2(QWidget *parent)
: QFrame(parent)
{
ui.setupUi(this);
//init();
}
QtWidgetsApplication2::~QtWidgetsApplication2()
{}
void QtWidgetsApplication2::init()
{
//setFixedSize(800, 600);
QFrame* frame_conetent = new QFrame(this);
frame_conetent->setMinimumSize(1024, 768);
frame_conetent->show();
//frame_conetent->setStyleSheet("background-color:#ff0000;");
QVBoxLayout* layout = new QVBoxLayout();
layout->setMargin(10);
layout->setSpacing(16);
frame_conetent->setLayout(layout);
for (int i = 0; i < 15; i++) {
lbl_list[i] = new QLabel();
lbl_list[i]->setStyleSheet("font:24px bold;");
lbl_list[i]->setText(QString("Label %1").arg(i));
layout->addWidget(lbl_list[i]);
}
lbl_mouse = new QLabel();
lbl_mouse->setStyleSheet("font:24px bold;");
layout->addWidget(lbl_mouse);
lbl_key = new QLabel();
lbl_key->setStyleSheet("font:24px bold;");
layout->addWidget(lbl_key);
m_hWnd = (HWND)this->winId();
RegisterGamePadDevice();
RegisterMouseDevice();
RegisterKeyboardDevice();
}
/*
* 0x01 指针 HID_USAGE_GENERIC_POINTER
0x02 鼠标 HID_USAGE_GENERIC_MOUSE
0x04 游戏杆 HID_USAGE_GENERIC_JOYSTICK
0x05 Game Pad HID_USAGE_GENERIC_GAMEPAD
0x06 键盘 HID_USAGE_GENERIC_KEYBOARD
0x07 键盘 HID_USAGE_GENERIC_KEYPAD
0x08 多轴控制器 HID_USAGE_GENERIC_MULTI_AXIS_CONTROLLER
*/
bool QtWidgetsApplication2::RegisterMouseDevice() {
RAWINPUTDEVICE rid;
rid.usUsagePage = 0x0001;
rid.usUsage = 0x0002; // mouse device
rid.dwFlags = RIDEV_DEVNOTIFY | RIDEV_INPUTSINK;
rid.hwndTarget = m_hWnd;
return ::RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE)) == TRUE;
}
bool QtWidgetsApplication2::RegisterKeyboardDevice() {
RAWINPUTDEVICE rid;
rid.usUsagePage = 0x0001;
rid.usUsage = 0x0006; // keyboard device
rid.dwFlags = RIDEV_DEVNOTIFY | RIDEV_INPUTSINK;
rid.hwndTarget = m_hWnd;
return ::RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE)) == TRUE;
}
//0x05 Game Pad HID_USAGE_GENERIC_GAMEPAD
bool QtWidgetsApplication2::RegisterGamePadDevice()
{
RAWINPUTDEVICE rid;
rid.usUsagePage = 0x0001;
rid.usUsage = 0x0005; // gamepad device
rid.dwFlags = RIDEV_DEVNOTIFY | RIDEV_INPUTSINK;
rid.hwndTarget = m_hWnd;
return ::RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE)) == TRUE;
}
bool QtWidgetsApplication2::nativeEvent(const QByteArray& eventType, void* message, long* result)
{
MSG* msg = static_cast<MSG*>(message);
if (msg->message == WM_INPUT)
{
//qDebug() << "接收到WM_INPUT消息(通过nativeEvent)";
UINT dwSize;
GetRawInputData((HRAWINPUT)msg->lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER));
LPBYTE lpb = new BYTE[dwSize];
if (lpb == NULL)
{
return false;
}
if (GetRawInputData((HRAWINPUT)msg->lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)) != dwSize)
{
delete[] lpb;
return false;
}
RAWINPUT* raw = (RAWINPUT*)lpb;
if (raw->header.dwType == RIM_TYPEMOUSE)
{
//qDebug() << "鼠标原始输入(通过nativeEvent):X=" << raw->data.mouse.lLastX << " Y=" << raw->data.mouse.lLastY;
lbl_mouse->setText(QString("X=%1 Y=%2 ulRawButtons=%3 ulButtons=%4 usFlags=%5 usButtonData=%6")
.arg(raw->data.mouse.lLastX).arg(raw->data.mouse.lLastY)
.arg(raw->data.mouse.ulRawButtons).arg((USHORT)raw->data.mouse.ulButtons)
.arg(raw->data.mouse.usFlags).arg(raw->data.mouse.usButtonData)
);
}
else if (raw->header.dwType == RIM_TYPEKEYBOARD)
{
//qDebug() << "键盘原始输入(通过nativeEvent):虚拟键码=" << raw->data.keyboard.VKey;
lbl_key->setText(QString("VKey=%1 MakeCode=%2 Flags=%3 Message=%4 Reserved=%5 ExtraInformation=%6" )
.arg(raw->data.keyboard.VKey).arg(raw->data.keyboard.MakeCode)
.arg(raw->data.keyboard.Flags).arg(raw->data.keyboard.Message)
.arg(raw->data.keyboard.Reserved).arg(raw->data.keyboard.ExtraInformation)
);
}
if (raw->header.dwType == RIM_TYPEHID)
{
int total_size = raw->data.hid.dwSizeHid * raw->data.hid.dwCount;
for (int i = 0; i < 15; i++)
{
lbl_list[i]->setText(QString("%1:%2").arg(i + 1).arg(raw->data.hid.bRawData[i]));
}
}
delete[] lpb;
}
return QWidget::nativeEvent(eventType, message, result);
}
标签:鼠标,手柄,mouse,raw,arg,测试代码,rid,data,lbl
From: https://www.cnblogs.com/thinkinc999/p/18589550