how to detect same keyboard key press only once

后端 未结 1 702
情歌与酒
情歌与酒 2021-01-28 18:34

I am designing a keyboard class that can detect the keyboard key press only one time but I still cannot figure out the way to do it. My goal is just check and perform the action

相关标签:
1条回答
  • 2021-01-28 19:14

    Bit 30 of the lParam value for the WM_KEYDOWN message indicates whether or not the key was previously down when the message was generated. You can use this to distinguish between the actual keypress and any subsequent key repeats.

    case WM_KEYDOWN:
        if (lParam & (1 << 30))
        {
            // this is a repeat
        }
        else
        {
            // first press
        }
        break;
    

    But if, as it seems, you're trying to check in realtime which keys are down, you can do this using the GetAsyncKeyState() function rather than relying on tracking keystate via your message loop.

    0 讨论(0)
提交回复
热议问题