qt 鼠标 键盘 录制 及 回放 代码
时间: 2023-11-06 16:07:18 浏览: 162
鼠标键盘录制
4星 · 用户满意度95%
以下是一个简单的 Qt 鼠标键盘录制及回放的示例代码:
```cpp
#include <QApplication>
#include <QWidget>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QTimer>
#include <QFile>
#include <QDataStream>
class Recorder : public QObject
{
Q_OBJECT
public:
Recorder(QObject *parent = nullptr)
: QObject(parent)
{
connect(qApp, &QApplication::aboutToQuit, this, &Recorder::stopRecording);
}
public slots:
void startRecording(QString filename)
{
if (m_file.isOpen())
{
return;
}
m_file.setFileName(filename);
if (!m_file.open(QIODevice::WriteOnly))
{
return;
}
m_dataStream.setDevice(&m_file);
m_dataStream.setVersion(QDataStream::Qt_5_15);
m_recording = true;
m_startTime = QDateTime::currentDateTime();
connect(qApp, &QApplication::focusChanged, this, &Recorder::onFocusChanged);
connect(qApp, &QApplication::applicationStateChanged, this, &Recorder::onApplicationStateChanged);
connect(qApp, &QApplication::aboutToQuit, this, &Recorder::stopRecording);
m_timer.start(20, this);
}
void stopRecording()
{
if (!m_recording)
{
return;
}
m_recording = false;
m_timer.stop();
disconnect(qApp, &QApplication::focusChanged, this, &Recorder::onFocusChanged);
disconnect(qApp, &QApplication::applicationStateChanged, this, &Recorder::onApplicationStateChanged);
disconnect(qApp, &QApplication::aboutToQuit, this, &Recorder::stopRecording);
m_file.close();
}
void onMouseEvent(QMouseEvent *event)
{
if (m_recording)
{
m_dataStream << qint64(QDateTime::currentDateTime().msecsTo(m_startTime));
m_dataStream << qint32(QEvent::MouseButtonPress + event->button());
m_dataStream << event->pos();
}
}
void onKeyEvent(QKeyEvent *event)
{
if (m_recording)
{
m_dataStream << qint64(QDateTime::currentDateTime().msecsTo(m_startTime));
m_dataStream << qint32(QEvent::KeyPress + event->key());
m_dataStream << qint32(event->modifiers());
}
}
private slots:
void onTimeout()
{
if (qApp->applicationState() == Qt::ApplicationActive)
{
QPoint pos = QCursor::pos();
m_dataStream << qint64(QDateTime::currentDateTime().msecsTo(m_startTime));
m_dataStream << qint32(QEvent::MouseMove);
m_dataStream << pos;
}
}
void onFocusChanged(QWidget *oldWidget, QWidget *newWidget)
{
if (m_recording)
{
m_dataStream << qint64(QDateTime::currentDateTime().msecsTo(m_startTime));
m_dataStream << qint32(QEvent::FocusOut);
m_dataStream << oldWidget;
m_dataStream << qint32(QEvent::FocusIn);
m_dataStream << newWidget;
}
}
void onApplicationStateChanged(Qt::ApplicationState state)
{
if (m_recording)
{
m_dataStream << qint64(QDateTime::currentDateTime().msecsTo(m_startTime));
m_dataStream << qint32(QEvent::ApplicationActivate + state);
}
}
private:
QFile m_file;
QDataStream m_dataStream;
bool m_recording = false;
QDateTime m_startTime;
QTimer m_timer;
};
class Player : public QObject
{
Q_OBJECT
public:
Player(QObject *parent = nullptr)
: QObject(parent)
{
}
public slots:
void play(QString filename)
{
if (m_file.isOpen())
{
return;
}
m_file.setFileName(filename);
if (!m_file.open(QIODevice::ReadOnly))
{
return;
}
m_dataStream.setDevice(&m_file);
m_dataStream.setVersion(QDataStream::Qt_5_15);
qint64 startTime = QDateTime::currentDateTime().toMSecsSinceEpoch();
while (!m_dataStream.atEnd())
{
qint64 timestamp;
m_dataStream >> timestamp;
qint32 eventType;
m_dataStream >> eventType;
switch (eventType)
{
case QEvent::MouseMove:
{
QPoint pos;
m_dataStream >> pos;
int delay = timestamp - startTime;
QTimer::singleShot(delay, this, [pos]() {
QCursor::setPos(pos);
});
break;
}
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
{
int button = eventType - QEvent::MouseButtonPress;
QPoint pos;
m_dataStream >> pos;
int delay = timestamp - startTime;
QTimer::singleShot(delay, this, [button, pos]() {
QMouseEvent event(QEvent::MouseButtonPress + button, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
qApp->sendEvent(qApp->focusWidget(), &event);
});
break;
}
case QEvent::KeyPress:
case QEvent::KeyRelease:
{
int key = eventType - QEvent::KeyPress;
int modifiers;
m_dataStream >> modifiers;
int delay = timestamp - startTime;
QTimer::singleShot(delay, this, [key, modifiers]() {
QKeyEvent event(key == 0 ? QEvent::KeyPress : QEvent::KeyRelease, key, Qt::NoModifier, QString(), false, modifiers);
qApp->sendEvent(qApp->focusWidget(), &event);
});
break;
}
case QEvent::FocusOut:
case QEvent::FocusIn:
{
QWidget *oldWidget, *newWidget;
m_dataStream >> oldWidget >> newWidget;
int delay = timestamp - startTime;
QTimer::singleShot(delay, this, [oldWidget, newWidget]() {
qApp->setActiveWindow(newWidget->window());
qApp->setActiveWindow(newWidget);
qApp->setActiveWindow(newWidget->window());
newWidget->setFocus(Qt::OtherFocusReason);
});
break;
}
case QEvent::ApplicationActivate:
case QEvent::ApplicationDeactivate:
case QEvent::ApplicationStateChange:
{
int state = eventType - QEvent::ApplicationActivate;
int delay = timestamp - startTime;
QTimer::singleShot(delay, this, [state]() {
qApp->setApplicationState(Qt::ApplicationState(state));
});
break;
}
default:
break;
}
startTime = timestamp;
}
m_file.close();
}
private:
QFile m_file;
QDataStream m_dataStream;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
w.show();
Recorder recorder;
Player player;
QTimer::singleShot(1000, [&]() {
recorder.startRecording("record.dat");
});
QTimer::singleShot(5000, [&]() {
player.play("record.dat");
});
return a.exec();
}
#include "main.moc"
```
这个示例代码使用了两个类 `Recorder` 和 `Player` 实现了录制和回放功能。`Recorder` 类用于录制鼠标和键盘事件,`Player` 类用于回放录制的事件。在 `main` 函数中,我们创建了一个 `Recorder` 和一个 `Player` 对象,并分别在程序启动后 1 秒和 5 秒后开始录制和回放。录制的数据保存在 `record.dat` 文件中,回放时从该文件中读取数据。
在 `Recorder` 类中,我们使用 `QDataStream` 将鼠标和键盘事件的时间戳、事件类型和事件数据写入到文件中。在鼠标或键盘事件发生时,我们会记录下事件的时间戳、事件类型和事件数据,然后写入到文件中。在 `stopRecording` 函数中,我们停止录制,并断开与 `QApplication` 的信号连接。
在 `Player` 类中,我们使用 `QDataStream` 从文件中读取鼠标和键盘事件的时间戳、事件类型和事件数据,并使用 `QTimer::singleShot` 来模拟鼠标和键盘事件的发生。在鼠标或键盘事件发生时,我们会计算出事件与上一个事件的时间差,然后使用 `QTimer::singleShot` 在相应的时间后模拟事件的发生。在 `play` 函数中,我们通过循环读取文件中的所有事件,并在每个事件发生的时间点模拟事件的发生。
注意:这个示例代码只是一个简单的实现,没有考虑到很多复杂的情况,例如多线程、多窗口、控件状态等。在实际应用中,需要根据具体情况进行修改和完善。
阅读全文