Microsoft KB Archive/179987

From BetaArchive Wiki

Article ID: 179987

Article Last Modified on 6/29/2004



APPLIES TO

  • Microsoft Visual Basic for Applications 5.0
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 16-bit Enterprise Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition



This article was previously published under Q179987

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:

177674 HOWTO: Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys


Keywords: kbbug kbprogramming kbpending KB179987