Question

Basically, when one types, a keydown event happens. If the key is held for more than a certain time (~1 sec) then the key is repeatedly pressed until keyup hapens. I would like to change the time it takes for the key to be automatically repressed in my c++ application. How can this be done?

Thanks

Was it helpful?

Solution

The speed at which a keypress becomes automatically recurring is controlled by Windows.

If you want to manipulate automatic recurrences of key-presses, it might be more advantageous to poll for the state of the key rather than waiting for the keydown event. It depends on how responsive you need your application to be.

This article may help you in figuring out how to query for key states: link

OTHER TIPS

You can use the SystemParametersInfo function to change the keyboard delay and refresh rate, as described in this newsgroup thread.

A simple way to handle this is to establish a buffer of time around the OnKeyDown event. Setup a timer that determines whether control passes to a secondary event handler. If the timer has expired, then it is OK to pass control. If the timer hasn't expired, then you should return and leave the event unhandled. Start the timer right before passing control to your secondary event handler.

void KeyDownHandler(...)
{
    // ...
    if (TimeLeft() <= 0)
    {
        StartTimer();
        handleKeyDown();
    }
}

A timer is better than counting duplicate events because you can't assume that a given system will have the same repeat rate set as yours.

I agree with Stuart that polling for the state of the key might work better. It depends upon what you are trying to accomplish.

Also note that this type of behavior might be highly annoying to your user - why do you need to ignore duplicates?

You might be able to tap into a Windows API but this might be controlled by the OS. Not sure... You might need to manually draw a command such as to simulate a key press multiple times after a set number of seconds after the key has been pressed.

Use SetKeySpeed api (Kernel)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top