Detecting whether Caps Lock is set

  • Thread starter Thread starter Franco Turrinelli
  • Start date Start date
F

Franco Turrinelli

I wonder if anyone can help me with this:

I have written a macro that the user can invoke using a
keyboard shortcut. The issue is that I would like the
macro to do something different depending on whether the
user presses "l" or Shift-"l" (i.e. "L"). Of course, if
the Caps Lock key is on, and the user does not notice, the
actions will be reversed. I'd like to be able, in the
macro, to test whether Caps Lock is set on.

Any thoughts? Sorry if this is a total "duh" question.
 
Franco,

Try something like

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

Sub AAA()
Dim Res As Boolean
Res = GetKeyState(vbKeyCapital) And 1
Debug.Print Res 'True is CAPS LOCK, False is not
End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
For the shift key you van use this

Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Public Const VK_SHIFT As Integer = &H10 'Shift

Sub test()
If GetKeyState(VK_SHIFT) < 0 Then
MsgBox "You used the Shift key"
Else
MsgBox "You not used the shift key"
End If
End Sub
 
Thank you both, it works. Now I'll try to understand
exactly why and how . . . ;-)
 
Back
Top