本例记录创建线程读取设备的鼠标键盘事件文件
keylistener.h
#ifndef KEYLISTENER_H
#define KEYLISTENER_H
#include <QObject>
#include <QThread>
#include <QString>
#include <linux/input.h>
#include <fcntl.h>
#include <unistd.h>
#include <QDebug>
#include <QPoint>
//本例中鼠标键盘事件是读取设备描述文件
#define keyDevicePath "/dev/input/event1"
#define mouseDevicePath "/dev/input/event0"
class KeyListener : public QThread {
Q_OBJECT
public:
explicit KeyListener(QObject *parent = nullptr);
~KeyListener();
void run() override; // 重写 QThread 的 run 方法
signals:
void mouseMove(const QPoint &globalPos); // 发送鼠标坐标信号
void enterPressed(int value); // 键盘enter和esc事件
private:
QString keyPath;// 键盘事件驱动路径
QString mouseDevice;// 鼠标事件驱动路径
bool running = true;// 运行标志
};
#endif // KEYLISTENER_H
keylistener.cpp
#include "keylistener.h"
#include <QDebug>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
#include <QPoint>
KeyListener::KeyListener(QObject *parent)
: QThread(parent) {
running = true;
mouseDevice = mouseDevicePath;
keyPath = keyDeviceP