Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Simulating key press in C++ for use in QML for virtual keyboard.



  • Hi. 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.



  • Please look at kde-app website. There are a lot of virtual keyboards already made.



  • [quote author="mrdebug" date="1406580379"]Please look at kde-app website. There are a lot of virtual keyboards already made.[/quote]

    I didn't quite understood what you ment by that.

    If you suppose I should use them "as is", that is a not a good idea (basicly impossible for what I'm doing).

    And if you suppose I should look through sourcecode... well most of the apps I already saw (about 4 or 5) are all widget based, and the worst part is that I'm stuck with qml for the moment.

    So any other ideas?


Log in to reply