How to set and unset caps lock

what i want is some code that i can run at the start of the program to set caps lock to on and also some code to set it off as the program ends.
kplonkAsked:
Who is Participating?
 
AndySulzConnect With a Mentor Commented:
dim x as vbkeycodeconstant
x = vbKeyCapital
sendkeys x
0
 
caraf_gCommented:
Check out
http://homepage.eircom.net/~carafa/VBSamples/VBAll/NumLock.zip
This sample shows you how to check and toggle Caps Lock, Num Lock AND Scroll lock.

Good luck!

Pino
0
 
ajsriramCommented:
Public Const VK_CAPITAL = &H14


Public Type KeyboardBytes
         kbByte(0 To 255) As Byte
    End Type
    Public kbArray As KeyboardBytes


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


Public Declare Function GetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long


Public Declare Function SetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
                                    
 


On  form, add a 3 command buttons (cmdToggle, cmdTurnOff, cmdTurnOff) and a label. Add the following code to the form:


Private Function CapsLock() As Integer
CapsLock
= GetKeyState(VK_CAPITAL) And 1 = 1
End Function



Private Sub Form_Load()

If CapsLock() = 1 Then Label1 = "On" Else Label1 = "Off"
End Sub



Private Sub cmdToggle_Click()
GetKeyboardState kbArray
kbArray.kbByte(VK_CAPITAL) =
IIf(kbArray.kbByte(VK_CAPITAL) = 1, 0, 1)
SetKeyboardState kbArray
Label1 =
IIf(CapsLock() = 1, "On", "Off")
End Sub



Private Sub cmdTurnOn_Click()
GetKeyboardState kbArray
kbArray.kbByte(VK_CAPITAL) = 1
SetKeyboardState kbArray
Label1 =
IIf(CapsLock() = 1, "On", "Off")
End Sub



Private Sub cmdTurnOff_Click()
GetKeyboardState kbArray
kbArray.kbByte(VK_CAPITAL) = 0
SetKeyboardState kbArray
Label1 =
IIf(CapsLock() = 1, "On", "Off")
End Sub
 
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
caraf_gCommented:
Yes, ajsriram, that's exactly what I'm doing in my example.
0
 
amebaCommented:
No, it isn't the same - you are using keybd_event (which works OK).

ajsriram used SetKeyboardState which has some problems (leaving lights in wrong state when focus goes to other app). Most of the VB sites have this incorrect (Win 3.1) code.
0
 
kplonkAuthor Commented:
the reason that i rejected this was the same as the one the was pointed out by ameba thanks for all the help though. amber as i used your code or atlest some of it if you repost as an answer you can have the points.  see ya later kieran
0
 
amebaCommented:
kplonk, I think you can "accept comment as an answer" - new feature on EE.

Happy New Year!
0
 
caraf_gCommented:
Happy New Year everyone
0
 
caraf_gCommented:
A message from the future:
Eejit.
0
All Courses

From novice to tech pro — start learning today.