2

I'm building a c++ tetris game (not c++ .Net). I feel my controls are weird. I want to make it so that when user presses one of the arrow keys, about 10ms of holding it down will start the repeat function windows has. It is set to about 500ms by default, and it is too laggy for my game. How can I set the speed at which it changes from the keydown to the repeat keydown? Not how many times / sec it repeats.

Thanks

*what I want to do is change the repeat delay to short

In control panel in keyboard settings there is repeat rate, how do i set this?

J. Polfer
  • 12,251
  • 10
  • 54
  • 83
jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • According to http://www.gamedev.net/community/forums/topic.asp?topic_id=540095, it states that WM_KEYDOWN doesn't handle repeats but WM_CHAR does. You may already know this, but I thought I'd mention it here as they are sortof discussing this topic. – J. Polfer Sep 17 '09 at 21:30
  • This question is very similar to another one you asked: http://stackoverflow.com/questions/1429472/change-speed-of-keystroke-c – J. Polfer Sep 17 '09 at 21:32

3 Answers3

8

Typically what you would do for this is instead of reacting to the WM_CHAR message that is subject to the normal key repeat settings, you would look for WM_KEYDOWN and WM_KEYUP, and take action based on a timer that you've got running. If you set the timer to fire every 50 ms for example, then you can repeat every 50 ms and still take the first action immediately when you get the WM_KEYDOWN message.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
1

According to MSDN, it also looks like you could use the SystemParametersInfo function, and call use SPI_SETKEYBOARDSPEED, SPI_SETKEYBOARDDELAY.

J. Polfer
  • 12,251
  • 10
  • 54
  • 83
  • 5
    While it's possible to change the user's settings in this way, I would find it unacceptable for a single application to modify the way *I* like my keyboard repeat settings configured. – Greg Hewgill Sep 17 '09 at 21:38
  • Just insure old settings are reset when appilcation closes – jmasterx Sep 17 '09 at 21:45
  • 2
    Seriously, don't do this. I would be very angry if any application changed any personal settings I had. – GManNickG Sep 17 '09 at 21:47
  • @unknown: And if your application crashes before it gets a chance to do so? If the computer unexpectedly powers off? This is just a poor solution. – GManNickG Sep 17 '09 at 21:47
  • 1
    Or if I switch to another application while yours is still running? – Greg Hewgill Sep 17 '09 at 22:02
  • 1
    @ Greg, GMan - With great power comes great responsibility. Your'e just going to have to trust he'll use the least invasive solution to the users. There's hundreds of instances like this in Win32. Greg made his argument, I think its a good one. – J. Polfer Sep 18 '09 at 12:29
  • This is useful in case you are writing an application which will be used for personalizing windows settings. Not the best idea if it is just for a game. – nurettin Feb 21 '17 at 12:08
-4

void Key_Set() { DWORD old = 0;

SystemParametersInfo(SPI_GETKEYBOARDDELAY, 0, &old, 0);


SystemParametersInfo(SPI_SETKEYBOARDDELAY,0, &old, 0);

}

jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • 6
    **Don't do this.** Why should your application be so important as to change settings on my computer? Just use the proper message, as Greg has shown. – GManNickG Sep 17 '09 at 21:46
  • Thank you. If you have total control over the application this should not be used, but that's not my case. Someone made a specific application that allows scripting and this is the only way to fix it. Switch to no repetition when this reaches the bad coded blackboxed places and back after it exists them. We aren't in a perfect world. – Forestrf Apr 12 '20 at 18:40