BUG: First WM_KEYDOWN Has Incorrect Previous Key State (240687)



The information in this article applies to:

  • Microsoft Platform Software Development Kit (SDK) 1.0, when used with:
    • the operating system: Microsoft Windows 95
    • the operating system: Microsoft Windows 98

This article was previously published under Q240687

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 that this is a problem in Windows 95 and Windows 98.

Modification Type:MinorLast Reviewed:7/11/2005
Keywords:kbbug kbfix KB240687