Skip to content

Commit 7608dbc

Browse files
committed
Separated logic into 'lost focus', 'regain focus' and 'on focus change'.
1 parent 6db6e98 commit 7608dbc

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ class Keyboard : public SubsystemInterface
104104
virtual void createStreamMessages( void ); /**< given state of device, create
105105
messages and put them on the
106106
stream for the raw state. */
107+
108+
virtual void loseFocus(); ///< called when window has lost focus
109+
virtual void regainFocus(); ///< called when window has regained focus
107110
// simplified versions where the caller doesn't care which key type was pressed.
108111
Bool isShift();
109112
Bool isCtrl();
@@ -119,6 +122,8 @@ class Keyboard : public SubsystemInterface
119122
WideChar translateKey( WideChar keyCode ); ///< translate key code to printable UNICODE char
120123
WideChar getPrintableKey( KeyDefType key, Int state );
121124
enum { MAX_KEY_STATES = 3};
125+
private:
126+
void changeFocus(); ///< called internally when window lost focus and regained focus
122127
protected:
123128

124129
/** get the key data for a single key, KEY_NONE should be returned when

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,37 @@ void Keyboard::update( void )
748748
void Keyboard::resetKeys( void )
749749
{
750750

751+
memset( m_keys, 0, sizeof( m_keys ) );
752+
memset( m_keyStatus, 0, sizeof( m_keyStatus ) );
753+
m_modifiers = KEY_STATE_NONE;
754+
if( getCapsState() )
755+
{
756+
m_modifiers |= KEY_STATE_CAPSLOCK;
757+
}
758+
759+
}
760+
761+
//-------------------------------------------------------------------------------------------------
762+
// called when window has lost focus
763+
//-------------------------------------------------------------------------------------------------
764+
void Keyboard::loseFocus()
765+
{
766+
changeFocus();
767+
}
768+
769+
//-------------------------------------------------------------------------------------------------
770+
// called when window has regained focus
771+
//-------------------------------------------------------------------------------------------------
772+
void Keyboard::regainFocus()
773+
{
774+
changeFocus();
775+
}
776+
777+
//-------------------------------------------------------------------------------------------------
778+
// called internally when window lost focus and regained focus
779+
//-------------------------------------------------------------------------------------------------
780+
void Keyboard::changeFocus()
781+
{
751782
// TheSuperHackers @fix Caball009 13/12/2025 Fix bug where game remains in waypoint mode
752783
// because the key up state for the alt key is not detected after alt tab.
753784
if (BitIsSet(m_keyStatus[KEY_LALT].state, KEY_STATE_DOWN))
@@ -763,14 +794,7 @@ void Keyboard::resetKeys( void )
763794
msg->appendIntegerArgument(KEY_STATE_UP);
764795
}
765796

766-
memset( m_keys, 0, sizeof( m_keys ) );
767-
memset( m_keyStatus, 0, sizeof( m_keyStatus ) );
768-
m_modifiers = KEY_STATE_NONE;
769-
if( getCapsState() )
770-
{
771-
m_modifiers |= KEY_STATE_CAPSLOCK;
772-
}
773-
797+
resetKeys();
774798
}
775799

776800
//-------------------------------------------------------------------------------------------------

GeneralsMD/Code/Main/WinMain.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,8 @@ LRESULT CALLBACK WndProc( HWND hWnd, UINT message,
419419
// ------------------------------------------------------------------------
420420
case WM_SETFOCUS:
421421
{
422-
//
423-
// reset the state of our keyboard cause we haven't been paying
424-
// attention to the keys while focus was away
425-
//
426422
if( TheKeyboard )
427-
TheKeyboard->resetKeys();
423+
TheKeyboard->regainFocus();
428424

429425
if (TheMouse)
430426
TheMouse->regainFocus();
@@ -436,7 +432,7 @@ LRESULT CALLBACK WndProc( HWND hWnd, UINT message,
436432
case WM_KILLFOCUS:
437433
{
438434
if (TheKeyboard )
439-
TheKeyboard->resetKeys();
435+
TheKeyboard->loseFocus();
440436

441437
if (TheMouse)
442438
{

0 commit comments

Comments
 (0)