Click here to Skip to main content
15,880,725 members
Articles / Desktop Programming / Win32
Alternative
Tip/Trick

Simulate a keystroke in windows

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
1 Sep 2011CPOL 7.5K  
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)


Written By
Software Developer (Senior) Freelancer
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --