Neeeck
Neeeck

Reputation: 68

How to send(keybd_event) Unicode Keys with c#

How can I send all Unicode key to 'keybd_event' . . ?

I found 'Alt + Unicode(Decimal code)' methods and C ++ code below

But, I've failed to try in 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);
    }
}

Upvotes: 4

Views: 2759

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596011

Don't use keybd_event(). Use SendInput() instead. It has a KEYEVENTF_UNICODE flag for this very purpose. You can send actual UTF-16 codeunits with it (each char in a .NET String is encoded as a UTF-16 codeunit). No need to simulate Alt+xxxx codes at all. For example:

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

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

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

Upvotes: 4

Related Questions