BUG: First WM_KEYDOWN Has Incorrect Previous Key State

ID: Q240687


The information in this article applies to:
  • Microsoft Win32 Software Development Kit (SDK)
    on the following platforms: Win95, Win98


SYMPTOMS

After switching from one application to another using Alt-Tab, or Alt-Esc, the first WM_KEYDOWN message received by the application may have bit 30 incorrectly set, indicating the previous key state was down.

This situation can occur if the first key pressed in the current application is the same key as the last key pressed and released in the previous application. Also, the mouse cursor must NOT be over the current application; if it is, the problem does not occur.

Note that the lParam repeat count (bits 0-15) indicate a 1 when the problem occurs. If the application swap is performed with Alt-Tab, then the Tab key must be released before the Alt key. Under Alt-Esc, the Alt key must be released first for the problem to occur. Thus, normal Alt-Tab operations often elicit the problem whereas Alt-Esc usually works properly.

In addition, if the application swap is performed by clicking into a new window with the mouse, and a key is held down, it is valid for the first key press detected in the window that has just gotten the focus to receive WM_KEYDOWN messages with the lParam previous state bit set. The following code sample illustrates a simple method to avoid the problem:


#define	REPEAT_MASK     ~(0x40000000)
BOOL	fGotFocus = FALSE;

LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam, LPARAM lParam)
{
   switch (message) 
   {
      case WM_SETFOCUS:
         fGotFocus = TRUE;
         break;

      case WM_KEYDOWN:
         if ( fGotFocus )
         {
            fGotFocus = FALSE;
            lParam &= REPEAT_MASK;
         }
         // continue processing... 


CAUSE

Event processing during foreground application swapping initiated by an Alt-key operation does not clear the last pressed and released key state when synchronizing application key state tracking data with the message queue under the conditions described above.


STATUS

Microsoft has confirmed this to be a problem in Windows 95 and Windows 98.

Additional query words: kbGrpUser

Keywords :
Version : WINDOWS:
Platform : WINDOWS
Issue type : kbbug


Last Reviewed: December 4, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.