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
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.