Setting the Windows keyboard speed
by Matthew Allen
MXF Entertainment
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;

Discuss this article in the forums


Date this article was posted to GameDev.net: 7/5/2000
(Note that this date does not necessarily correspond to the date the article was written)

See Also:
Windows

© 1999-2011 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
Comments? Questions? Feedback? Click here!