By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,784 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,784 IT Pros & Developers. It's quick & easy.

Toggle the "Caps Lock" key

P: n/a
Can anyone tell me how to toggle the "Caps Lock" key?

Thanks in advance
Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Steven" <sb****@advhomecare.org> schrieb
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.
--
Armin

http://learn.to/quote
http://www.plig.net/nnq/nquote.html

Nov 20 '05 #2

P: n/a
* "Steven" <sb****@advhomecare.org> scripsit:
Can anyone tell me how to toggle the "Caps Lock" key?


You may want to translate this VB6 sample to VB.NET:

<http://www.activevb.de/tipps/vb6tipps/tipp0181.html>

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

P: n/a
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

-----Original Message-----
* "Steven" <sb****@advhomecare.org> scripsit:
Can anyone tell me how to toggle the "Caps Lock" key?


You may want to translate this VB6 sample to VB.NET:

<http://www.activevb.de/tipps/vb6tipps/tipp0181.html>

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

Nov 20 '05 #4

P: n/a
* "Steven" <sb****@advhomecare.org> scripsit:
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>
Nov 20 '05 #5

P: n/a
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.

Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.