Skip to content

Commit fdd2f87

Browse files
committed
Fix keyboard repeat delay to use real-time instead of frames
1 parent 9d5a1a2 commit fdd2f87

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

Generals/Code/GameEngine/Include/GameClient/Keyboard.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ struct KeyboardIO
7575
UnsignedByte status; // StatusType, above
7676
UnsignedShort state; // KEY_STATE_* in KeyDefs.h
7777
UnsignedInt sequence; // sequence info from DirectX used for order
78+
UnsignedInt keyDownTimeMS; // real-time in milliseconds when key went down
7879

7980
};
8081

@@ -84,7 +85,7 @@ struct KeyboardIO
8485
class Keyboard : public SubsystemInterface
8586
{
8687

87-
enum { KEY_REPEAT_DELAY = 10 };
88+
enum { KEY_REPEAT_DELAY_MS = 333 };
8889

8990
public:
9091

Generals/Code/GameEngine/Source/GameClient/Input/Keyboard.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ void Keyboard::updateKeys( void )
141141
m_keyStatus[ m_keys[ index ].key ].status = m_keys[ index ].status;
142142
m_keyStatus[ m_keys[ index ].key ].sequence = m_inputFrame;
143143

144+
// Update key down time for new key presses
145+
if( BitIsSet( m_keys[ index ].state, KEY_STATE_DOWN ) )
146+
{
147+
m_keyStatus[ m_keys[ index ].key ].keyDownTimeMS = timeGetTime();
148+
}
149+
144150
// prevent ALT-TAB from causing a TAB event
145151
if( m_keys[ index ].key == KEY_TAB )
146152
{
@@ -213,13 +219,14 @@ Bool Keyboard::checkKeyRepeat( void )
213219

214220
// Scan Keyboard status array for first key down
215221
// long enough to repeat
222+
UnsignedInt now = timeGetTime();
216223
for( key = 0; key < 256; key++ )
217224
{
218225

219226
if( BitIsSet( m_keyStatus[ key ].state, KEY_STATE_DOWN ) )
220227
{
221228

222-
if( (m_inputFrame - m_keyStatus[ key ].sequence) > Keyboard::KEY_REPEAT_DELAY )
229+
if( now - m_keyStatus[ key ].keyDownTimeMS > Keyboard::KEY_REPEAT_DELAY_MS )
223230
{
224231
// Add key to this frame
225232
m_keys[ index ].key = (UnsignedByte)key;
@@ -233,8 +240,9 @@ Bool Keyboard::checkKeyRepeat( void )
233240
for( index = 0; index< NUM_KEYS; index++ )
234241
m_keyStatus[ index ].sequence = m_inputFrame;
235242

236-
// Set repeated key so it will repeat again in two frames
237-
m_keyStatus[ key ].sequence = m_inputFrame - (Keyboard::KEY_REPEAT_DELAY + 2);
243+
// Set repeated key so it will repeat again after a short interval
244+
const UnsignedInt KEY_REPEAT_INTERVAL_MS = 67; // ~2 frames at 30 FPS
245+
m_keyStatus[ key ].keyDownTimeMS = now - (Keyboard::KEY_REPEAT_DELAY_MS - KEY_REPEAT_INTERVAL_MS);
238246

239247
retVal = TRUE;
240248
break; // exit for key

GeneralsMD/Code/GameEngine/Include/GameClient/Keyboard.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ struct KeyboardIO
7575
UnsignedByte status; // StatusType, above
7676
UnsignedShort state; // KEY_STATE_* in KeyDefs.h
7777
UnsignedInt sequence; // sequence info from DirectX used for order
78+
UnsignedInt keyDownTimeMS; // real-time in milliseconds when key went down
7879

7980
};
8081

@@ -84,7 +85,7 @@ struct KeyboardIO
8485
class Keyboard : public SubsystemInterface
8586
{
8687

87-
enum { KEY_REPEAT_DELAY = 10 };
88+
enum { KEY_REPEAT_DELAY_MS = 333 };
8889

8990
public:
9091

GeneralsMD/Code/GameEngine/Source/GameClient/Input/Keyboard.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ void Keyboard::updateKeys( void )
141141
m_keyStatus[ m_keys[ index ].key ].status = m_keys[ index ].status;
142142
m_keyStatus[ m_keys[ index ].key ].sequence = m_inputFrame;
143143

144+
// Update key down time for new key presses
145+
if( BitIsSet( m_keys[ index ].state, KEY_STATE_DOWN ) )
146+
{
147+
m_keyStatus[ m_keys[ index ].key ].keyDownTimeMS = timeGetTime();
148+
}
149+
144150
// prevent ALT-TAB from causing a TAB event
145151
if( m_keys[ index ].key == KEY_TAB )
146152
{
@@ -213,13 +219,14 @@ Bool Keyboard::checkKeyRepeat( void )
213219

214220
// Scan Keyboard status array for first key down
215221
// long enough to repeat
222+
UnsignedInt now = timeGetTime();
216223
for( key = 0; key < 256; key++ )
217224
{
218225

219226
if( BitIsSet( m_keyStatus[ key ].state, KEY_STATE_DOWN ) )
220227
{
221228

222-
if( (m_inputFrame - m_keyStatus[ key ].sequence) > Keyboard::KEY_REPEAT_DELAY )
229+
if( now - m_keyStatus[ key ].keyDownTimeMS > Keyboard::KEY_REPEAT_DELAY_MS )
223230
{
224231
// Add key to this frame
225232
m_keys[ index ].key = (UnsignedByte)key;
@@ -233,8 +240,9 @@ Bool Keyboard::checkKeyRepeat( void )
233240
for( index = 0; index< NUM_KEYS; index++ )
234241
m_keyStatus[ index ].sequence = m_inputFrame;
235242

236-
// Set repeated key so it will repeat again in two frames
237-
m_keyStatus[ key ].sequence = m_inputFrame - (Keyboard::KEY_REPEAT_DELAY + 2);
243+
// Set repeated key so it will repeat again after a short interval
244+
const UnsignedInt KEY_REPEAT_INTERVAL_MS = 67; // ~2 frames at 30 FPS
245+
m_keyStatus[ key ].keyDownTimeMS = now - (Keyboard::KEY_REPEAT_DELAY_MS - KEY_REPEAT_INTERVAL_MS);
238246

239247
retVal = TRUE;
240248
break; // exit for key

0 commit comments

Comments
 (0)