BUG: Multiple SendKeys Statement Turns Off NumLock Key
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 INFORMATIONSteps to Reproduce Behavior
- Start a new Standard EXE project in Visual Basic. Form1 is created by
default.
- Add a CommandButton to Form1.
- Copy the following code to the Code window of Form1:
Option Explicit
Private Sub Command1_Click()
SendKeys "a"
SendKeys "b"
End Sub
- 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.
- 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:
Q177674 HOWTO: Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys
Additional query words:
Keywords : kbprg GnrlVb kbVBp400 kbVBp500 VB4WIN vbwin
Version : WINDOWS:4.0,5.0
Platform : WINDOWS
Issue type : kbbug
|