如何模拟按键 [英] How to simulate a keypress

查看:40
本文介绍了如何模拟按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know about AutoHotKey but I want to make my own program for e.g. make it press F5 every 10 seconds. I searched the internet and Stack Overflow but did not find a solution

有没有办法在 C 中做到这一点?我正在使用并针对 Windows 8.1

Is there a way to do it in C or not? I am using and targeting Windows 8.1

推荐答案

您想要使用 SendInput 函数.以下代码每 10 秒向 Windows 发送一对按下键和按下键的输入事件.

You'd want to use the SendInput function. The following code sends a key-down, key-up pair of input events to Windows every 10 seconds.

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

这篇关于如何模拟按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆