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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Status of NumLock, ScrollLock and CapsLock 2

Status
Not open for further replies.

Flupke

Programmer
Jun 26, 2002
94
BE
How can I detect the status of the NumLock, ScrollLock and CapsLock and put it in a panel in the statusbar in a VB.NET project?

MAny thanks,

Michel
 
I don't think that we can do this without an API call in .NET

Try the following

API is
Public Declare Function GetKeyState Lib "user32" Alias "GetKeyState" (ByVal nVirtKey As Long) As Integer

And use the following code to find out the status of each key

Dim icapslock, inumlock, iscroll, iinsert As Integer
Dim msgTxt As String

icapslock = GetKeyState(&H14)
inumlock = GetKeyState(&H90)
iscroll = GetKeyState(&H91)
iinsert = GetKeyState(&H2D)

msgTxt = "CapsLock is " & IIf(icapslock = 1, "ON", "OFF") & vbCrLf
msgTxt += "NumLock is " & IIf(inumlock = 1, "ON", "OFF") & vbCrLf
msgTxt += "ScrollLock is " & IIf(iscroll = 1, "ON", "OFF") & vbCrLf
msgTxt += "Insert is " & IIf(iinsert = 1, "ON", "OFF") & vbCrLf

MessageBox.Show(msgTxt)

-Kris
 
You can also return a byte from the API call and use the Keys object in VB.Net to make things a bit more DotNet-ish. I built a class to call the function return the key states as boolean:

-----
Public Class Keyboard

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

Public ReadOnly Property CapsLock() As Boolean
Get
Return GetKeyState(Keys.CapsLock)
End Get
End Property

Public ReadOnly Property Insert() As Boolean
Get
Return GetKeyState(Keys.Insert)
End Get
End Property

Public ReadOnly Property NumLock() As Boolean
Get
Return GetKeyState(Keys.NumLock)
End Get
End Property

Public ReadOnly Property ScrollLock() As Boolean
Get
Return GetKeyState(Keys.Scroll)
End Get
End Property

End Class
-----

Then from within your routine, you just create an instance of the class and use the properties:

-----
Private Sub MySub()

Dim kbdHook As Keyboard = New Keyboard

If kbdHook.CapsLock Then
MsgBox("CapsLock is On")
Else
MsgBox("CapsLock is Off")
End If

End Sub
-----


Glen Appleton

VB.Net student.
 
I have not tried it myself, but it appears that the old Keysta32.ocx has been replaced by the Statusbar control in VB5 or probably later.
===========================================================
Microsoft knowledge base 172193 says:
KeySta32.OCX
You can use the Key State control to display or modify the CAPS LOCK, NUM LOCK, INS, and SCROLL LOCK keyboard states.

In Visual Basic 5.0, you can modify the status of CAPS LOCK, NUM LOCK, INS, and SCROLL LOCK using the SendKeys command or Windows API commands. The state of the above keys can be displayed using the StatusBar control. The StatusBar control can be found with the Windows Common Controls, ComCtl32.OCX.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top