Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Can not generate Cursor KeyEvent

[Solved] Can not generate Cursor KeyEvent

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • McLionM Offline
    McLionM Offline
    McLion
    wrote on last edited by
    #1

    Hi

    I am generating KeyEvents with this code:
    @QWidget *ActWidget = QApplication::focusWidget();
    if(ActWidget)
    { 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,
    McLion

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      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);
      switch(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).

      1 Reply Last reply
      0
      • McLionM Offline
        McLionM Offline
        McLion
        wrote on last edited by
        #3

        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 ;-)
        Thanks.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved