Keyboard Input & the Win32 message loop

前端 未结 2 1398
春和景丽
春和景丽 2020-11-28 11:09

How do I handle key presses and key up events in the windows message loop? I need to be able to call two functions OnKeyUp(char c); and OnKeyDown(char c);

2条回答
  •  余生分开走
    2020-11-28 11:21

    Use char c = MapVirtualKey(param,MAPVK_VK_TO_CHAR); to convert virtual key codes to char, and process WM_KEYUP and WM_KEYDOWN and their wParams.

    if (PeekMessage (&mssg, hwnd, 0, 0, PM_REMOVE))
    {
        switch (mssg.message)
        {
            case WM_QUIT:
                PostQuitMessage (0);
                notdone = false;
                quit = true;
                break;
    
            case WM_KEYDOWN:
                WPARAM param = mssg.wParam;
                char c = MapVirtualKey (param, MAPVK_VK_TO_CHAR);
                this->p->Input ()->Keyboard ()->Listeners ()->OnKeyDown (c);
                break;
    
            case WM_KEYUP:
                WPARAM param = mssg.wParam;
                char c = MapVirtualKey (param, MAPVK_VK_TO_CHAR);
                this->p->Input ()->Keyboard ()->Listeners ()->OnKeyUp (c);
                break;
        }
        // dispatch the message
        TranslateMessage (&mssg);
        DispatchMessage (&mssg);
    }
    

提交回复
热议问题