QT 模拟鼠标事件
2017-03-20 15:58
1351 查看
模拟鼠标按键
和模拟键盘按键类似,也是通过发送相应的事件来实现的,安装相应的事件监听器,具体发送事件:
QPoint pos;
pos.setX(88);
pos.setY(58);
QMouseEvent *mEvnPress;
QMouseEvent *mEvnRelease;
mEvnPress = new QMouseEvent(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(QWidget::focusWidget(),mEvnPress);
mEvnRelease = new QMouseEvent(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(QWidget::focusWidget(),mEvnRelease);
主要的分析函数是:
QMouseEvent::QMouseEvent ( Type type, const QPoint & position, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers )
参数分析:
The type parameter must be one of QEvent::MouseButtonPress, QEvent::MouseButtonRelease, QEvent::MouseButtonDblClick, or QEvent::MouseMove.
The position is the mouse cursor's position relative to the receiving widget.
The button that caused the event is given as a value from the Qt::MouseButton enum. If the event type is MouseMove, the appropriate button for this event is Qt::NoButton.
The mouse and keyboard states at the time of the event are specified by buttons and modifiers.
The globalPos() is initialized to QCursor::pos(), which may not be appropriate. Use the other constructor to specify the global position explicitly.
主要说明:
这里的pos的位置是接受鼠标事件的widget的内部的一个局部位置。也就是说他的鼠标按键的产生点是:先通过
QApplication::sendEvent(QWidget::focusWidget(),mEvnPress);//也就是说先是指明了传递给哪个widget
然后再根据mEventPress来进行具体的再改widget中的定位,以及具体的按键是什么。
bool MainWindow::event(QEvent *e)
{
if(QEvent::KeyPress == e->type() )
{
QMouseEvent *mouseEvent = 0;
QPoint pos;
QKeyEvent *event = dynamic_cast<QKeyEvent*>(e);
if(Qt::Key_F5 == event->key())
{
qDebug()<<"simulate mouse left button down";
pos = this->mapFromGlobal(QCursor::pos());
mouseEvent = new QMouseEvent(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::postEvent(this, mouseEvent);
}
else if(Qt::Key_F6 == event->key())
{
qDebug()<<"simulaite mouse right button donw";
pos = this->mapFromGlobal(QCursor::pos());
mouseEvent = new QMouseEvent(QEvent::MouseButtonPress, pos, Qt::RightButton, Qt::RightButton, Qt::NoModifier);
QApplication::postEvent(this, mouseEvent);
}
}
return QFrame::event(e);
}
和模拟键盘按键类似,也是通过发送相应的事件来实现的,安装相应的事件监听器,具体发送事件:
QPoint pos;
pos.setX(88);
pos.setY(58);
QMouseEvent *mEvnPress;
QMouseEvent *mEvnRelease;
mEvnPress = new QMouseEvent(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(QWidget::focusWidget(),mEvnPress);
mEvnRelease = new QMouseEvent(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(QWidget::focusWidget(),mEvnRelease);
主要的分析函数是:
QMouseEvent::QMouseEvent ( Type type, const QPoint & position, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers )
参数分析:
The type parameter must be one of QEvent::MouseButtonPress, QEvent::MouseButtonRelease, QEvent::MouseButtonDblClick, or QEvent::MouseMove.
The position is the mouse cursor's position relative to the receiving widget.
The button that caused the event is given as a value from the Qt::MouseButton enum. If the event type is MouseMove, the appropriate button for this event is Qt::NoButton.
The mouse and keyboard states at the time of the event are specified by buttons and modifiers.
The globalPos() is initialized to QCursor::pos(), which may not be appropriate. Use the other constructor to specify the global position explicitly.
主要说明:
这里的pos的位置是接受鼠标事件的widget的内部的一个局部位置。也就是说他的鼠标按键的产生点是:先通过
QApplication::sendEvent(QWidget::focusWidget(),mEvnPress);//也就是说先是指明了传递给哪个widget
然后再根据mEventPress来进行具体的再改widget中的定位,以及具体的按键是什么。
bool MainWindow::event(QEvent *e)
{
if(QEvent::KeyPress == e->type() )
{
QMouseEvent *mouseEvent = 0;
QPoint pos;
QKeyEvent *event = dynamic_cast<QKeyEvent*>(e);
if(Qt::Key_F5 == event->key())
{
qDebug()<<"simulate mouse left button down";
pos = this->mapFromGlobal(QCursor::pos());
mouseEvent = new QMouseEvent(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::postEvent(this, mouseEvent);
}
else if(Qt::Key_F6 == event->key())
{
qDebug()<<"simulaite mouse right button donw";
pos = this->mapFromGlobal(QCursor::pos());
mouseEvent = new QMouseEvent(QEvent::MouseButtonPress, pos, Qt::RightButton, Qt::RightButton, Qt::NoModifier);
QApplication::postEvent(this, mouseEvent);
}
}
return QFrame::event(e);
}
相关文章推荐
- Qt模拟鼠标按键事件
- QT平台上模拟鼠标事件案例
- Qt for Mac模拟鼠标以及键盘事件
- QT 模拟鼠标事件
- QT中模拟鼠标点击事件
- Qt使用Windows Api模拟鼠标单击双击滚轮事件
- linux 模拟鼠标事件 qt
- QT 模拟鼠标事件
- QT添加托盘以及SendInput模拟鼠标键盘用法
- Qt 学习(8)Qt Creator中鼠标键盘事件的处理实现自定义鼠标指针(转)
- 通过鼠标事件来看QT/Embedded 窗体事件是如何派发的
- 【转】C#模拟键盘鼠标事件
- Android自动化测试初探(五): 再述模拟键盘鼠标事件(adb shell 实现)
- 模拟键盘鼠标事件----自己做外挂
- 使用SWT模拟鼠标键盘事件
- VC模拟按键或鼠标事件
- JavaScript中通过鼠标事件实现模拟拖放效果
- 使用SWT模拟鼠标键盘事件
- Android自动化测试初探(四): 模拟键盘鼠标事件(Socket+Instrumentation实现)
- 模拟鼠标键盘事件