vba to alert user that the CAPS LOCK is on

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
679
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have some code that creates an input box for inputting a password and if the password is incorrect a message box is created (see code below). It would be good to have some additional vba that detects at this point whether the CAPS LOCK is on and then give a second message to the user letting them know that the CAPS LOCK is 'on'. Can this be achieved?

Any help much appreciated.

VBA Code:
If Len(sErrors) > 0 Then MsgBox "Incorrect password for:" & sErrors, vbOKOnly
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Ok, have found a solution...
Insert module:
VBA Code:
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Long
Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long

Function CapsLock() As Boolean
Dim Res As Long
Dim KBState(0 To 255) As Byte
Res = GetKeyboardState(KBState(0))
CapsLock = KBState(&H14) And 1
End Function

Then use this code:
Code:
If CapsLock = True Then MsgBox "Caps is on"

If anyone can spot any potential issues with this then please let me know.
Thanks to anyone who put any time to this.
 
Upvote 0
Simpler option would be to place the onus on the user and amend the message ...
If Len(sErrors) > 0 Then MsgBox "Incorrect password for: " & sErrors & vbCr & "Is CapsLock on ?", vbOKOnly

If anyone can spot any potential issues with this then please let me know
Read this
 
Upvote 0
Member in thread below is having problem making 32bit code work with 64bit Office
You may be interested in watching how it develops ...
 
Upvote 0
Thanks Yongle, I did think about the simple method you've described above by adding the 'is CapsLock on' but thought the solution I've got looks a bit slicker to the end user, however, when I saw the 32bit part of the code it did ring alarm bells, hence my comments about running into problems, so many thanks for the link that's just the type of thing I need to watch :)
 
Upvote 0

Forum statistics

Threads
1,214,432
Messages
6,119,468
Members
448,900
Latest member
Fairooza

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