0

Part of the code:

LRESULT CALLBACK WindowProc(
    _In_ HWND   hwnd,
    _In_ UINT   uMsg,
    _In_ WPARAM wParam,
    _In_ LPARAM lParam)
{ ...
    switch(uMsg)
    { ...
        case WM_SYSKEYDOWN:
        case WM_SYSKEYUP:
        case WM_KEYDOWN:
        case WM_KEYUP:
        {
            uint8_t VKCODE = wParam;
            bool WasDown = ((lParam & (1<<30)) != 0);
            bool IsDown = ((lParam & (1<<31)) == 0);
            if(WasDown != IsDown)
            {
                if(VKCode == 'W')
                {
                    OutputDebugStringA("W: ");
                    if(WasDown)
                    {
                        OutputDebugStringA("WasDown ");
                    }
                    if(IsDown)
                    {
                        OutputDebugStringA("IsDown ");
                    }
                    OutputDebugStringA("\n");
                }
                else if(VKCode == 'A')
                {
                    OutputDebugStringA("A\n");
                }
                ...
            }
        } break;
        ...
    }
    ...
}

1) First, when I press 'A', will always output 2 'A's.

2) Second, according to this page: https://learn.microsoft.com/en-us/windows/desktop/inputdev/wm-keyup

30 The previous key state. The value is always 1 for a WM_KEYUP message.

31 The transition state. The value is always 1 for a WM_KEYUP message.

then (lParam & (1<<31)) will always be non-zero, so IsDown will always be false, but when I press(hit and release) 'W' it will output:

IsDown
WasDown

How come it will output "IsDown" if bool IsDown is always false?

3) If I comment out 3 cases before WM_KEYUP like this:

// case WM_SYSKEYDOWN:
// case WM_SYSKEYUP:
// case WM_KEYDOWN:

then when I press 'A' will only output one 'A', when I press 'W' will only output "WasDown", never "IsDown". I didn't wrote any code under previous 3 cases before comment them out anyway, what happened?

BTW, I'm following this video if anyone wants to see the full code:

https://www.youtube.com/watch?v=J3y1x54vyIQ

oNion
  • 125
  • 3
  • 9
  • 5
    From what you say under point 3 it sounds like you don't quite understand how `case` statements work in C/C++. The way you have your code structured, all four messages will be processed the same way. As far as #2 goes, bit 30 and 31 are always 1 for `WM_KEYUP` but for `WM_KEYDOWN` bit 30 can be 0 or 1 and bit 31 is always 0. – Jonathan Potter Mar 28 '19 at 10:38
  • @JonathanPotter So when I press a key, WindowProc process the message for WM_KEYDOWN and WM_KEYUP with the same block of code under case KEY_UP? – oNion Mar 28 '19 at 10:44
  • 3
    Yes, because when you have multiple `case` statements without a `break` in between them, control will flow through them from the matching `case` to the next `break`. – Jonathan Potter Mar 28 '19 at 10:48
  • 1
    https://cboard.cprogramming.com/cplusplus-programming/170137-jumping-into-cplusplus-chapter-7-activity-2-twelve-days-christmas.html – David Heffernan Mar 28 '19 at 10:52
  • @JonathanPotter Thank you, now I get it – oNion Mar 28 '19 at 10:58
  • 1
    @oNion general advice: before trying to make Windows programs you should get familiar with the basics of the C language. – Jabberwocky Mar 28 '19 at 12:41

0 Answers0