LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I disable and enable the NumLock on the keyboard ?

Is there a CVI function to enable or to disable the numeric lock
on the keyboard ?

thanks David
0 Kudos
Message 1 of 6
(4,369 Views)
Seems that tunring on and off the numlock key is not so simple as desired.
There is not a native CVI function for that, but looking in the SDK and the knowledge base, I arranged this piece of software that works on a Win98 machine:

void ToggleNumLock (void)
{
short state;
int err;
BYTE ks[256];

// Method 1: should work on Win98 but it doesn't
/*
err = GetKeyboardState (ks);
ks[90] = !ks[90];
err = SetKeyboardState (ks);
*/

// Method 2: should work on WinNT or Win2K
/*
VOID keybd_event(
BYTE bVk, // virtual-key code
BYTE bScan, // hardware scan code
DWORD dwFlags, // function options
ULONG_PTR dwExtraInfo // additional keystroke data
);
*/
//err = GetKeyboardState (ks
);
/* bstate holds the desired state for the button
if( (bState && !(ks[VK_NUMLOCK] & 1)) ||
(!bState && (ks[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);
*/
//}

// Method 3: actually works on Win98
err = GetKeyboardState (ks);
state = GetKeyState (VK_NUMLOCK);
ks[VK_NUMLOCK] = !state;
err = SetKeyboardState (ks);

return;
}

For reference, look at these two Microsoft articles in the MSDN:

Q177674 HOWTO: Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys
Q127190 : HOWTO: Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys

Hope this helps
Roberto


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 6
(4,369 Views)
A little comment just to correct a big mistake in the source code: since hex is NOT dec (!!), method 1 must be written this way:

// Method 1: should work on Win98 but it doesn't
err = GetKeyboardState (ks);
ks[0x90] = !ks[0x90]; // 0x90 is the value of the macro VK_NUMLOCK
err = SetKeyboardState (ks);

This way it works correctly without need to use GetKeyState as in my method 3.

Roberto


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 6
(4,369 Views)
I tried running your example under Win98SE (see attached project).
It seems the physical and virtual keyboard get out of sync until the
virtual state is toggled to match the physical LED state. What gives?

"Roberto Bozzolo" wrote in message
news:506500000005000000C2740000-1019262487000@exchange.ni.com...
> A little comment just to correct a big mistake in the source code:
> since hex is NOT dec (!!), method 1 must be written this way:
>
> // Method 1: should work on Win98 but it doesn't
> err = GetKeyboardState (ks);
> ks[0x90] = !ks[0x90]; // 0x90 is the value of the
> macro VK_NUMLOCK
> err = SetKeyboardState (ks);
>
> This way it works correctly without need to use GetKeyState as in my
> method 3.
>
> Roberto




[Attachment numlock.zip, see below]
0 Kudos
Message 4 of 6
(4,369 Views)
I could reproduce your situation only partially.

I found that:
- if I press the button on the panel an odd number of times
- next exit the program
- next press the numlock key to toggle state
when I restart the program the keyboard and panel led state are opposite one to each other and so remain if I press the numloky keyboard key (i.e. keyboard light ON -> panel led OFF and vice versa); in this situation, the first time I press the panel button, the led changes its state while the keyboard light doesn't, so syncronizing the two lights.

If this is the situation, simply adding two times NumLock (TRUE); at the beginning of the program gives a correct and syncronized state between logical and physical keyboard.

Roberto


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 5 of 6
(4,369 Views)
Calling NumLock(TRUE) twice at start-up works. Another work-around is to
count how many times Num Lock is toggled in the NumLock() routine. Just
before the program exits, if it was toggled an odd number of times, you
toggle it one more time. This has the affect of leaving the Num Lock key
and Windows' virtual keyboard state in the same state as when the program
began (good Windows etiquette :-)).

Nick


"Roberto Bozzolo" wrote in message
news:506500000005000000B8760000-1019262487000@exchange.ni.com...
> I could reproduce your situation only partially.
>
> I found that:
> - if I press the button on the panel an odd number of times
> - next exit the program
> - next press the numlock key to toggle state
> when I restart
the program the keyboard and panel led state are
> opposite one to each other and so remain if I press the numloky
> keyboard key (i.e. keyboard light ON -> panel led OFF and vice versa);
> in this situation, the first time I press the panel button, the led
> changes its state while the keyboard light doesn't, so syncronizing
> the two lights.
>
> If this is the situation, simply adding two times NumLock (TRUE); at
> the beginning of the program gives a correct and syncronized state
> between logical and physical keyboard.
>
> Roberto
0 Kudos
Message 6 of 6
(4,369 Views)