C++ 使用WM_CHAR键入Unicode符号

C++ 使用WM_CHAR键入Unicode符号,c++,winapi,unicode,C++,Winapi,Unicode,如何使用WM_CHAR消息键入一些Unicode符号(如西里尔字母)?现在我有了西里尔字母符号的错误输入。 这是我的代码: DWORD dwCurrentTreadID = GetCurrentThreadId(); HWND hForeground = GetForegroundWindow(); DWORD dwForegroungThreadID = GetWindowThreadProcessId(hForeground, NULL); AttachThreadInput(dwForeg

如何使用WM_CHAR消息键入一些Unicode符号(如西里尔字母)?现在我有了西里尔字母符号的错误输入。 这是我的代码:

DWORD dwCurrentTreadID = GetCurrentThreadId();
HWND hForeground = GetForegroundWindow();
DWORD dwForegroungThreadID = GetWindowThreadProcessId(hForeground, NULL);
AttachThreadInput(dwForegroungThreadID,dwCurrentTreadID,true);
PostMessageW(GetFocus(), WM_CHAR, character, 1);
。改用
SendInput()

INPUT input = {0};

input.type = INPUT_KEYBOARD;
input.ki.wScan = (WORD) character;
input.ki.dwFlags = KEYEVENTF_UNICODE;

SendInput(1, &input, sizeof(INPUT));
Windows中的Unicode使用UTF-16
wScan
为16位,因此它只能容纳单个UTF-16编码单元。您可以将多达
U+FFFF
的Unicode码点放入单个代码单元,但要发送高于U+FFFF的码点(需要2个代码单元),您必须提供2个
输入值,每个代码单元一个:

INPUT input[2] = {0};
int numInput;

// character should be a 32bit codepoint and not exceed 0x10FFFF...
if (character <= 0xFFFF)
{
    input[0].type = INPUT_KEYBOARD;
    input[0].ki.wScan = (WORD) character;
    input[0].ki.dwFlags = KEYEVENTF_UNICODE;

    numInput = 1;
}
else
{
    character -= 0x010000;

    input[0].type = INPUT_KEYBOARD;
    input[0].ki.wScan = (WORD) (((character >> 10) & 0x03FF) + 0xD800);
    input[0].ki.dwFlags = KEYEVENTF_UNICODE;

    input[0].type = INPUT_KEYBOARD;
    input[1].ki.wScan = (WORD) ((character & 0x03FF) + 0xDC00);
    input[0].ki.dwFlags = KEYEVENTF_UNICODE;

    numInput = 2;
}

SendInput(numInput, input, sizeof(INPUT));
输入[2]={0};
int numInput;
//字符应为32位代码点,且不超过0x10FFFF。。。
如果(字符>10)&0x03FF)+0xD800);
输入[0]。ki.dwFlags=KEYEVENTF_UNICODE;
输入[0]。类型=输入\键盘;
输入[1].ki.wScan=(字)((字符&0x03FF)+0xDC00);
输入[0]。ki.dwFlags=KEYEVENTF_UNICODE;
numInput=2;
}
SendInput(numInput,input,sizeof(input));
您可以将其封装在发送UTF-16编码输入字符串的函数中:

void SendInputStr(const std::wstring &str) // in C++11, use std::u16string instead...
{
    if (str.empty()) return;

    std::vector<INPUT> input(str.length());

    for (int i = 0; i < str.length(); ++i)
    {
        input[i].type = INPUT_KEYBOARD;
        input[i].ki.wScan = (WORD) str[i];
        input[i].ki.dwFlags = KEYEVENTF_UNICODE;
    }

    SendInput(input.size(), &input[0], sizeof(INPUT));
}
void SendInputStr(const std::wstring&str)//在C++11中,使用std::u16string代替。。。
{
if(str.empty())返回;
标准::向量输入(str.length());
对于(int i=0;i
您可能不知道。可能您使用的是自动化,而不是伪造输入。