Question

For my QML based application I would like to have a virtual keyboard (Qt Enterprise is such a tease, but 200$< is beyond my capabilities), so I decided to write my own. But I not quite sure how:

Here is how I see the code:

keypress.h

#ifndef KEYPRESS_H
#define KEYPRESS_H

#include <QObject>

class KeyPress : public QObject
{

    Q_OBJECT

public:
    explicit KeyPress(QObject *parent = 0);

    Q_INVOKABLE void virtKeyPress(const int& Char);

signals:
    void virtKeyPressed(const int& source);

private:
    int vKey;
};

#endif // KEYPRESS_H

keypress.cpp

#include "keypress.h"
#include <QKeyEvent>

KeyPress::KeyPress(QObject *parent) :
    QObject(parent)
{

}

void KeyPress::virtKeyPress(const int &Char){

    vKey = Char;
    QKeyEvent event(QEvent::KeyPress, vKey, Qt::NoModifier);
    // Here I guess I should do somthing like "sendEvent(parent, &event);" or whatever, but nothing seems to work.
    emit virtKeyPressed(vKey);


}

I have a few ideas on how to inplement this in just qml, but where’s the fun in that. :) So I would like to ask for your guidance on the subject. Btw, I’ve searched through info on the web, yet no working code found.

Was it helpful?

Solution

Sending a key event to a Qt Quick object is like :

QKeyEvent *event = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
QCoreApplication::postEvent (engine->rootObjects().first(), event);

You can also take a look at Input Panel Example. It shows how to input text into widgets by an input panel using only the pointer and no keyboard.

Another one is VirtualKeyboard. See the source code and change it to suit your needs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top