vendredi 4 janvier 2019

When simulate input events using windows header it stops the program until give a interrupt manually

Here is the program which i created to simulate multiple input events to give some interrupt for another process using windows header. This program generate number of left click events ,right click events,mouse cursor move event and key board input events.

#pragma once

class InputEventGenerator
{

public:

InputEventGenerator();
~InputEventGenerator();
static void simulatekeyPressEvent(int keyPressCount);
static void simulateMouseLeftClick(int clickCount);
static void simulateMouseRightClick(int clickCount);
static void simulateMouseCurserMove();

private:

InputEventGenerator(const InputEventGenerator&) = delete;
InputEventGenerator operator=(const InputEventGenerator&) = delete;

};

This is the .cpp file

#include "InputEventGenerator.h"
#include <iostream>
#include <windows.h>
#include<atlbase.h>
#include <atlwin.h>

void InputEventGenerator::simulatekeyPressEvent(int keyPressCount)
{
    std::cout << "simulate key press event" << std::endl;
    INPUT ip;
    int KeyPress = keyPressCount;
    Sleep(3000);

    while (KeyPress > 0)
    {
        ip.type = INPUT_KEYBOARD;   // Set up a generic keyboard event.
        ip.ki.wScan = 0; // hardware scan code for key
        ip.ki.time = 0;
        ip.ki.dwExtraInfo = 0;
        ip.ki.wVk = 0x41; //Press the "A" key virtual-key code for the "a" key
        ip.ki.dwFlags = 0; // 0 for key press
        SendInput(1, &ip, sizeof(INPUT));
        ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release 
        //Release the "A" key
        SendInput(1, &ip, sizeof(INPUT));
        std::cout << "key press done" << std::endl;
        Sleep(3000);
        --KeyPress;
        }
    }

void InputEventGenerator::simulateMouseLeftClick(int clickCount)
{
    int click_count = clickCount;
    std::cout << "Left click started" << std::endl;

    SetCursorPos(748, 294);
    while (click_count > 0)
    {
        INPUT input = { 0 };
        input.type = INPUT_MOUSE;
        input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN;
        SendInput(1, &input, sizeof(INPUT));
        Sleep(200);

        input.type = INPUT_MOUSE;
        input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP;
        SendInput(1, &input, sizeof(input));
        Sleep(200);
        std::cout << "left mouse click  done" << std::endl;
        //::ZeroMemory(&input, sizeof(INPUT));
        click_count--;
        }
}

void InputEventGenerator::simulateMouseRightClick(int clickCount)
{
    int click_count = clickCount;
    std::cout << "Right click started" << std::endl;
    SetCursorPos(748, 294);

    while (click_count > 0)
    {
        INPUT input = { 0 };
        input.type = INPUT_MOUSE;
        input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTDOWN;
        SendInput(1, &input, sizeof(INPUT));
        Sleep(200);

        input.type = INPUT_MOUSE;
        input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTUP;
        SendInput(1, &input, sizeof(input));
        std::cout << "right mouse click  done" << std::endl;
        Sleep(200);
        //::ZeroMemory(&input, sizeof(INPUT));
        click_count--;
    }
}

void InputEventGenerator::simulateMouseCurserMove()
{
    std::cout << "simulate mouse move" << std::endl;
    POINT p;
    SetCursorPos(0, 0);

    int loopCount = 100;
    while (loopCount > 0)
   {
        GetCursorPos(&p);
        p.x += 10;
        p.y += 10;
        INPUT input;
        input.type = INPUT_MOUSE;
        input.mi.mouseData = 0;
        input.mi.time = 0;
        input.mi.dx = p.x*(65536 / GetSystemMetrics(SM_CXSCREEN));
        input.mi.dy = p.y*(65536 / GetSystemMetrics(SM_CYSCREEN));
        input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK |MOUSEEVENTF_ABSOLUTE;
        SendInput(1, &input, sizeof(input));
        Sleep(500);
        --loopCount;
    }
 }

This is the main function

#include "InputEventGenerator.h"
#include<atlbase.h>
#include <atlwin.h>

int main(int argc, char * argv[])
{
    InputEventGenerator::simulateMouseCurserMove();
    Sleep(20);
    InputEventGenerator::simulateMouseRightClick(10);
    Sleep(20);
    InputEventGenerator::simulatekeyPressEvent(5);
    Sleep(20);
    InputEventGenerator::simulateMouseLeftClick(10);
    Sleep(20);
    return 0;
}

Here in the main function at simulateMouseLeftClick() call it stops the program and wait until press Enter.I don't understand the process behind this

Aucun commentaire:

Enregistrer un commentaire