Capslock, Numlock and Scrolllock

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

can anyone out there tell me how do u check the state of
these keys in vb.net? or is there any components in
the .NET Framework that supports doing so? any help is
appreciated.

Thanks.
 
Hi Chris,

It's pretty complicated, because it can only be done, to my knowledge,
through the windows API. Here's some code I use successfully:
Public Declare Function GetKeyState Lib "user32" Alias "GetKeyState" (ByVal
nVirtKey As Long) As Integer

(declare this at the top of the class statement, right after 'inherits
system.windows.forms.form')

in the form's load event:

StatusBar1.Panels.Add("Status")

StatusBar1.Panels.Add("") ' time

StatusBar1.Panels.Add("") ' capslock

StatusBar1.Panels.Add("") ' numlock

StatusBar1.Panels.Add("") ' scroll lock

StatusBar1.Panels.Add("") ' insert key

StatusBar1.Panels.Add("") ' messages

StatusBar1.Panels(0).Width = 90

StatusBar1.Panels(1).Width = 85

StatusBar1.Panels(2).Width = 115

StatusBar1.Panels(3).Width = 115

StatusBar1.Panels(4).Width = 115

StatusBar1.Panels(5).Width = 135

StatusBar1.Panels(6).Width = 335

Then add a timer:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick

Dim capslocki, numlocki, scrolli, inserti As Integer

StatusBar1.Panels(1).Text = Now.ToShortTimeString

capslocki = GetKeyState(&H14)

numlocki = GetKeyState(&H90)

scrolli = GetKeyState(&H91)

inserti = GetKeyState(&H2D)

If capslocki And 1 Then

StatusBar1.Panels(2).Text = "CapsLock On"

Else

StatusBar1.Panels(2).Text = "CapsLock Off"

End If

If numlocki And 1 Then

StatusBar1.Panels(3).Text = "NumLock On"

Else

StatusBar1.Panels(3).Text = "NumLock Off"

End If

If scrolli And 1 Then

StatusBar1.Panels(4).Text = "Scroll Lock On"

Else

StatusBar1.Panels(4).Text = "Scroll Lock Off"

End If

If inserti And 1 Then

StatusBar1.Panels(5).Text = "Insert Mode"

Else

StatusBar1.Panels(5).Text = "Overwrite Mode"

End If

You can then use panels(6) for various messages, as appropriate.

HTH,

Bernie Yaeger
 
Back
Top