Search code examples
c++windowskeyboard-eventskeystroke

How to print/press '@' using keybd_event function?


To press 'a' code is

keybd_event(VkKeyScan(64),0,0,0);

Releasing key code is

keybd_event(VkKeyScan(64),0,KEYEVENTF_KEYUP,0);

For pressing '@' i need combination of two key - SHIFT & 2 , but i don't know how.

keybd_event (https://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx)


Solution

  • Try the following:

    1. Press Shift
    2. Press 2
    3. Release 2
    4. Release Shift

    Addendum

    I just checked my own code where I did the same thing... keybd_event is deprecated (as stated on the site you linked), you should use SendInput instead.

    This are my two functions to send the key press and release:

    void sendKeyDown(unsigned char keyCode)
    {
        INPUT input;
        input.type = INPUT_KEYBOARD;
    
        input.ki.wVk = keyCode;
        input.ki.dwFlags = 0;
        input.ki.time = 0;
        input.ki.dwExtraInfo = 0;
    
        SendInput(1, &input, sizeof(INPUT));
    }
    
    void sendKeyUp(unsigned char keyCode)
    {
        INPUT input;
        input.type = INPUT_KEYBOARD;
    
        input.ki.wVk = keyCode;
        input.ki.dwFlags = KEYEVENTF_KEYUP;
        input.ki.time = 0;
        input.ki.dwExtraInfo = 0;
    
        SendInput(1, &input, sizeof(INPUT));
    }
    

    And this should then give you an @:

    sendKeyDown(VK_SHIFT);
    sendKeyDown(0x32);
    sendKeyUp(0x32);
    sendKeyUp(VK_SHIFT);
    

    Please check the 0x32, I can't reliably test it at the moment to be the key 2.