Capslock in VBA

Asala42

Well-known Member
Joined
Feb 26, 2002
Messages
2,318
Hey all,

How do you programatically enable the CAPSLOCK key in VBA? I've tried:

Sendkeys "{CAPSLOCK}"

When I run this, Excel's status bar flashes "CAPS" for about 1 second then disappears. It seems to have no impact on the keyboard for case-sensitivity - which is what I'm shooting for.
 
I agree, like:

http://www.vbapi.com

Although it's more of a philosophical question really if Microsoft should support their software with documentation or should they leave it to amateurs or hackers on the web. Besides, if you don't have the API Viewer, API's are a complete pain in the rear-end.
 
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Thank you zacemmel. I have been trying for the last hour or 2 to get the *#&$7 capslock to set via vba. This worked like a champ. I tried researching API calls but couldn't really find the info needed to set up the declaration. Where is this located?

Thanks again.
wenckdm
 
Upvote 0
Another way to handle the problem;

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

Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Long
Private Declare Function GetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
Private Declare Function SetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long


Private Sub Workbook_Open()
KeyCode = &H14
GetKeyboardState kbArray
kbArray.kbByte(KeyCode) = IIf(kbArray.kbByte(KeyCode) = 1, 0, 1)
SetKeyboardState kbArray
End Sub

I tried the above and it worked nicely!
It reverses the process. Pressing Shift now becomes lowercase..
NEAT!!
 
Upvote 0
Here's some alternative code covering On, Off and Toggle :
Code:
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long
Public keyCodes(0 To 255) As Byte, caps%

Sub CapsLock_Off()
If GetKeyState(20) Then keyCodes(20) = 0
caps = SetKeyboardState(keyCodes(0))
End Sub

Sub Capslock_On()
If Not GetKeyState(20) Then keyCodes(20) = 1
caps = SetKeyboardState(keyCodes(0))
End Sub

Sub CapsLock_Toggle()
If GetKeyState(20) Then
    keyCodes(20) = 0
Else
    keyCodes(20) = 1
End If
caps = SetKeyboardState(keyCodes(0))
End Sub
 
Upvote 0
here's some alternative code covering on, off and toggle :
Code:
declare function getkeystate lib "user32" (byval nvirtkey as long) as integer
declare function setkeyboardstate lib "user32" (lppbkeystate as byte) as long
public keycodes(0 to 255) as byte, caps%

sub capslock_off()
if getkeystate(20) then keycodes(20) = 0
caps = setkeyboardstate(keycodes(0))
end sub

sub capslock_on()
if not getkeystate(20) then keycodes(20) = 1
caps = setkeyboardstate(keycodes(0))
end sub

sub capslock_toggle()
if getkeystate(20) then
    keycodes(20) = 0
else
    keycodes(20) = 1
end if
caps = setkeyboardstate(keycodes(0))
end sub

thank you..
 
Upvote 0
thank you..

is there a sub routine for the ff functions?
or something?



declare function getkeystate lib "user32" (byval nvirtkey as long) as integer
declare function setkeyboardstate lib "user32" (lppbkeystate as byte) as long


</pre>
 
Upvote 0
Here's some alternative code covering On, Off and Toggle :
Code:
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long
Public keyCodes(0 To 255) As Byte, caps%

Sub CapsLock_Off()
If GetKeyState(20) Then keyCodes(20) = 0
caps = SetKeyboardState(keyCodes(0))
End Sub

Sub Capslock_On()
If Not GetKeyState(20) Then keyCodes(20) = 1
caps = SetKeyboardState(keyCodes(0))
End Sub

Sub CapsLock_Toggle()
If GetKeyState(20) Then
    keyCodes(20) = 0
Else
    keyCodes(20) = 1
End If
caps = SetKeyboardState(keyCodes(0))
End Sub


I added Private and it worked...

Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long
 
Upvote 0

Forum statistics

Threads
1,214,805
Messages
6,121,656
Members
449,045
Latest member
Marcus05

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top