SendInput() 键盘字母 C/C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1021175/
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-08-27 18:25:33  来源:igfitidea点击:

SendInput() Keyboard letters C/C++

c++cwinapikeyboard

提问by

I am trying to use SendInput()to send a sentence to another application (Notepad) and then send it hitting the EnterKey.

我正在尝试使用SendInput()将一个句子发送到另一个应用程序(记事本),然后将其发送到Enter键。

Any code snippets? Or help

任何代码片段?或帮助

回答by Nathan Kidd

INPUT input;
WORD vkey = VK_F12; // see link below
input.type = INPUT_KEYBOARD;
input.ki.wScan = MapVirtualKey(vkey, MAPVK_VK_TO_VSC);
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
input.ki.wVk = vkey;
input.ki.dwFlags = 0; // there is no KEYEVENTF_KEYDOWN
SendInput(1, &input, sizeof(INPUT));

input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));

List of virtual key codes.....

虚拟键码列表.....

回答by Lê Quang Duy

I made a modification after reading @Nathan's code, this referenceand combined with @jave.web's suggestion. This code can be used to input characters (both upper-case and lower-case).

我在阅读@Nathan 的代码、此参考并结合@jave.web 的建议后进行了修改。此代码可用于输入字符(大写和小写)。

#define WINVER 0x0500
#include<windows.h>
void pressKeyB(char mK)
{
    HKL kbl = GetKeyboardLayout(0);
    INPUT ip;
    ip.type = INPUT_KEYBOARD;
    ip.ki.time = 0;
    ip.ki.dwFlags = KEYEVENTF_UNICODE;
    if ((int)mK<65 && (int)mK>90) //for lowercase
    {
        ip.ki.wScan = 0;
        ip.ki.wVk = VkKeyScanEx( mK, kbl );
    }
    else //for uppercase
    {
        ip.ki.wScan = mK;
        ip.ki.wVk = 0;

    }
    ip.ki.dwExtraInfo = 0;
    SendInput(1, &ip, sizeof(INPUT));
}

Below is the function for pressing Return key:

下面是按回车键的功能:

    void pressEnter()
{
    INPUT ip;
    ip.type = INPUT_KEYBOARD;
    ip.ki.time = 0;
    ip.ki.dwFlags = KEYEVENTF_UNICODE;
    ip.ki.wScan = VK_RETURN; //VK_RETURN is the code of Return key
    ip.ki.wVk = 0;

    ip.ki.dwExtraInfo = 0;
    SendInput(1, &ip, sizeof(INPUT));

}

回答by Idan K

The SendInput function accepts an array of INPUT structures. The INPUT structures can either be a mouse or keyboard event. The keyboard event structurehas a member called wVk which can be any key on the keyboard. The Winuser.h header file provides macro definitions (VK_*) for each key.

SendInput 函数接受一个 INPUT 结构数组。INPUT 结构可以是鼠标或键盘事件。的键盘事件结构有一个称为WVK构件,其可以是键盘上的任意键。Winuser.h 头文件为每个键提供宏定义 (VK_*)。

回答by John Knoeller

Theres a simple C++ sample here http://nibuthomas.wordpress.com/2009/08/04/how-to-use-sendinput/

这里有一个简单的 C++ 示例http://nibuthomas.wordpress.com/2009/08/04/how-to-use-sendinput/

And a more complete VB sample here http://vb.mvps.org/samples/SendInput/

还有一个更完整的 VB 示例http://vb.mvps.org/samples/SendInput/