发送按键事件到当前聚焦的窗体。
1.获取当前聚焦的QWidget
代码语言:javascript复制QWidget *receiver = QApplication::focusWidget();
qDebug()<<"Send key event to focus widget "<<receiver->objectName();
2.装载press与release事件
- key为按键键值(整型);
- value为按键实际值(QString).
QKeyEvent keyPress(QEvent::KeyPress, key, Qt::NoModifier, value);
QKeyEvent keyRelease(QEvent::KeyRelease, key, Qt::NoModifier, value);
3.发送按键事件
代码语言:javascript复制QApplication::sendEvent(receiver, &keyPress);
QApplication::sendEvent(receiver, &keyRelease);