Skip to content

Commit 7ed4f88

Browse files
committed
Fix keyboard repeat delay to use real-time instead of frames
1 parent d6074b7 commit 7ed4f88

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
@@ -77,6 +77,7 @@ struct KeyboardIO
7777
UnsignedByte status; // StatusType, above
7878
UnsignedShort state; // KEY_STATE_* in KeyDefs.h
7979
UnsignedInt sequence; // sequence info from DirectX used for order
80+
UnsignedInt keyDownTimeMS; // real-time in milliseconds when key went down
8081

8182
};
8283

@@ -86,7 +87,7 @@ struct KeyboardIO
8687
class Keyboard : public SubsystemInterface
8788
{
8889

89-
enum { KEY_REPEAT_DELAY = 10 };
90+
enum { KEY_REPEAT_DELAY_MS = 333 };
9091

9192
public:
9293

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

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

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

215221
// Scan Keyboard status array for first key down
216222
// long enough to repeat
223+
UnsignedInt now = timeGetTime();
217224
for( key = 0; key < ARRAY_SIZE(m_keyStatus); key++ )
218225
{
219226

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

223-
if( (m_inputFrame - m_keyStatus[ key ].sequence) > Keyboard::KEY_REPEAT_DELAY )
230+
if( now - m_keyStatus[ key ].keyDownTimeMS > Keyboard::KEY_REPEAT_DELAY_MS )
224231
{
225232
// Add key to this frame
226233
m_keys[ index ].key = (UnsignedByte)key;
@@ -234,8 +241,9 @@ Bool Keyboard::checkKeyRepeat( void )
234241
for( index = 0; index< NUM_KEYS; index++ )
235242
m_keyStatus[ index ].sequence = m_inputFrame;
236243

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

240248
retVal = TRUE;
241249
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
@@ -77,6 +77,7 @@ struct KeyboardIO
7777
UnsignedByte status; // StatusType, above
7878
UnsignedShort state; // KEY_STATE_* in KeyDefs.h
7979
UnsignedInt sequence; // sequence info from DirectX used for order
80+
UnsignedInt keyDownTimeMS; // real-time in milliseconds when key went down
8081

8182
};
8283

@@ -86,7 +87,7 @@ struct KeyboardIO
8687
class Keyboard : public SubsystemInterface
8788
{
8889

89-
enum { KEY_REPEAT_DELAY = 10 };
90+
enum { KEY_REPEAT_DELAY_MS = 333 };
9091

9192
public:
9293

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

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

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

215221
// Scan Keyboard status array for first key down
216222
// long enough to repeat
223+
UnsignedInt now = timeGetTime();
217224
for( key = 0; key < ARRAY_SIZE(m_keyStatus); key++ )
218225
{
219226

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

223-
if( (m_inputFrame - m_keyStatus[ key ].sequence) > Keyboard::KEY_REPEAT_DELAY )
230+
if( now - m_keyStatus[ key ].keyDownTimeMS > Keyboard::KEY_REPEAT_DELAY_MS )
224231
{
225232
// Add key to this frame
226233
m_keys[ index ].key = (UnsignedByte)key;
@@ -234,8 +241,9 @@ Bool Keyboard::checkKeyRepeat( void )
234241
for( index = 0; index< NUM_KEYS; index++ )
235242
m_keyStatus[ index ].sequence = m_inputFrame;
236243

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

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

0 commit comments

Comments
 (0)