Microsoft KB Archive/101125

= ACC: SendKeys Macro Action Does Not Work on Toggle Keys (1.x/2.0) =

Article ID: 101125

Article Last Modified on 5/6/2003

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition

-



This article was previously published under Q101125



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:

  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)   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  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.



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

<div class="references_section">