Keypress event in C++

我的未来我决定 提交于 2019-12-01 13:35:31

Avoid using a keyboard related message like WM_KEYDOWN or WM_CHAR to detect a key, Windows repeats WM_KEYDOWN when the user holds it down. You simply need a your own bool flag that keeps track of the state of the key. Only change your game object state when you see a difference between the state as reported by GetAsyncKeyState() and this flag. Roughly:

bool movingLeft = false;
...
if ((GetAsyncKeyState(VK_LEFT) < 0) != movingLeft) {
    movingLeft = !movingLeft;
    gameObject->setVelocity(movingLeft ? -10 : 0);
}

Use KeyPress events (or KeyUp).

Btw according to MSDN SHORT WINAPI GetAsyncKeyState(__in int vKey);

Determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.

It doesn't say anything about detecting a keydown event.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!