使用SendInput发送两个或更多字符 [英] Sending Two or more chars using SendInput
问题描述
要发送字符,我们可以使用SendInput。我如何使用它发送多个字符?
To send a char, we can use SendInput. How can I use it to send more than one char?
我尝试了此代码,但未发送任何内容:
I tried this code but it does not send anything:
INPUT in;
in.type=INPUT_KEYBOARD;
in.ki.wScan=0;
in.ki.time=0;
in.ki.dwExtraInfo=0;
in.ki.wVk=0x53+0x54;
SendInput(2,&in,sizeof(INPUT));
那么,正确的方法是什么?
So, what is the right way?
推荐答案
SendInput()
的第一个参数指定多少个 INPUT
结构您正在传递。您仅传递1,但是您告诉 SendInput()
您正在传递2。
The first parameter of SendInput()
specifies how many INPUT
structures you are passing in. You are only passing in 1, but you are telling SendInput()
that you are passing in 2.
您不能在单个 INPUT
中指定两个单独的虚拟密钥。您需要声明一个包含多个 INPUT
的数组,每个虚拟键一个,并且不要忘记包含2个 INPUT $ c $每个虚拟键c> s-一个用于keydown事件,一个用于keyup事件。因此,在您的示例中,您实际上需要4个
INPUT
来发送2个虚拟密钥,如@ user4581301的答案所示。
You cannot specify two separate virtual keys in a single INPUT
. You need to declare an array of multiple INPUT
s, one for each virtual key, and don't forget to include 2 INPUT
s for each virtual key - one for the keydown event and one for the keyup event. So, in your example, you actually need 4 INPUT
s to send 2 virtual keys, as shown in @user4581301's answer.
现在,对于 KEYEVENTF_UNICODE
,您不使用虚拟键,而是使用实际的Unicode代码点,它们使用UTF-16码元指定,每个输入
。因此,这意味着如果要发送需要UTF-16代理对的Unicode代码点,则需要2个 INPUT
s,一个用于高代理,一个用于低代理。 。 SendInput()
文档中提到了 NOT ,但 vScan code>字段是一个16位的
WORD
,并且 KEYEVENTF_UNICODE
事件生成 WM_CHAR
消息,它将UTF-16代理代码单位作为单独的消息传递。
Now, regarding KEYEVENTF_UNICODE
, you don't use virtual keys with it, you use actual Unicode codepoints instead, where they are specified using UTF-16 codeunits, one per INPUT
. So that means if you want to send a Unicode codepoint that requires a UTF-16 surrogate pair, you need 2 INPUT
s, one for the high surrogate and one for the low surrogate. That caveat is NOT mentioned in the SendInput()
documentation, but it is implied by the fact that the vScan
field is a 16bit WORD
, and that KEYEVENTF_UNICODE
events generate WM_CHAR
messages, which passes UTF-16 surrogate codeunits as separate messages.
因此,要使用发送Unicode字符串KEYEVENTF_UNICODE
,您可以执行以下操作:
So, to send a string of Unicode characters using KEYEVENTF_UNICODE
, you can do something like this:
#include <vector>
#include <string>
void SendInputString(const std::wstring &str)
{
int len = str.length();
if (len == 0) return;
std::vector<INPUT> in(len*2);
ZeroMemory(&in[0], in.size()*sizeof(INPUT));
int i = 0, idx = 0;
while (i < len)
{
WORD ch = (WORD) str[i++];
if ((ch < 0xD800) || (ch > 0xDFFF))
{
in[idx].type = INPUT_KEYBOARD;
in[idx].ki.wScan = ch;
in[idx].ki.dwFlags = KEYEVENTF_UNICODE;
++idx;
in[idx] = in[idx-1];
in[idx].ki.dwFlags |= KEYEVENTF_KEYUP;
++idx;
}
else
{
in[idx].type = INPUT_KEYBOARD;
in[idx].ki.wScan = ch;
in[idx].ki.dwFlags = KEYEVENTF_UNICODE;
++idx;
in[idx].type = INPUT_KEYBOARD;
in[idx].ki.wScan = (WORD) str[i++];
in[idx].ki.dwFlags = KEYEVENTF_UNICODE;
++idx;
in[idx] = in[idx-2];
in[idx].ki.dwFlags |= KEYEVENTF_KEYUP;
++idx;
in[idx] = in[idx-2];
in[idx].ki.dwFlags |= KEYEVENTF_KEYUP;
++idx;
}
}
SendInput(in.size(), &in[0], sizeof(INPUT));
}
这篇关于使用SendInput发送两个或更多字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!