Link to home
Start Free TrialLog in
Avatar of hutchison
hutchison

asked on

Can i check the status of Num Lock?

I need to check the status of num lock to make sure that it is on all the time?
Avatar of KDivad
KDivad

Add to declarations section:

Private Declare Function GetKeyState Lib "user32" Alias "GetKeyState" (ByVal nVirtKey As Long) As Integer

To check the numlock, use:

If GetKeyState(vbKeyNumLock) = 1 Then
    'NumLock is on
Else
    'NumLock is off
end if

Hope this helps!
ASKER CERTIFIED SOLUTION
Avatar of cantrell
cantrell

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hutchison

ASKER

I like your proposal kDivad but we have an unusual problem is that it somtimes will turn off by itself during operation so i need to keep checking its value and if it is off then turn it back on. I can see your answer to this cantrell but i was wondering if you had a way of turning it on kDivad.
I like your proposal kDivad but we have an unusual problem is that it somtimes will turn off by itself during operation so i need to keep checking its value and if it is off then turn it back on. I can see your answer to this cantrell but i was wondering if you had a way of turning it on kDivad.
Use my code above, and just put

    'Turn numlock on.
    GetKeyboardState kbArray
    kbArray.kbByte(VK_NUMLOCK) = 1
    SetKeyboardState kbArray

in a timer event that checks every 10 seconds to a minute.
Mine would be the same as cantrell's. I would've posted it but all I noticed was "check the status of num lock" and I failed to see "to make sure that it is on all the time".

Later,
I have put the major part of your code into a module and the other code into a timer checking every 10 seconds but it doens't work. If i turn num lock of on purpose then it doesn't com eback on. Whats wrong?
We forgot one little detail. I've never actually tried them, but I've heard that GetKeyboardState and SetKeyboardState only work in the current thread. I don't know of any other way to actually set the numlock globally.
P.S. GetKeyState is also thread-dependent. It seems there isn't any way to correctly get/set the state of the locks from VB. With memory I/O, it's actually a fairly simple matter using Peek and Poke, too bad VB doesn't support them.