使用Windows标头模拟输入事件时,它将停止程序,直到手动中断

时间:2019-01-04 09:30:22

标签: windows c++11 input simulate sendinput

这是我创建的程序,用于模拟多个输入事件,以使用Windows标头为另一个进程提供一些中断。 该程序生成左击事件,右击事件,鼠标光标移动事件和键盘输入事件的数量。

#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;

};

这是.cpp文件

#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;
    }
 }

这是主要功能

#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;
}

在mainMovieMouseLeftClick()处调用它会停止程序并等待按Enter。我不了解其背后的过程

0 个答案:

没有答案