Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting Caps Lock/Num Lock/Scroll Lock

Status
Not open for further replies.

MartinF

Technical User
Sep 19, 2000
143
FR
Hi,

Can anyone please tell me if it is possible to tell whether or not CAPS/NUM/SCROLL locks are on/off thru VB code, if so, can you point me in the general direction.

Thanks in advance
 
Set the form's KeyPreview property to True (the form will respond to keyboard events before a given control does). In the form KeyPress event, try something like:

Select Case KeyAscii
Case vbKeyCapital
'Caps lock code
Case vbKeyNumlock
'NumLock code
Case Else
End Select

Don't know that I've ever seen a constant/hex value for the ScrLock key- that's a new one on me. You can find more about key constants & related topics in the VB help.
 
Hi there,

Thanks for the reply, it will detect the pressing of CAPS LOCK in the KeyDown event, but is there a way of telling if the caps lock is on or off?

Thanks
 
You can use an API, or use an ocx called KEYSTA32 which was shipped with VS VB6.

You will find it under:

\Microsoft Visual Studio\Common\Tools\CONTROLS\KEYSTA32.ocx

Set a reference to this (PROJECT|COMPONENTS). It will not be listed in the components list, so you need to click the search button.

Once added to the project, add the control to a form and set it's visible property to false.

In code you can check the key states like:

Dim bCapsLockOn As Boolean
Dim bNumLockOn As Boolean
Dim bScrollLockOn As Boolean

Me.MhState1.Style = keyCapsLock
bCapsLockOn = Me.MhState1.Value

Me.MhState1.Style = keyNumLock
bNumLockOn = Me.MhState1.Value

Me.MhState1.Style = keyScrollLock
bScrollLockOn = Me.MhState1.Value
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top