模拟键盘点击(c++)

2020-03-31  本文已影响0人  猪猪一号
#include <windows.h>
#include <thread>
#include <chrono>

int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int       cmdShow)
{


    INPUT input[4];
    memset(input, 0, sizeof(input));

    input[0].type = input[1].type = input[2].type = input[3].type = INPUT_KEYBOARD;  //设置键盘模式

    input[0].ki.wVk = input[3].ki.wVk = VK_MENU;
    input[1].ki.wVk = input[2].ki.wVk = '1';  //模拟ALT + 1

    input[2].ki.dwFlags = input[3].ki.dwFlags = KEYEVENTF_KEYUP;

    SendInput(4, input, sizeof(INPUT));

    INPUT input2[4];
    memset(input2, 0, sizeof(input2));

    input2[0].type = input2[1].type = input2[2].type = input2[3].type = INPUT_KEYBOARD;  //设置键盘模式

    input2[0].ki.wVk = input2[3].ki.wVk = VK_MENU;
    input2[1].ki.wVk = input2[2].ki.wVk = '2';  //模拟ALT + 2

    input2[2].ki.dwFlags = input2[3].ki.dwFlags = KEYEVENTF_KEYUP;

    std::this_thread::sleep_for(std::chrono::milliseconds(200));

    SendInput(4, input2, sizeof(INPUT));


    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读