BUG: Multiple SendKeys Statements Turn Off NumLock Key

ID: Q179987

The information in this article applies to:

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:

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 kbVBp400 kbVBp500 VB4WIN vbwin 
Version           : WINDOWS:4.0,5.0
Platform          : WINDOWS
Issue type        : kbbug
Solution Type     : kbpending

Last Reviewed: February 12, 1998