Constructor arguments of QKeyEvent ?

  • Hello,
    I must implement a simple virtual keyboard. It is not all clear for me how to do it. Buttons/Layout etc. is simple but how to send the keys.
    Here some code examples:

    QWidget * w = QApplication::focusWidget();
       QKeyEvent e(QEvent::KeyPress, Qt::Key_X,Qt::NoModifier, "ab");
       QApplication::sendEvent(w, &e);
       QKeyEvent e2(QEvent::KeyRelease, Qt::Key_A,Qt::NoModifier, "a");
       QApplication::sendEvent(w, &e2);

    Unclear is the constructor of QKeyEvent. Why argument 2 AND argument 4.
    Argument 2 should describe the key. For what is argument 4. I found that this string (multiple chars) will be send to the input widget and I must set here a string which is independent of argument 2. For what is argument 2 then ?
    Second question is: Must I send a keyrelease for proper working of the virtual keyboard.
    Any other tips for a developing a virtual keyboard are welcome, too.

  • Lifetime Qt Champion


    I would seek inspiration in existing implementations

  • Lifetime Qt Champion


    The QKeyEvent constructor documentation explains all the parameters.

  • Hello @mrjj ,
    as a inspiration it is good.
    They use an other concept, in the close sense it is not really a virtual keyboard. It works only with a QLineEdit (and its subclasses) and sends keystrokes never but rather manipulates the QLineEdit::text directly.

    In general I dont understand, why I must send a keystroke to a special widget with the following code (I in my code I look for the focused.)
    QApplication::sendEvent(wiget, &event);
    This works only for Qt-Apps. Why can I not send a key to the operating system and the system processes it relating to its rules ?

  • Lifetime Qt Champion

    Oh, sorry I thought it would be more clever made.
    To send events to the OS, you need to use native API.

    Qt has support for handling the virtual keyboard
    notice the QEvent::RequestSoftwareInputPanel
    I have not tested these on Desktop but Docs seems not to limit use.

    If you want to make a native virtual keyboard that work with ANY app, you need to dive into the native app for that.

Log in to reply

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