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?
hutchisonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KDivadCommented:
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!
0
cantrellCommented:
I had the same problem. This is my app that I wrote and just put in the startup group so it turns it on every time I start up my computer. You could set a timer also if you wanted to. Here is the code...


Public Const VK_NUMLOCK = &H90

Public Type KeyboardBytes
     kbByte(0 To 255) As Byte
End Type

Public kbArray As KeyboardBytes

Public Declare Function GetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long

Public Declare Function SetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long


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

End Sub

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hutchisonAuthor Commented:
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.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

hutchisonAuthor Commented:
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.
0
cantrellCommented:
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.
0
KDivadCommented:
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,
0
hutchisonAuthor Commented:
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?
0
KDivadCommented:
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.
0
KDivadCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.