Toggle the "Caps Lock" key

  • Thread starter Thread starter Steven
  • Start date Start date
Steven said:
Can anyone tell me how to toggle the "Caps Lock" key?

Have a look at the API function "keybd_event" in the help index. There is
also an example on how to set the state.
 
Well, I did get it to run and change the colors. However
none of the keys seemed to toggle. Here is what I wrote:

*****************************************

Public Const VK_CAPITAL = &H14

Private Declare Function GetKeyboardState Lib "user32" _
(ByVal pbKeyState As Byte) As Long

Private Declare Function SetKeyboardState Lib "user32" _
(ByVal lppbKeyState As Byte) As Long

Const VK_NUMLOCK = &H90
Const VK_SCROLL = &H91
'Const VK_CAPITAL = &H14

Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 50
Call Toggle(7)
Call Toggle(7)
End Sub

Private Sub Command1_Click(ByVal Index As Integer)
MsgBox(2 ^ Index)
Call Toggle(2 ^ Index)
End Sub



Private Sub Timer1_Timer()
Call Toggle(Rnd() * 8)
End Sub

Private Sub Toggle(ByVal Key As Integer)
If Key And 1 Then
If ToggleKey(VK_NUMLOCK) Then
Me.lblNumLock.BackColor =
System.Drawing.Color.Aquamarine
Else
Me.lblNumLock.BackColor =
System.Drawing.Color.RoyalBlue
End If
End If

If Key And 2 Then
If ToggleKey(VK_CAPITAL) Then
Me.lblCapsLock.BackColor =
System.Drawing.Color.Aquamarine
Else
Me.lblCapsLock.BackColor =
System.Drawing.Color.RoyalBlue
End If
End If

If Key And 4 Then
If ToggleKey(VK_SCROLL) Then
Me.lblScrollLock.BackColor =
System.Drawing.Color.Aquamarine
Else
Me.lblScrollLock.BackColor =
System.Drawing.Color.RoyalBlue
End If
End If
End Sub

Private Function ToggleKey(ByVal Key As Byte) As Boolean
Dim State As Boolean
Dim Keys(255) As Byte

Call GetKeyboardState(Keys(0))
State = Keys(Key)

If State <> True Then
Keys(Key) = 1
Else
Keys(Key) = 0
End If
Call SetKeyboardState(Keys(0))
ToggleKey = Not State
End Function


Private Sub Check1_CheckedChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Check1.CheckedChanged
If Check1.Checked = True Then
Timer1.Enabled = True
Else
Timer1.Enabled = False
End If

End Sub

Private Sub btnNumLock_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnNumLock.Click
Call Command1_Click(0)
End Sub

Private Sub btnCaps_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnCaps.Click
Call Command1_Click(1)
End Sub

Private Sub btnScroll_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnScroll.Click
Call Command1_Click(2)
End Sub

**********************************************

Any ideas?

Steven
 
* "Steven said:
Well, I did get it to run and change the colors. However
none of the keys seemed to toggle. Here is what I wrote:

*****************************************

Public Const VK_CAPITAL = &H14

Declare the constant as 'Byte'.
Private Declare Function GetKeyboardState Lib "user32" _
(ByVal pbKeyState As Byte) As Long

Private Declare Function SetKeyboardState Lib "user32" _
(ByVal lppbKeyState As Byte) As Long

Replace all 'As Long' with 'As Int32'.
Const VK_NUMLOCK = &H90
Const VK_SCROLL = &H91
'Const VK_CAPITAL = &H14

Declare them as 'Byte'.
Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 50
Call Toggle(7)
Call Toggle(7)
End Sub

Private Sub Command1_Click(ByVal Index As Integer)
MsgBox(2 ^ Index)
Call Toggle(2 ^ Index)
End Sub

Get rid of the 'Call'.

Try again and let us know if it worked.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Improve your quoting style:
<http://learn.to/quote>
<http://www.plig.net/nnq/nquote.html>
 
Hi Steven,

Did Herfried's suggestion works for you?
If you have any concern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top