Qt软键盘-发送按键事件

发送按键事件到当前聚焦的窗体。

1.获取当前聚焦的QWidget

1
2
QWidget *receiver = QApplication::focusWidget();
qDebug()<<"Send key event to focus widget "<<receiver->objectName();

2.装载press与release事件

  • key为按键键值(整型);
  • value为按键实际值(QString).
    1
    2
    QKeyEvent keyPress(QEvent::KeyPress,     key, Qt::NoModifier, value);
    QKeyEvent keyRelease(QEvent::KeyRelease, key, Qt::NoModifier, value);

3.发送按键事件

1
2
QApplication::sendEvent(receiver, &keyPress);
QApplication::sendEvent(receiver, &keyRelease);