[Solved] Can not generate Cursor KeyEvent

  • Hi

    I am generating KeyEvents with this code:
    @QWidget *ActWidget = QApplication::focusWidget();
    { datastring.append(inData->mid(uNextPara.l += 1, uLenNextPara.c[0]));
    QKeyEvent keyPress(QEvent::KeyPress, datastring.toULong(), Qt::NoModifier);
    QApplication::sendEvent(ActWidget, &keyPress);
    QKeyEvent keyRelease(QEvent::KeyRelease, datastring.toULong(), Qt::NoModifier);
    QApplication::sendEvent(ActWidget, &keyRelease); }
    This works perfect for Tab, Enter ... but it does not work for the curser keys Up, Down, Left and Right.
    I tried with different modifiers, to no avail.

    I read out the KeyCode used by an attached Keyboard with this:
    @case QEvent::KeyPress:
    { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    qDebug("Key %d pressed, Event type: %d", keyEvent->key(), event->type());
    break; }

    to make sure that I use the correct code, which I do.
    Any idea what I am doing wrong?

    Thanks and best regards,

  • Moderators


    In your event handler you check for KeyPress only.
    I tried this (on Windows):
    void MyWindow::keyPressEvent(QKeyEvent *evt) {
    qDebug() << "Qt press: " << evt->key(); }

    void MyWindow::keyReleaseEvent(QKeyEvent *evt) {
    qDebug() << "Qt release: " << evt->key(); }

    bool MyWindow::nativeEvent(const QByteArray&, void message, long) {
    MSG* msg = reinterpret_cast<MSG*>(message);
    case WM_KEYDOWN:
    qDebug() << "native press:" << msg->wParam; break;
    case WM_KEYUP:
    qDebug() << "native release:" << msg->wParam; break;
    return false;
    For arrow keys only the native press event seems to occur, while release is registered by Qt too. Something must be eating the arrow presses i guess but I don't know what. You can either hunt down the eater or just check for release instead of press (if that fits your case).

  • I reimplemented the notify of the QApplication object.
    I actually did not filter out the KeyPress event (did not return with true and forward to notify) because I just wanted to have the debug output. I now added the KeyRelease event the same way (again, not filtered out) and since then, the cursor keys work.

    There must be something I don't understand, but it works now ;-)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.