VBA - Determine if the Caps Lock button is enabled on Password entry

Patrick Roth - Click for blog homepageWith password policies in place in Dynamics 10.0 RTM, it was easy to be able for your user to 'lock themselves out' after a mistyped password or two. 

Because Dynamics GP doesn't give you any kind of warning that the Caps Lock key is enabled, the user wouldn't necessarily notice this is the real issue and not a typo and all of a sudden you are locked out. 

With 10.0 SP2 and the change on how Dexterity/GP tries to login; this is less of a challenge but it still is possible for the user to lock themselves out of Dynamics GP because they left the Caps Lock key on. 

To solve this, I wrote some VBA code that works on the Login window in Dynamics GP.  It is somewhat advanced in that it uses the Win32 API to get the status of the keyboard buffer and then specifically checks to see if the Caps Lock key is enabled or not.  If it is, a message box warns the user that has happened and suggests turning it off.  It would be a lot cooler to get a balloontip to point to the password field (as Windows does) but I'm not sure that is possible in VBA and while pretty to see, it doesn't give any advantages to just using a simple message box.

In the code sample below, both the Win32 API declaration and the VBA code is shown for this project.  The package file that I created for my 10.0 installation is also attached to this post.  I didn't test this on other Dynamics GP versions but I would suspect that it should work at least back to 8.0.  This code should also work on any password (or otherwise) field that you choose to add it to with just minor tweaks.

Login Window Code Example

 Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long

Const VK_CAPITAL = &H14
     
Private Sub Password_AfterGotFocus()
    
    Dim CapsLockState As Boolean
    Dim error_message As String
    Dim keys(0 To 255) As Byte
    GetKeyboardState keys(0)
    
    CapsLockState = keys(VK_CAPITAL)
    If CapsLockState = True Then
       error_message = "Having Caps Lock on may cause you to enter your password incorrectly." + Chr(13) + Chr(13) + "You should press the Caps Lock key to turn it off before entering your password."
       MsgBox error_message, vbOKOnly + vbQuestion
    End If
    
End Sub

Patrick
Developer Support

// Copyright © Microsoft Corporation. All Rights Reserved.
// This code released under the terms of the
// Microsoft Public License (MS-PL, https://opensource.org/licenses/ms-pl.html.)

Login_Password_Warning.zip