Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved QML and Windows API

    QML and Qt Quick
    2
    4
    1441
    Loading More Posts
    • 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.
    • W
      Wilha last edited by

      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);
              }
          }
      }
      
      raven-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @Wilha last edited by

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

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        W 2 Replies Last reply Reply Quote 1
        • W
          Wilha @raven-worx last edited by

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

          1 Reply Last reply Reply Quote 0
          • W
            Wilha @raven-worx last edited by

            @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")));
            
            1 Reply Last reply Reply Quote 0
            • First post
              Last post