Search code examples
c++cwinapisendkeys

How to send unicode keys with c++ (keybd_event)


My friend is learning Norwegian and i want to make a global hot key program which sends keys such as

æ
ø
å

My problem is that keybd_event function wont allow me to send those keys, i seem to be restricted to the virtual key codes is there another function that i could use or some trick to sending them?


Solution

  • You have to use SendInput instead. keybd_event does not support sending such characters (except if they are already in the current codepage, like on Norwegian computers). A bit of sample code to send an å:

    KEYBDINPUT kb={0};
    INPUT Input={0};
    
    // down
    kb.wScan = 0x00c5;
    kb.dwFlags = KEYEVENTF_UNICODE;
    Input.type = INPUT_KEYBOARD;
    Input.ki = kb;
    ::SendInput(1,&Input,sizeof(Input));
    
    // up
    kb.wScan = 0x00c5;
    kb.dwFlags = KEYEVENTF_UNICODE|KEYEVENTF_KEYUP;
    Input.type = INPUT_KEYBOARD;
    Input.ki = kb;
    ::SendInput(1,&Input,sizeof(Input));
    

    In case you didn't know: it is easy to install additional keyboard layouts on Windows and switch between them with a shortcut.

    Lykke til!