Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

Simulate a keystroke in windows

0.00/5 (No votes)
1 Sep 2011CPOL 7.7K  
With SendInput:void SetNumLock (bool active){ BYTE keyState[256]; GetKeyboardState((LPBYTE)&keyState); bool active0= keyState[VK_NUMLOCK] & 1; if (active0!=active) { INPUT inp[2]; ZeroMemory(inp, sizeof(inp)); inp[0].type=INPUT_KEYBOARD; ...

With SendInput:


C#
void SetNumLock (bool active)
{
    BYTE keyState[256]; GetKeyboardState((LPBYTE)&keyState);
    bool active0= keyState[VK_NUMLOCK] & 1;
    if (active0!=active) {
         INPUT inp[2]; ZeroMemory(inp, sizeof(inp));

          inp[0].type=INPUT_KEYBOARD;
          inp[0].ki.dwFlags=0;
          inp[0].ki.wVk= VK_NUMLOCK;

          inp[1].type=INPUT_KEYBOARD;
          inp[1].ki.dwFlags=KEYEVENTF_KEYUP;
          inp[1].ki.wVk=VK_NUMLOCK;

        SendInput(2, inp, sizeof(INPUT));
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)