Setting the Windows keyboard speed

Published July 05, 2000 by Matthew Allen, posted by Myopic Rhino
Do you see issues with this article? Let us know.
Advertisement
updated: 11/9/99

If you're using Windows messages for your keyboard input in a game, you'll either have to deal with whatever repeat rate the user has set for his machine or you'll have to set it for yourself. Luckily, setting it is extremely easy, so I'll just give you the code and let you learn from that. Be sure to restore the user's values before you exit your program!

// VARIABLES
int nKBDelay; // The old keyboard delay.
DWORD nKBSpeed; // The old keyboard repeat speed.

// SET NEW SPEED

// Get the old keyboard repeat delay.
// We need this so we can restore the users settings when the program exits.
if ( SystemParametersInfo( SPI_GETKEYBOARDDELAY, 0,
&nKBDelay, 0 ) == FALSE )
return FALSE;

// Get the old keyboard repeat speed.
if ( SystemParametersInfo( SPI_GETKEYBOARDSPEED, 0,
&nKBSpeed, 0 ) == FALSE )
return FALSE;

// Set the keyboard repeat delay to the shortest possible.
// This is the number '0'. This value can be between 0 and 3.
// 0 = 250 ms delay, 3 = 1 second (1000 ms) delay.
if ( SystemParametersInfo( SPI_SETKEYBOARDDELAY, 0, NULL,
SPIF_UPDATEINIFILE ) == FALSE )
return FALSE;

// Set the keyboard repeat speed to the fastest possible.
// This is the '31' below. It can be any number between 0 and 31,
// 0 = 2.5 repetitions per second, 31 = 31 repetitions per second.
if ( SystemParametersInfo( SPI_SETKEYBOARDSPEED, 31, NULL,
SPIF_UPDATEINIFILE ) == FALSE )
return FALSE;

// RESTORE USER'S SPEED

// Reset the keyboard speeds.
if ( SystemParametersInfo( SPI_SETKEYBOARDDELAY, nKBDelay, NULL,
SPIF_UPDATEINIFILE ) == FALSE )
return FALSE;

if ( SystemParametersInfo( SPI_SETKEYBOARDSPEED, nKBSpeed, NULL,
SPIF_UPDATEINIFILE ) == FALSE )
return FALSE;
Cancel Save
0 Likes 0 Comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement