ACC: SendKeys Macro Action Doesn't Work on Toggle Keys (1.x/2.0)

ID: Q101125


The information in this article applies to:


SYMPTOMS

Advanced: Requires expert coding, interoperability, and multiuser skills.

A SendKeys macro action will not work correctly on the toggle keys CAPS LOCK or SCROLL LOCK. You may see the CAPS LOCK or SCROLL LOCK lights flash on the keyboard, but the lights will not remain on and the keys will not be correctly activated after the SendKeys action is completed. Note that SendKeys also will not work correctly with the ALT+PRINT SCREEN key combination.


CAUSE

SendKeys keystrokes are sent to applications at a high level. Microsoft Windows 3.x traps toggle keys and the PRINT SCREEN key at a lower level, keeping them from your application.


RESOLUTION

NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.

There is a workaround for the CAPS LOCK problem. To force input in a field into uppercase, set the control's Format property to ">". The UCase() function can also be used to force entries in a field to uppercase.

Although a SendKeys macro action cannot toggle keys, a Windows API SetKeyboardState() function call can toggle keys. The following example demonstrates how to use the Windows API SetKeyboardState() function:

  1. Create a module and type the following line in the Declarations section:
    
          Option Explicit
    
          Declare Sub GetKeyboardState Lib "User" (ByVal lpKeyState As String)
          Declare Sub SetKeyboardState Lib "User" (ByVal lpKeyState As String) 


  2. Type the following procedure:
    
          Function SetKeys ()
          Dim keys As String
          Const VK_CAPITAL = &H14
    
          keys = Space$(256)
    
          GetKeyboardState keys
    
          If &H1 = (Asc(Mid(keys, VK_CAPITAL + 1, 1)) And &H1) Then
              keys = Left(keys, VK_CAPITAL) & Chr(0) & Right(keys, Len(keys) _
                 - (VK_CAPITAL + 1))
          Else
              keys = Left(keys, VK_CAPITAL) & Chr(1) & Right(keys, Len(keys) _
                - (VK_CAPITAL + 1))
          End If
    
          SetKeyboardState keys
    
          End Function 


  3. To test this function, type the following line in the Immediate window, and then press ENTER:
    ? SetKeys()
    Note that the CAPSLOCK key will toggle from whatever state it was in previously.



MORE INFORMATION

Steps to Reproduce Behavior

The following SendKeys action will not toggle the CAPS LOCK key to on:

   MacroName       Action
   ----------------------
   Macro1          SendKeys


   Macro1 Actions
   --------------------------
   SendKeys

      Keystrokes:  {CAPSLOCK}
      Wait:        NO 


REFERENCES

For more information about SendKeys, search for "SendKeys Statement" using the Microsoft Access Help Menu.

For more information about API functions, search for "API" using the Microsoft Access Help menu.

Additional query words: send key num


Keywords          : kbusage McrOthr 
Version           : 1.0 1.1 2.0
Platform          : WINDOWS 
Issue type        : kbprb 

Last Reviewed: March 25, 1999