How to send Keys to Another Window - Windows API in QT C++

How to send Keys to Another Window - Windows API in QT C++

Hello everyone, in this article we are going to talk about how can we use Windows API's in our QT C++ projects. We are going to see it with a C++ Console example application.

Let's begin.

In this example we are going to call the same functions which I have talked at How to use Interop Services in C# (Focus on external window). You can take a look at that article to see what are we going to do here.

Let's get started then.

Firstly you have to add below code line to your .pro file. This line will specify that we are going to access to User32.dll library which located at operating System folder.

LIBS += -lUser32

So in our main.c file we can start to write required codes.

Firslty we have to include required libraries.

#include <QCoreApplication>
#include <QThread>
#include <iostream>
//We are telling the program with below library, we are going to access to the system API's
#include "Windows.h"

using namespace std;
Now in main function : Firstly We have to find the target window with FindWindow( ... ) method.

HWND window = FindWindowW(NULL, L"exampleText.txt - Not Defteri");
And if a window was found then we have to focus on that window with SetForegroundWindow( ... ) method.

if (window != 0)
    {
        SetForegroundWindow(window);
        //Waiting a little bit is good
        QThread::msleep(1000);
    }

Now we are ready to send texts inside of selected program above.

There is no method directly like SendKeys.Send to send a string in C++. So we need to create our unicode string and send it byte as byte with SendInput method with INPUT struct.
In here, First we need to specify the text which will be sent, Then we are going to create a vector which will contain key inputs. Make configuations of the key input. We specify the every character of the text as unicode keyboard_input. Lastly we send the characters by same order

        //First we need to specify the text which will be sent
        std::wstring text = L"Burak Hamdi TUFAN\nthecodeprogram.com\n";
        //Then we are going to create a vector which will contain key inputs
        std::vector< INPUT > vec;

        for(auto ch : text)
        {
            INPUT key_input = { 0 };
            //Make configuations of the key input
            //We specify the every character of the text as unicode keyboard_input
            key_input.type = INPUT_KEYBOARD;
            key_input.ki.dwFlags = KEYEVENTF_UNICODE;
            key_input.ki.wScan = ch;
            vec.push_back(key_input);
        }
        //Lastly we send the characters by same order
        SendInput(vec.size(), vec.data(), sizeof(INPUT));

Here we are ready. Below image you can see the result of the example. Sending keys to external window with QT C++ Example Output

That is all in this article.

Burak Hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!

Comments

  • There is no comment. Be the owner of first comment...