BUG: Multiple SendKeys Statements Turn Off NumLock Key

Last reviewed: February 10, 1998
Article ID: Q179987
The information in this article applies to:
  • Microsoft Visual Basic for Applications version 5.0
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 16-bit and 32-bit, for Windows, version 4.0
  • Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0

SYMPTOMS

Executing at least two SendKeys statements in a row results in turning off the NumLock key. This problem may also affect the CapsLock and ScrollLock keys.

CAUSE

This problem deals with a nesting of capturing the keyboard state. The first SendKeys statement takes a snapshot of the keyboard state and turns off all toggles. The second SendKeys statement executes before the first one played out all keys and restored the keyboard state. So, the keyboard state is recorded again by the second SendKeys, this time with all toggles still off. Eventually, the keyboard state is restored to the later state (toggles off).

RESOLUTION

To work around this problem, do one of the following:

  • Send all the characters in a single SendKeys statement.

    -or-

  • Execute a DoEvents function between each SendKeys statement. However, depending on the complexity of the key strokes, this may not work in all cases.

    -or-

  • Determine the setting of the NumLock key prior to using SendKeys. Then, turn off the NumLock before using SendKeys. After using SendKeys, reset the NumLock to its previous setting. This is accomplished using the GetKeyboardState, keybd_event and SetKeyboardState API functions. See the REFERENCES section below for more information.

    -or-

  • Use API functions instead of SendKeys. See the REFERENCES section below for more information,

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.

  2. Add a CommandButton to Form1.

  3. Copy the following code to the Code window of Form1:

          Option Explicit
    

          Private Sub Command1_Click()
    
             SendKeys "a"
             SendKeys "b"
          End Sub
    
    

  4. On the Run menu, click Start or press the F5 key to start the program. If the NumLock light is off, turn on the NumLock light by pressing the NumLock key. Click the CommandButton and note that the NumLock light turns off.

  5. Close Visual Basic and repeat the steps above; this time adding DoEvents, as follows:

          Private Sub Command1_Click()
    
             SendKeys "a"
             DoEvents
             SendKeys "b"
          End Sub
    
       NOTE: You should restart Visual Basic before trying the DoEvents
       solution. Otherwise, the keyboard state may be set incorrectly,
       preventing any workaround attempt from being successful.
    
    

REFERENCES

"Visual Basic 5.0 Programmer's Guide to the Win32 API," by Dan Appleman Chapter 6: Hardware and System Functions

For additional information, see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q177674
   TITLE     : HOWTO: Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys
Keywords          : kbprg GnrlVB vbwin vb4win vb5all vb4all
Version           : WINDOWS:4.0,5.0
Platform          : WINDOWS
Issue type        : kbbug
Solution Type     : kbpending


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 10, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.