How can Excel vba detect if Caps Lock or Num Lock is on?

  • Thread starter Thread starter abc
  • Start date Start date
Here's one way. I'm sure there are others.

'put these declarations at the top of the module
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long)
As Integer
Private Const kCapital = 20
Private Const kNumlock = 144

Public Function CapsLock() As Boolean
CapsLock = KeyState(kCapital)
End Function

Public Function NumLock() As Boolean
NumLock = KeyState(kNumlock)
End Function

Private Function KeyState(lKey As Long) As Boolean
KeyState = CBool(GetKeyState(lKey))
End Function

Robin Hammond
www.enhanceddatasystems.com
 
It works! Thank you so much ^^

Robin Hammond said:
Here's one way. I'm sure there are others.

'put these declarations at the top of the module
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long)
As Integer
Private Const kCapital = 20
Private Const kNumlock = 144

Public Function CapsLock() As Boolean
CapsLock = KeyState(kCapital)
End Function

Public Function NumLock() As Boolean
NumLock = KeyState(kNumlock)
End Function

Private Function KeyState(lKey As Long) As Boolean
KeyState = CBool(GetKeyState(lKey))
End Function

Robin Hammond
www.enhanceddatasystems.com
 
Back
Top