QML and Windows API



  • How do i go about implementing the Windows API with QML, specifically the sendInput() method where i can call it after a press event is triggered in QML. For example I have the following c++ code that I want to implement on the QML code following it.

    I'm new to Qt and QML so bear with my ignorance if this is harder than it may seem.

           #include "Windows.h"
           #include "WinUser.h"
            INPUT key;
            key.type = INPUT_KEYBOARD;
            key.ki.wScan = 0; // hardware scan code for key
            key.ki.time = 0;
            key.ki.dwExtraInfo = 0;
            key.ki.wVk = 0x30; // virtual-key code for the "0" key
            key.ki.dwFlags = 0; // 0 for key press
            SendInput(1, &key, sizeof(INPUT));
            key.ki.wVk = 0x30; // virtual-key code for the "0" key
            key.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
            SendInput(1, &key, sizeof(INPUT));
    
    *********************************
    
    Window {
    
        visible: true
        width: Screen.width/2
        height: Screen.height/2
    
        Rectangle {
            id: numberZero
            border.width : 2
            border.color : "blue"
            width: parent.width/8
            height: parent.width/8
        }
    
        Text {
            id: numberZeroText
            anchors.centerIn: numberZero
            text: "0"
        }
    
        MouseArea {
            id: clickNumberZeroMouseArea
            anchors.fill: numberZero
            width: numberZero.width
            height: numberZero.height
            onPressed: {
                console.log(Qt.Key_0);
            }
        }
    }
    

  • Moderators

    @Wilha
    This needs to be done from C++.
    So you could create a custom QObject with invokable methods and publish it to QML.



  • @raven-worx
    Thanks for pointing me in the right direction. I'll give it a go and come back with results later.
    Thanks again.



  • @raven-worx
    I went ahead and did as you specified, the issue is that the Qt application is the layer that is capturing my key events, how do I send the events to Windows OS directly bypassing the Qt app. Therefore, Windows OS captures the events and the input is sent to "notepad" for example.

    heres a snippet of the code:

    main.qml:

        MouseArea {
            id: clickNumberZeroMouseArea
            anchors.fill: numberZero
            width: numberZero.width
            height: numberZero.height
            onPressed: {
                //console.log(Qt.Key_0);
                keyEvent.typeNow();
            }
        }
    

    keyevent.h:

    class KeyEvent : public QObject
    {
        Q_OBJECT
    public:
        Q_INVOKABLE void typeNow();
    };
    

    keyevent.cpp:

    void KeyEvent::typeNow(){
        INPUT key;
        key.type = INPUT_KEYBOARD;
        key.ki.wScan = 0; // hardware scan code for key
        key.ki.time = 0;
        key.ki.dwExtraInfo = 0;
        key.ki.wVk = 0x30; // virtual-key code for the "0" key
        key.ki.dwFlags = 0; // 0 for key press
        SendInput(1, &key, sizeof(INPUT));
        key.ki.wVk = 0x30; // virtual-key code for the "0" key
        key.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
        SendInput(1, &key, sizeof(INPUT));
    }
    

    main.cpp:

        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        qmlRegisterType<KeyEvent>("keyEvent", 1, 0, "keyEvent");
        KeyEvent keyEvent;
        engine.rootContext()->setContextProperty("keyEvent", &keyEvent);
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    

Log in to reply
 

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