Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML and Windows API
Forum Updated to NodeBB v4.3 + New Features

QML and Windows API

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 1.8k Views
  • 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 Offline
    W Offline
    Wilha
    wrote on 27 Jan 2016, 05:46 last edited by
    #1

    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);
            }
        }
    }
    
    R 1 Reply Last reply 27 Jan 2016, 07:08
    0
    • W Wilha
      27 Jan 2016, 05:46

      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);
              }
          }
      }
      
      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 27 Jan 2016, 07:08 last edited by
      #2

      @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 28 Jan 2016, 05:52
      1
      • R raven-worx
        27 Jan 2016, 07:08

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

        W Offline
        W Offline
        Wilha
        wrote on 28 Jan 2016, 05:52 last edited by
        #3

        @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
        0
        • R raven-worx
          27 Jan 2016, 07:08

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

          W Offline
          W Offline
          Wilha
          wrote on 30 Jan 2016, 08:51 last edited by
          #4

          @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
          0

          1/4

          27 Jan 2016, 05:46

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved