Skip to content

Commit b6c1c54

Browse files
committed
tweak(keyboard): Apply minor improvements to Keyboard class to query pressed keys (#1926)
1 parent d81e743 commit b6c1c54

File tree

10 files changed

+100
-60
lines changed

10 files changed

+100
-60
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
/** The key tables */
103103
//=============================================================================
104104

105-
enum KeyDefType CPP_11(: Int)
105+
enum KeyDefType CPP_11(: UnsignedByte)
106106
{
107107
// keypad keys ----------------------------------------------------------------
108108
KEY_KP0 = DIK_NUMPAD0,
@@ -224,10 +224,15 @@ enum KeyDefType CPP_11(: Int)
224224

225225
// specials -------------------------------------------------------------------
226226
KEY_NONE = 0x00, ///< to report end of key stream
227-
KEY_LOST = 0xFF ///< to report lost keyboard focus
227+
KEY_LOST = 0xFF, ///< to report lost keyboard focus
228228

229229
};
230230

231+
enum
232+
{
233+
KEY_COUNT = 256
234+
};
235+
231236
// state for keyboard IO ------------------------------------------------------
232237
enum
233238
{

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@
6565
struct KeyboardIO
6666
{
6767

68-
enum StatusType
68+
enum StatusType CPP_11(: UnsignedByte)
6969
{
7070
STATUS_UNUSED = 0x00, // Key has not been used
7171
STATUS_USED = 0x01 // Key has been eaten
7272
};
7373

74-
UnsignedByte key; // key data
74+
void setUsed() { status = STATUS_USED; }
75+
76+
UnsignedByte key; // KeyDefType, key data
7577
UnsignedByte status; // StatusType, above
7678
UnsignedShort state; // KEY_STATE_* in KeyDefs.h
7779
UnsignedInt sequence; // sequence info from DirectX used for order
@@ -91,7 +93,7 @@ class Keyboard : public SubsystemInterface
9193
Keyboard( void );
9294
virtual ~Keyboard( void );
9395

94-
// you may extend the functionanilty of these for your device
96+
// you may extend the functionality of these for your device
9597
virtual void init( void ); /**< initialize the keyboard, only extend this
9698
functionality, do not replace */
9799
virtual void reset( void ); ///< Reset keyboard system
@@ -111,10 +113,11 @@ class Keyboard : public SubsystemInterface
111113
// access methods for key data
112114
void resetKeys( void ); ///< reset the state of the keys
113115
KeyboardIO *getFirstKey( void ); ///< get first key ready for processing
114-
void setKeyStatusData( UnsignedByte key,
116+
KeyboardIO *findKey( KeyDefType key, KeyboardIO::StatusType status ); ///< get key ready for processing, can return NULL
117+
void setKeyStatusData( KeyDefType key,
115118
KeyboardIO::StatusType data ); ///< set key status
116-
WideChar translateKey( WideChar keyCode ); ///< translte key code to printable UNICODE char
117-
WideChar getPrintableKey( UnsignedByte key, Int state );
119+
WideChar translateKey( WideChar keyCode ); ///< translate key code to printable UNICODE char
120+
WideChar getPrintableKey( KeyDefType key, Int state );
118121
enum { MAX_KEY_STATES = 3};
119122
protected:
120123

@@ -126,10 +129,10 @@ class Keyboard : public SubsystemInterface
126129
void initKeyNames( void ); ///< initialize the key names table
127130
void updateKeys( void ); ///< update the state of our key data
128131
Bool checkKeyRepeat( void ); ///< check for repeating keys
129-
UnsignedByte getKeyStatusData( UnsignedByte key ); ///< get key status
130-
Bool getKeyStateBit( UnsignedByte key, Int bit ); ///< get key state bit
131-
UnsignedInt getKeySequenceData( UnsignedByte key ); ///< get key sequence
132-
void setKeyStateData( UnsignedByte key, UnsignedByte data ); ///< get key state
132+
UnsignedByte getKeyStatusData( KeyDefType key ); ///< get key status
133+
Bool getKeyStateBit( KeyDefType key, Int bit ); ///< get key state bit
134+
UnsignedInt getKeySequenceData( KeyDefType key ); ///< get key sequence
135+
void setKeyStateData( KeyDefType key, UnsignedByte data ); ///< get key state
133136

134137
UnsignedShort m_modifiers;
135138
// internal keyboard data members
@@ -142,21 +145,20 @@ class Keyboard : public SubsystemInterface
142145
//Bool m_rControlState; // 1 if right control is down
143146
//Bool m_lAltState; // 1 if left alt is down
144147
//Bool m_rAltState; // 1 if right alt is down
145-
UnsignedByte m_shift2Key; // what key is the secondary shift key
148+
KeyDefType m_shift2Key; // what key is the secondary shift key
146149

147150
enum { NUM_KEYS = 256 };
148151
KeyboardIO m_keys[ NUM_KEYS ]; ///< the keys
149-
KeyboardIO m_keyStatus[ NUM_KEYS ]; ///< the key status flags
152+
KeyboardIO m_keyStatus[ KEY_COUNT ]; ///< the key status flags
150153

151-
enum { KEY_NAMES_COUNT = 256 };
152154
struct
153155
{
154156

155157
WideChar stdKey;
156158
WideChar shifted;
157159
WideChar shifted2;
158160

159-
} m_keyNames[ KEY_NAMES_COUNT ];
161+
} m_keyNames[ KEY_COUNT ];
160162
UnsignedInt m_inputFrame; ///< frame input was gathered on
161163

162164
};

Generals/Code/GameEngine/Source/GameClient/GUI/Gadget/GadgetListBox.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ WindowMsgHandledType GadgetListBoxInput( GameWindow *window, UnsignedInt msg,
727727
continue;
728728
for(j = 0; j < TheKeyboard->MAX_KEY_STATES; ++j)
729729
{
730-
if(dString->getText().getCharAt(0) == TheKeyboard->getPrintableKey(mData1, j))
730+
if(dString->getText().getCharAt(0) == TheKeyboard->getPrintableKey((KeyDefType)mData1, j))
731731
{
732732
list->selectPos = position;
733733
Int prevPos = getListboxTopEntry(list);

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

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Keyboard *TheKeyboard = NULL;
5050
void Keyboard::createStreamMessages( void )
5151
{
5252

53-
// santiy
53+
// sanity
5454
if( TheMessageStream == NULL )
5555
return;
5656

@@ -89,6 +89,7 @@ void Keyboard::createStreamMessages( void )
8989
}
9090

9191
// next key please
92+
key->setUsed();
9293
key++;
9394

9495
}
@@ -213,7 +214,7 @@ Bool Keyboard::checkKeyRepeat( void )
213214

214215
// Scan Keyboard status array for first key down
215216
// long enough to repeat
216-
for( key = 0; key < 256; key++ )
217+
for( key = 0; key < ARRAY_SIZE(m_keyStatus); key++ )
217218
{
218219

219220
if( BitIsSet( m_keyStatus[ key ].state, KEY_STATE_DOWN ) )
@@ -261,7 +262,7 @@ void Keyboard::initKeyNames( void )
261262
/*
262263
* Initialize the keyboard key-names array.
263264
*/
264-
for( i = 0; i < KEY_NAMES_COUNT; i++ )
265+
for( i = 0; i < ARRAY_SIZE(m_keyNames); i++ )
265266
{
266267

267268
m_keyNames[ i ].stdKey = L'\0';
@@ -765,42 +766,56 @@ KeyboardIO *Keyboard::getFirstKey( void )
765766
return &m_keys[ 0 ];
766767
}
767768

769+
//-------------------------------------------------------------------------------------------------
770+
//-------------------------------------------------------------------------------------------------
771+
KeyboardIO *Keyboard::findKey( KeyDefType key, KeyboardIO::StatusType status )
772+
{
773+
for (KeyboardIO *io = getFirstKey(); io->key != KEY_NONE; ++io)
774+
{
775+
if (io->key == key && io->status == status)
776+
{
777+
return io;
778+
}
779+
}
780+
return NULL;
781+
}
782+
768783
//-------------------------------------------------------------------------------------------------
769784
/** return the key status for the specified key */
770785
//-------------------------------------------------------------------------------------------------
771-
UnsignedByte Keyboard::getKeyStatusData( UnsignedByte key )
786+
UnsignedByte Keyboard::getKeyStatusData( KeyDefType key )
772787
{
773788
return m_keyStatus[ key ].status;
774789
}
775790

776791
//-------------------------------------------------------------------------------------------------
777792
/** Get the key state data as a Bool for the specified key */
778793
//-------------------------------------------------------------------------------------------------
779-
Bool Keyboard::getKeyStateBit( UnsignedByte key, Int bit )
794+
Bool Keyboard::getKeyStateBit( KeyDefType key, Int bit )
780795
{
781796
return (m_keyStatus[ key ].state & bit) ? 1 : 0;
782797
}
783798

784799
//-------------------------------------------------------------------------------------------------
785800
/** return the sequence data for the given key */
786801
//-------------------------------------------------------------------------------------------------
787-
UnsignedInt Keyboard::getKeySequenceData( UnsignedByte key )
802+
UnsignedInt Keyboard::getKeySequenceData( KeyDefType key )
788803
{
789804
return m_keyStatus[ key ].sequence;
790805
}
791806

792807
//-------------------------------------------------------------------------------------------------
793808
/** set the key status data */
794809
//-------------------------------------------------------------------------------------------------
795-
void Keyboard::setKeyStatusData( UnsignedByte key, KeyboardIO::StatusType data )
810+
void Keyboard::setKeyStatusData( KeyDefType key, KeyboardIO::StatusType data )
796811
{
797812
m_keyStatus[ key ].status = data;
798813
}
799814

800815
//-------------------------------------------------------------------------------------------------
801816
/** set the key state data */
802817
//-------------------------------------------------------------------------------------------------
803-
void Keyboard::setKeyStateData( UnsignedByte key, UnsignedByte data )
818+
void Keyboard::setKeyStateData( KeyDefType key, UnsignedByte data )
804819
{
805820
m_keyStatus[ key ].state = data;
806821
}
@@ -973,16 +988,14 @@ Bool Keyboard::isAlt()
973988
}
974989

975990

976-
WideChar Keyboard::getPrintableKey( UnsignedByte key, Int state )
991+
WideChar Keyboard::getPrintableKey( KeyDefType key, Int state )
977992
{
978-
if((key < 0 || key >=KEY_NAMES_COUNT) || ( state < 0 || state >= MAX_KEY_STATES))
993+
if((key < 0 || key >= ARRAY_SIZE(m_keyNames)) || ( state < 0 || state >= MAX_KEY_STATES))
979994
return L'\0';
980995
if(state == 0)
981996
return m_keyNames[key].stdKey;
982997
else if(state == 1)
983998
return m_keyNames[key].shifted;
984999
else
9851000
return m_keyNames[key].shifted2;
986-
987-
9881001
}

Generals/Code/GameEngine/Source/GameClient/MessageStream/HotKey.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ GameMessageDisposition HotKeyTranslator::translateGameMessage(const GameMessage
9898
}
9999
if(newModState != 0)
100100
return disp;
101-
WideChar key = TheKeyboard->getPrintableKey(msg->getArgument(0)->integer, 0);
101+
WideChar key = TheKeyboard->getPrintableKey((KeyDefType)msg->getArgument(0)->integer, 0);
102102
UnicodeString uKey;
103103
uKey.concat(key);
104104
AsciiString aKey;

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
/** The key tables */
103103
//=============================================================================
104104

105-
enum KeyDefType CPP_11(: Int)
105+
enum KeyDefType CPP_11(: UnsignedByte)
106106
{
107107
// keypad keys ----------------------------------------------------------------
108108
KEY_KP0 = DIK_NUMPAD0,
@@ -224,10 +224,15 @@ enum KeyDefType CPP_11(: Int)
224224

225225
// specials -------------------------------------------------------------------
226226
KEY_NONE = 0x00, ///< to report end of key stream
227-
KEY_LOST = 0xFF ///< to report lost keyboard focus
227+
KEY_LOST = 0xFF, ///< to report lost keyboard focus
228228

229229
};
230230

231+
enum
232+
{
233+
KEY_COUNT = 256
234+
};
235+
231236
// state for keyboard IO ------------------------------------------------------
232237
enum
233238
{

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@
6565
struct KeyboardIO
6666
{
6767

68-
enum StatusType
68+
enum StatusType CPP_11(: UnsignedByte)
6969
{
7070
STATUS_UNUSED = 0x00, // Key has not been used
7171
STATUS_USED = 0x01 // Key has been eaten
7272
};
7373

74-
UnsignedByte key; // key data
74+
void setUsed() { status = STATUS_USED; }
75+
76+
UnsignedByte key; // KeyDefType, key data
7577
UnsignedByte status; // StatusType, above
7678
UnsignedShort state; // KEY_STATE_* in KeyDefs.h
7779
UnsignedInt sequence; // sequence info from DirectX used for order
@@ -91,7 +93,7 @@ class Keyboard : public SubsystemInterface
9193
Keyboard( void );
9294
virtual ~Keyboard( void );
9395

94-
// you may extend the functionanilty of these for your device
96+
// you may extend the functionality of these for your device
9597
virtual void init( void ); /**< initialize the keyboard, only extend this
9698
functionality, do not replace */
9799
virtual void reset( void ); ///< Reset keyboard system
@@ -111,10 +113,11 @@ class Keyboard : public SubsystemInterface
111113
// access methods for key data
112114
void resetKeys( void ); ///< reset the state of the keys
113115
KeyboardIO *getFirstKey( void ); ///< get first key ready for processing
114-
void setKeyStatusData( UnsignedByte key,
116+
KeyboardIO *findKey( KeyDefType key, KeyboardIO::StatusType status ); ///< get key ready for processing, can return NULL
117+
void setKeyStatusData( KeyDefType key,
115118
KeyboardIO::StatusType data ); ///< set key status
116-
WideChar translateKey( WideChar keyCode ); ///< translte key code to printable UNICODE char
117-
WideChar getPrintableKey( UnsignedByte key, Int state );
119+
WideChar translateKey( WideChar keyCode ); ///< translate key code to printable UNICODE char
120+
WideChar getPrintableKey( KeyDefType key, Int state );
118121
enum { MAX_KEY_STATES = 3};
119122
protected:
120123

@@ -126,10 +129,10 @@ class Keyboard : public SubsystemInterface
126129
void initKeyNames( void ); ///< initialize the key names table
127130
void updateKeys( void ); ///< update the state of our key data
128131
Bool checkKeyRepeat( void ); ///< check for repeating keys
129-
UnsignedByte getKeyStatusData( UnsignedByte key ); ///< get key status
130-
Bool getKeyStateBit( UnsignedByte key, Int bit ); ///< get key state bit
131-
UnsignedInt getKeySequenceData( UnsignedByte key ); ///< get key sequence
132-
void setKeyStateData( UnsignedByte key, UnsignedByte data ); ///< get key state
132+
UnsignedByte getKeyStatusData( KeyDefType key ); ///< get key status
133+
Bool getKeyStateBit( KeyDefType key, Int bit ); ///< get key state bit
134+
UnsignedInt getKeySequenceData( KeyDefType key ); ///< get key sequence
135+
void setKeyStateData( KeyDefType key, UnsignedByte data ); ///< get key state
133136

134137
UnsignedShort m_modifiers;
135138
// internal keyboard data members
@@ -142,21 +145,20 @@ class Keyboard : public SubsystemInterface
142145
//Bool m_rControlState; // 1 if right control is down
143146
//Bool m_lAltState; // 1 if left alt is down
144147
//Bool m_rAltState; // 1 if right alt is down
145-
UnsignedByte m_shift2Key; // what key is the secondary shift key
148+
KeyDefType m_shift2Key; // what key is the secondary shift key
146149

147150
enum { NUM_KEYS = 256 };
148151
KeyboardIO m_keys[ NUM_KEYS ]; ///< the keys
149-
KeyboardIO m_keyStatus[ NUM_KEYS ]; ///< the key status flags
152+
KeyboardIO m_keyStatus[ KEY_COUNT ]; ///< the key status flags
150153

151-
enum { KEY_NAMES_COUNT = 256 };
152154
struct
153155
{
154156

155157
WideChar stdKey;
156158
WideChar shifted;
157159
WideChar shifted2;
158160

159-
} m_keyNames[ KEY_NAMES_COUNT ];
161+
} m_keyNames[ KEY_COUNT ];
160162
UnsignedInt m_inputFrame; ///< frame input was gathered on
161163

162164
};

GeneralsMD/Code/GameEngine/Source/GameClient/GUI/Gadget/GadgetListBox.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ WindowMsgHandledType GadgetListBoxInput( GameWindow *window, UnsignedInt msg,
727727
continue;
728728
for(j = 0; j < TheKeyboard->MAX_KEY_STATES; ++j)
729729
{
730-
if(dString->getText().getCharAt(0) == TheKeyboard->getPrintableKey(mData1, j))
730+
if(dString->getText().getCharAt(0) == TheKeyboard->getPrintableKey((KeyDefType)mData1, j))
731731
{
732732
list->selectPos = position;
733733
Int prevPos = getListboxTopEntry(list);

0 commit comments

Comments
 (0)