Skip to content

Commit d7aff97

Browse files
committed
Fix console key being printed into the console
1 parent 80839fc commit d7aff97

File tree

6 files changed

+59
-4
lines changed

6 files changed

+59
-4
lines changed

src/common/KeyIdentification.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ namespace Scancode {
5555

5656
class Key
5757
{
58-
public:
5958
public:
6059
enum class Kind {
6160
INVALID, // No key.

src/engine/client/key_binding.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,18 @@ void SetBinding(Key key, int team, std::string binding)
274274
bindingsModified = true;
275275
}
276276

277-
const std::vector<Key>& GetConsoleKeys()
278-
{
279-
static std::vector<Keyboard::Key> consoleKeys;
277+
static std::vector<Keyboard::Key> consoleKeys;
278+
static std::string consoleKeysString;
280279

280+
void UpdateConsoleKeyCache() {
281281
// Only parse the variable when it changes
282282
Util::optional<std::string> modifiedString = cl_consoleKeys.GetModifiedValue();
283283
if ( modifiedString )
284284
{
285285
const char* text_p = modifiedString.value().c_str();
286286
consoleKeys.clear();
287+
consoleKeysString.clear();
288+
287289
while ( true )
288290
{
289291
const char* token = COM_Parse( &text_p );
@@ -294,13 +296,25 @@ const std::vector<Key>& GetConsoleKeys()
294296
Keyboard::Key k = Keyboard::StringToKey(token);
295297
if (k.IsBindable()) {
296298
consoleKeys.push_back(k);
299+
consoleKeysString += KeyToStringUnprefixed( k );
297300
}
298301
}
299302
}
303+
}
304+
305+
const std::vector<Key>& GetConsoleKeys()
306+
{
307+
UpdateConsoleKeyCache();
300308

301309
return consoleKeys;
302310
}
303311

312+
const std::string& GetConsoleKeysString() {
313+
UpdateConsoleKeyCache();
314+
315+
return consoleKeysString;
316+
}
317+
304318
void SetConsoleKeys(const std::vector<Key>& keys)
305319
{
306320
std::string text;

src/engine/client/key_identification.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,32 @@ Key StringToKey(Str::StringRef str)
186186
return Key::NONE;
187187
}
188188

189+
std::string KeyToStringUnprefixed( Key key ) {
190+
if ( key.kind() == Key::Kind::KEYNUM ) {
191+
return KeynumToString( key.AsKeynum() );
192+
}
193+
194+
if ( key.kind() == Key::Kind::UNICODE_CHAR ) {
195+
return CharToString( key.AsCharacter() );
196+
}
197+
198+
if ( key.kind() == Key::Kind::SCANCODE ) {
199+
int sc = key.AsScancode();
200+
if ( char c = ScancodeToAscii( sc ) ) {
201+
return CharToString( c );
202+
}
203+
for ( auto& functionKey : leftRightFunctionKeys ) {
204+
if ( sc == functionKey.scancode ) {
205+
return functionKey.name;
206+
}
207+
}
208+
// make a hex string
209+
return Str::Format( "0x%02x", key.AsScancode() );
210+
}
211+
212+
return "<INVALID KEY>";
213+
}
214+
189215
std::string KeyToString(Key key)
190216
{
191217
if (key.kind() == Key::Kind::KEYNUM) {

src/engine/client/key_identification.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ namespace Keyboard {
3939

4040
Key StringToKey(Str::StringRef name);
4141
std::string KeyToString(Key key);
42+
std::string KeyToStringUnprefixed(Key key);
4243

4344
// Returns the code point of a character typed by the given key, or 0 if none is found.
4445
int GetCharForScancode(int scancode);

src/engine/client/keys.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Util::optional<std::string> GetBinding(Key key, BindTeam team, bool useDefault);
7878
// Get/set the keys which toggle (both open and close) the console.
7979
// The source of truth is cl_consoleKeys, but these provide an interface with Key objects.
8080
const std::vector<Key>& GetConsoleKeys();
81+
const std::string& GetConsoleKeysString();
8182
void SetConsoleKeys(const std::vector<Key>& keys);
8283

8384
// Gets all keys that, if pressed, would execute the given command, based on the current team.

src/engine/sys/sdl_input.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,20 @@ static void IN_ProcessEvents( bool dropInput )
11131113
{
11141114
std::string text = e.text.text;
11151115

1116+
/* We can get a console key as a text event after we've opened the console from a key up/down event,
1117+
* in a separate execution of IN_ProcessEvents()
1118+
* The key character can also be anywhere in the text
1119+
* This might be an SDL bug */
1120+
text.erase(
1121+
std::remove_if( text.begin(), text.end(),
1122+
[]( unsigned char c ) {
1123+
for( const unsigned char key : Keyboard::GetConsoleKeysString() ) {
1124+
return c == key;
1125+
}
1126+
}
1127+
), text.end()
1128+
);
1129+
11161130
const char* c = text.c_str();
11171131
while ( *c ) {
11181132
int width = Q_UTF8_Width( c );

0 commit comments

Comments
 (0)