問題描述
要發送字符,我們可以使用 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
- 一個用于 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, 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 代碼單元指定,每個 INPUT代碼>.所以這意味著如果你想發送一個需要 UTF-16 代理對的 Unicode 代碼點,你需要 2 組向下/向上
INPUT
,一組用于高代理,一組用于低代理.該警告在 SendInput()
文檔中未提及,但實際上 vScan
字段是 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 sets of down/up INPUT
s, one set for the high surrogate, and one set 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.
因此,要使用 KEYEVENTF_UNICODE
發送一串 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 發送兩個或更多字符的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!