windows 如何在不使用键盘的情况下按键盘上的键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2659841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:18:00  来源:igfitidea点击:

How to press the keys on keyboard without using keyboard?

c++windows

提问by Newbie

I want my program to press certain keys on my keyboard without me doing it physically.

我希望我的程序无需我亲自操作即可按下键盘上的某些键。

How i do this?

我怎么做?

Edit:found this keybd_event() function. seem to be working http://www.codeproject.com/KB/cpp/sendkeys_cpp_Article.aspx

编辑:找到这个 keybd_event() 函数。似乎正在工作 http://www.codeproject.com/KB/cpp/sendkeys_cpp_Article.aspx

采纳答案by fabrizioM

回答by Virne

There is SendInputfunction that can generate keystrokes and other kinds of input. I have used to create application similar to virtual keyboards.

SendInput能产生击键和其他类型的输入的功能。我曾经创建过类似于虚拟键盘的应用程序。

Example using Unicode:

使用 Unicode 的示例:

// This may be needed
// #define _WIN32_WINNT 0x0501 

#include <windows.h>
#include <winuser.h>

void    pressKey(WORD a_unicode)    
{       
        KEYBDINPUT kbinput;
        ZeroMemory(&kbinput, sizeof(kbinput));
        kbinput.wScan = a_unicode;
        kbinput.dwFlags = KEYEVENTF_UNICODE; 
        kbinput.time = 0;

        INPUT input;
        ZeroMemory(&input, sizeof(input));
        input.type = INPUT_KEYBOARD;
        input.ki = kbinput;

        SendInput(1, &input, sizeof(input));
}   

回答by Brian R. Bondy

Use SendMessagewith WM_KEYDOWN

SendMessageWM_KEYDOWN 一起使用

Example:

例子:

SendMessage(hwnd, WM_KEYDOWN, VK_T, NULL);