如何模拟按键

时间:2017-08-06 10:37:57

标签: c windows

我知道AutoHotKey,但我想制作自己的程序,例如每10秒按一次F5。我搜索了互联网和Stack Overflow,但没有找到解决方案

有没有办法在C中做到?我正在使用和定位Windows 8.1

1 个答案:

答案 0 :(得分:1)

您想要使用SendInput功能。以下代码每10秒向Windows发送一个按键,键入的输入事件对。

#include <windows.h>

static const int delay_ms = 10000;

void sendF5(
    UINT uTimerID,
    UINT uMsg,
    DWORD_PTR dwUser,
    DWORD_PTR dw1,
    DWORD_PTR dw2) {

    INPUT input[2] = {0};
    input[0].type = input[1].type =
        INPUT_KEYBOARD;
    input[0].ki.wVk =
        input[1].ki.wVk = VK_F5;
    input[1].ki.dwFlags =
        KEYEVENTF_KEYUP;
    input[0].ki.dwExtraInfo =
        input[1].ki.dwExtraInfo =
        GetMessageExtraInfo();

    SendInput(2, input, sizeof(INPUT));
}

int WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    PWSTR pCmdLine,
    int nCmdShow) {

    timeSetEvent(delay_ms, 1000,
        sendF5, 0, TIME_PERIODIC);
    return 0;
}