简体   繁体   中英

C++ WM_KEYDOWN different intervals

I am currently trying to get KeyBoard Input from the WM_KEYDOWN and WM_CHAR message for my own InputBox.

This is the code that I am using for basic input, which works fine for characters:

if(msg.message == WM_KEYDOWN)
{
    keyHandled = false;
    //handle other keys here, e.g. VK_LEFT
}
else if(msg.message == WM_CHAR && !keyHandled)
{
    keyHandled = true;
    gui->UpdateInput(msg.wParam);
}

If the key that is being pressed is also a key that triggers the WM_CHAR message, the interval is as in usual input boxes.
However, if it is a key like VK_LEFT , it keeps receiving the WM_KEYDOWN message without any delay.

Is there any way that I can receive all keys with the same interval or do I have to implement a timer that handles the delay between the messages? I have also had a look at the WM_KEYDOWN message on msdn, but I could not find anything related to the intervals.

Windows has its own delays for sending Keyboard events from Keyboard input and this isn't something you can simply change. As you know, holding down a key will result in a first message, a delay, and then a series of more messages with a quicker interval. You can get around this by requesting states rather than relying on the messages directly. This is called Unbuffered input and is state oriented. To store your states, you can do the following:

bool keys[256];

When you are checking windows events, you can update the key states accordingly like this:

//In your WinProc function most likely
if (msg.message == WM_KEYDOWN)
{
    keys[msg.wParam] = true;
}

if (msg.message == WM_KEYUP)
{
    keys[msg.wParam] = false;
}

Then whenever you'd like, you can request the state of a specific key through the following function:

bool getKeyPressed(char keycode)
{
    return keys[keycode];
}

For example:

//This could be in some random update function and called
//whenever you need the information.
if (getKeyPressed(VK_UP))
{
//Do something here...
}

The above function can be used wherever you'd like, therefore the frequency at which you update is completely up to you at that point. As mentioned before, this is Unbuffered input and is State oriented, whereas Buffered Input is Event oriented.

I think that this internal delay can not be modified easily. One approach could be writing your own general key handler that keeps track of the states of all keys. For example, a list containg the keycodes of all pressed keys. On WM_KEYDOWN you add the keycode to the list and on WM_KEYUP you remove it. Then create something like a timer that simply notifies you in the desired delay times and calls your key handling function for each element of the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM