如何用c#发送(keybd_event) Unicode密钥

本文关键字:event Unicode 密钥 keybd 何用 发送 | 更新日期: 2023-09-27 18:02:23

如何将所有Unicode密钥发送到'keybd_event' . .

?

我发现'Alt + Unicode(Decimal code)'方法和c++代码下面

但是,我在c#中尝试失败了…

void TypingMessageFromCodePage(TCHAR* Message, UINT CodePage=0)
{
    TCHAR Word[2];
    TCHAR WordCode[64];
    char MultiByte[64];
    static const BYTE NumCode[10]={0x2D, 0x23, 0x28, 0x22, 0x25, 0x0C, 0x27, 0x24, 0x26, 0x21};
    int Length = wcslen(Message);
    for(int i=0; i<Length; i++)
    {
        Word[0] = Message[i];
        Word[1] = L''0';
        WideCharToMultiByte(CodePage, 0, Word, -1, MultiByte, 64, NULL, NULL);
        _itow((int)(((~MultiByte[0])^0xff)<<8)+((~MultiByte[1])^0xff), WordCode, 10);
        keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0);
        for(int j=0; j<wcslen(WordCode); j++)
        {
            keybd_event(NumCode[(int)WordCode[j]-48], MapVirtualKey(NumCode[(int)WordCode[j]-48], 0), 0, 0);
            keybd_event(NumCode[(int)WordCode[j]-48], MapVirtualKey(NumCode[(int)WordCode[j]-48], 0), KEYEVENTF_KEYUP, 0);
        }
        keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), KEYEVENTF_KEYUP, 0);
    }
}

如何用c#发送(keybd_event) Unicode密钥

不要使用keybd_event()。使用SendInput()代替。为了这个目的,它有一个KEYEVENTF_UNICODE标志。你可以用它发送实际的UTF-16码单元(每个char在一个。net String被编码为一个UTF-16码单元)。不需要模拟Alt+xxxx代码。例如:

https://stackoverflow.com/a/30630477/65863

https://stackoverflow.com/a/15442914/65863

https://stackoverflow.com/a/8885228/65863