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...