CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 1999
    Location
    Canada
    Posts
    176

    Num Lock, Caps Lock & Scoll Lock

    Could someone tell me how to toggle ON and OFF the Num Lock, Caps Lock & Scroll Lock leds?

    Where there is a WISH, there is a WILL.

  2. #2
    Join Date
    Apr 1999
    Posts
    306

    Re: Num Lock, Caps Lock & Scoll Lock

    Catch them in your overriden function PreTranslateMessage() and what ever you wanna do with them. Something like this:

    int CMyDialog::PreTranslateMessage(MSG *pMsg)
    {
    if(pMsg->message==WM_KEYDOWN)
    if(pMsg->wParam==VK_NLOCK ||pMsg->message==VK_CLOCK||pMsg->message==VK_SLOCK)
    {
    // do your stuff here like
    return 0;
    // or whatever you need
    }

    return CDialog::PretranslateMessage(pMsg);
    }


  3. #3
    Join Date
    Apr 1999
    Posts
    9

    Re: Num Lock, Caps Lock & Scoll Lock

    Hi,

    You could refer to the KB article number Q127190 in the MSDN. This article tells you exactly how you can acieve this. He asks you to use the keybd_event() to do this. A sample code for toggling the num lock is also given. The sample is as follows:

    #include <windows.h>

    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);
    }
    }

    void main()

    {
    SetNumLock( TRUE );

    }

    VK_NUMLOCK is used for num lock. The same code can be modified with VK_CAPITAL & VK_SCROLL for caps lock & scroll lock respectively.

    Hope this helps.

    Regards,
    Smriti Venkatesh

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured