How to blink LED’s in Keyboard?

12 03 2009


Do you remember those golden DOS days, where we access the video RAM directly and set the status of NumLock, ScrollLock etc and blink the LED of keyboard. Now in modern windows environment we are no more allowed to access the video RAM directly. But is there any way to blink the keyboard LEDs as we did before?

keyboardblinking


Yes. The trick is to send NumLock keystroke event by using keybd_input() function. See the sample code snippet from MSDN.

// Set NUMLOCK Status.
void SetNumLock( BOOL bState )
{
    BYTE keyState[256];

    GetKeyboardState((LPBYTE)&keyState);
    if( (bState && !(keyState[VK_NUMLOCK] & 1)) ||
        (!bState && (keyState[VK_NUMLOCK] & 1)) )
    {
        // Simulate a key press
        keybd_event( VK_NUMLOCK,
                     0x45,
                     KEYEVENTF_EXTENDEDKEY | 0,
                     0 );

        // Simulate a key release
        keybd_event( VK_NUMLOCK,
                     0x45,
                     KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
                     0);
    }
}

// Blink NUMLOCK.
void BlinkNumLock()
{
    // Blink status.
    bool bBlink = false;

    // Blink the NUMLOCK periodically.
    while( true )
    {
        SetNumLock( bBlink );
        bBlink = !bBlink;
        Sleep( 100 );
    }
}

You can also use SendInput(), which is the latest version of keybd_event() to simulate keystrokes.

And one more thing, I was searching for an image for this post, but couldn’t find a suitable one. And this image is suggested by my wife. 🙂 How is it? Did you like it? She would like to hear from you 😉


Targeted Audiance – Intermediate.

Advertisements