Num Lock Problem

Sunday, March 03, 2002 14:05:01
home

Derived from Microsoft Knowledge Base Articles Q179987 and Q177674

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,