开发者

Need help with WM_KEYDOWN

I need help with WM_KEYDOWN, I want to remove the "pause" between the first keypress and the repeating keypresses.

If you continually hold down a button, I want the program to realize it directly.

Im trying to make a game with directx. Please tell me if I should use som开发者_如何学Goething else than WM_KEYDOWN.


This might not be exactly the behaviour you're after, but can't you simply ignore further WM_KEYDOWN messages until you receive a WM_KEYUP?


Please define “continually”. If user will press a button and release it immediately than you will get WM_KEYDOWN and WM_KEYUP event following immediately. Otherwise, if user does not release the key for some period of time, Windows will detect it using some internal timer and continue to fire WM_KEYDOWN events until a button is released.

You cannot do much about it because you have to wait some period of time in order to tell if a button is being pressed and not released.

What you can do, however, is disregard continuous WM_KEYDOWN events from Windows and treat button as being pressed and not released until you get a WM_KEYUP event. Let's call it a bet or, even better, a branch optimization.


You can use boolean variables and set them to true when a key is down. If the variable's value is true, you stop doing the action. And when the key is up, you set the variable to false.

// ...
some switch
// ...
case WM_KEYDOWN:
    if (!keydown) {
        // do the magic
        keydown=true;
    }
break;
case WM_KEYUP:
    keydown=false;
break;

Of course if you want to do something continually, you should for example set a timer in the if (!keydown) statement and stop the timer when the user releases the key.


There is a flag coming with WM_KEYDOWN indicating if a WM_KEYDOWN is the first one or the repeated ones. Just search MSDN for WM_KEYDOWN you should be able to find it.

From MSDN: lParam bit 30: Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is zero if the key is up.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜