1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//===========================================================================//
#include "keys.h"
#include "cdll_engine_int.h"
#include "cmd.h"
#include "toolframework/itoolframework.h"
#include "toolframework/itoolsystem.h"
#include "tier1/utlbuffer.h"
#include "vgui_baseui_interface.h"
#include "tier2/tier2.h"
#include "inputsystem/iinputsystem.h"
#include "cheatcodes.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
enum KeyUpTarget_t
{
KEY_UP_ANYTARGET = 0,
KEY_UP_ENGINE,
KEY_UP_VGUI,
KEY_UP_TOOLS,
KEY_UP_CLIENT,
};
struct KeyInfo_t
{
char *m_pKeyBinding;
unsigned char m_nKeyUpTarget : 3; // see KeyUpTarget_t
unsigned char m_bKeyDown : 1;
};
//-----------------------------------------------------------------------------
// Current keypress state
//-----------------------------------------------------------------------------
static KeyInfo_t s_pKeyInfo[BUTTON_CODE_LAST];
//-----------------------------------------------------------------------------
// Trap mode is used by the keybinding UI
//-----------------------------------------------------------------------------
static bool s_bTrapMode = false;
static bool s_bDoneTrapping = false;
static ButtonCode_t s_nTrapKeyUp = BUTTON_CODE_INVALID;
static ButtonCode_t s_nTrapKey = BUTTON_CODE_INVALID;
//-----------------------------------------------------------------------------
// Can keys be passed to various targets?
//-----------------------------------------------------------------------------
static inline bool ShouldPassKeyUpToTarget( ButtonCode_t code, KeyUpTarget_t target )
{
return ( s_pKeyInfo[code].m_nKeyUpTarget == target ) || ( s_pKeyInfo[code].m_nKeyUpTarget == KEY_UP_ANYTARGET );
}
/*
===================
Key_SetBinding
===================
*/
void Key_SetBinding( ButtonCode_t keynum, const char *pBinding )
{
char *pNewBinding;
int l;
if ( keynum == BUTTON_CODE_INVALID )
return;
// free old bindings
if ( s_pKeyInfo[keynum].m_pKeyBinding )
{
// Exactly the same, don't re-bind and fragment memory
if ( !Q_strcmp( s_pKeyInfo[keynum].m_pKeyBinding, pBinding ) )
return;
delete[] s_pKeyInfo[keynum].m_pKeyBinding;
s_pKeyInfo[keynum].m_pKeyBinding = NULL;
}
// allocate memory for new binding
l = Q_strlen( pBinding );
pNewBinding = (char *)new char[ l+1 ];
Q_strncpy( pNewBinding, pBinding, l + 1 );
pNewBinding[l] = 0;
s_pKeyInfo[keynum].m_pKeyBinding = pNewBinding;
}
/*
===================
Key_Unbind_f
===================
*/
CON_COMMAND_F( unbind, "Unbind a key.", FCVAR_DONTRECORD )
{
ButtonCode_t b;
if ( args.ArgC() != 2 )
{
ConMsg( "unbind <key> : remove commands from a key\n" );
return;
}
b = g_pInputSystem->StringToButtonCode( args[1] );
if ( b == BUTTON_CODE_INVALID )
{
ConMsg( "\"%s\" isn't a valid key\n", args[1] );
return;
}
if ( b == KEY_ESCAPE )
{
ConMsg( "Can't unbind ESCAPE key\n" );
return;
}
Key_SetBinding( b, "" );
}
CON_COMMAND_F( unbind_mac, "Unbind a key on the Mac only.", FCVAR_DONTRECORD )
{
if ( IsOSX() )
{
ButtonCode_t b;
if ( args.ArgC() != 2 )
{
ConMsg( "unbind <key> : remove commands from a key\n" );
return;
}
b = g_pInputSystem->StringToButtonCode( args[1] );
if ( b == BUTTON_CODE_INVALID )
{
ConMsg( "\"%s\" isn't a valid key\n", args[1] );
return;
}
if ( b == KEY_ESCAPE )
{
ConMsg( "Can't unbind ESCAPE key\n" );
return;
}
Key_SetBinding( b, "" );
}
}
CON_COMMAND_F( unbindall, "Unbind all keys.", FCVAR_DONTRECORD )
{
int i;
for ( i=0; i<BUTTON_CODE_LAST; i++ )
{
if ( !s_pKeyInfo[i].m_pKeyBinding )
continue;
// Don't ever unbind escape or console key
if ( i == KEY_ESCAPE )
continue;
if ( i == KEY_BACKQUOTE )
continue;
Key_SetBinding( (ButtonCode_t)i, "" );
}
}
#ifndef SWDS
CON_COMMAND_F( escape, "Escape key pressed.", FCVAR_CLIENTCMD_CAN_EXECUTE )
{
EngineVGui()->HideGameUI();
}
#endif
/*
===================
Key_Bind_f
===================
*/
void BindKey( const char *pchBind, bool bShow, const char *pchCmd )
{
if ( !g_pInputSystem )
return;
ButtonCode_t b = g_pInputSystem->StringToButtonCode( pchBind );
if ( b == BUTTON_CODE_INVALID )
{
ConMsg( "\"%s\" isn't a valid key\n", pchBind );
return;
}
if ( bShow )
{
if (s_pKeyInfo[b].m_pKeyBinding)
{
ConMsg( "\"%s\" = \"%s\"\n", pchBind, s_pKeyInfo[b].m_pKeyBinding );
}
else
{
ConMsg( "\"%s\" is not bound\n", pchBind );
}
return;
}
if ( b == KEY_ESCAPE )
{
pchCmd = "cancelselect";
}
Key_SetBinding( b, pchCmd );
}
CON_COMMAND_F( bind, "Bind a key.", FCVAR_DONTRECORD )
{
int i, c;
char cmd[1024];
c = args.ArgC();
if ( c != 2 && c != 3 )
{
ConMsg( "bind <key> [command] : attach a command to a key\n" );
return;
}
// copy the rest of the command line
cmd[0] = 0; // start out with a null string
for ( i=2 ; i< c ; i++ )
{
if (i > 2)
{
Q_strncat( cmd, " ", sizeof( cmd ), COPY_ALL_CHARACTERS );
}
Q_strncat( cmd, args[i], sizeof( cmd ), COPY_ALL_CHARACTERS );
}
BindKey( args[1], c == 2, cmd );
}
CON_COMMAND_F( bind_mac, "Bind this key but only on Mac, not win32", FCVAR_DONTRECORD )
{
if ( IsOSX() )
{
int i, c;
char cmd[1024];
c = args.ArgC();
if ( c != 2 && c != 3 )
{
ConMsg( "bind <key> [command] : attach a command to a key\n" );
return;
}
// copy the rest of the command line
cmd[0] = 0; // start out with a null string
for ( i=2 ; i< c ; i++ )
{
if (i > 2)
{
Q_strncat( cmd, " ", sizeof( cmd ), COPY_ALL_CHARACTERS );
}
Q_strncat( cmd, args[i], sizeof( cmd ), COPY_ALL_CHARACTERS );
}
BindKey( args[1], c == 2, cmd );
}
}
/*
============
Key_CountBindings
Count number of lines of bindings we'll be writing
============
*/
int Key_CountBindings( void )
{
int i;
int c = 0;
for ( i = 0; i < BUTTON_CODE_LAST; i++ )
{
if ( !s_pKeyInfo[i].m_pKeyBinding || !s_pKeyInfo[i].m_pKeyBinding[0] )
continue;
c++;
}
return c;
}
/*
============
Key_WriteBindings
Writes lines containing "bind key value"
============
*/
void Key_WriteBindings( CUtlBuffer &buf )
{
int i;
for ( i = 0 ; i < BUTTON_CODE_LAST ; i++ )
{
if ( !s_pKeyInfo[i].m_pKeyBinding || !s_pKeyInfo[i].m_pKeyBinding[0] )
continue;
buf.Printf( "bind \"%s\" \"%s\"\n", g_pInputSystem->ButtonCodeToString( (ButtonCode_t)i ), s_pKeyInfo[i].m_pKeyBinding );
}
}
/*
============
Key_NameForBinding
Returns the keyname to which a binding string is bound. E.g., if
TAB is bound to +use then searching for +use will return "TAB"
============
*/
const char *Key_NameForBinding( const char *pBinding )
{
int i;
const char *pBind = pBinding;
if ( pBinding[0] == '+' )
{
++pBind;
}
for (i=0 ; i<BUTTON_CODE_LAST ; i++)
{
if (s_pKeyInfo[i].m_pKeyBinding)
{
if (*s_pKeyInfo[i].m_pKeyBinding)
{
if ( s_pKeyInfo[i].m_pKeyBinding[0] == '+' )
{
if ( !Q_strcasecmp( s_pKeyInfo[i].m_pKeyBinding+1, (char *)pBind ) )
return g_pInputSystem->ButtonCodeToString( (ButtonCode_t)i );
}
else
{
if ( !Q_strcasecmp( s_pKeyInfo[i].m_pKeyBinding, (char *)pBind ) )
return g_pInputSystem->ButtonCodeToString( (ButtonCode_t)i );
}
}
}
}
// Xbox 360 controller: Handle the dual bindings for duck and zoom
if ( !Q_stricmp( "duck", pBind ) )
return Key_NameForBinding( "toggle_duck" );
if ( !Q_stricmp( "zoom", pBind ) )
return Key_NameForBinding( "toggle_zoom" );
return NULL;
}
/*
============
Key_NameForBinding
Returns the keyname to which a binding string is bound. E.g., if
TAB is bound to +use then searching for +use will return "TAB"
Does not perform "helpful" removal of '+' character from bindings.
============
*/
const char *Key_NameForBindingExact( const char *pBinding )
{
int i;
for (i=0 ; i<BUTTON_CODE_LAST ; i++)
{
if (s_pKeyInfo[i].m_pKeyBinding)
{
if (*s_pKeyInfo[i].m_pKeyBinding)
{
if ( !Q_strcasecmp( s_pKeyInfo[i].m_pKeyBinding, pBinding ) )
return g_pInputSystem->ButtonCodeToString( (ButtonCode_t)i );
}
}
}
return NULL;
}
const char *Key_BindingForKey( ButtonCode_t code )
{
if ( code < 0 || code > BUTTON_CODE_LAST )
return NULL;
if ( !s_pKeyInfo[ code ].m_pKeyBinding )
return NULL;
return s_pKeyInfo[ code ].m_pKeyBinding;
}
CON_COMMAND( key_listboundkeys, "List bound keys with bindings." )
{
int i;
for (i=0 ; i<BUTTON_CODE_LAST ; i++)
{
const char *pBinding = Key_BindingForKey( (ButtonCode_t)i );
if ( !pBinding || !pBinding[0] )
continue;
ConMsg( "\"%s\" = \"%s\"\n", g_pInputSystem->ButtonCodeToString( (ButtonCode_t)i ), pBinding );
}
}
CON_COMMAND( key_findbinding, "Find key bound to specified command string." )
{
if ( args.ArgC() != 2 )
{
ConMsg( "usage: key_findbinding substring\n" );
return;
}
const char *substring = args[1];
if ( !substring || !substring[ 0 ] )
{
ConMsg( "usage: key_findbinding substring\n" );
return;
}
int i;
for (i=0 ; i<BUTTON_CODE_LAST ; i++)
{
const char *pBinding = Key_BindingForKey( (ButtonCode_t)i );
if ( !pBinding || !pBinding[0] )
continue;
if ( Q_strstr( pBinding, substring ) )
{
ConMsg( "\"%s\" = \"%s\"\n",
g_pInputSystem->ButtonCodeToString( (ButtonCode_t)i ), pBinding );
}
}
}
//-----------------------------------------------------------------------------
// Initialization, shutdown
//-----------------------------------------------------------------------------
void Key_Init (void)
{
ReadCheatCommandsFromFile( "scripts/cheatcodes.txt" );
ReadCheatCommandsFromFile( "scripts/mod_cheatcodes.txt" );
}
void Key_Shutdown( void )
{
for ( int i = 0; i < ARRAYSIZE( s_pKeyInfo ); ++i )
{
delete[] s_pKeyInfo[ i ].m_pKeyBinding;
s_pKeyInfo[ i ].m_pKeyBinding = NULL;
}
ClearCheatCommands();
}
//-----------------------------------------------------------------------------
// Purpose: Starts trap mode (used for keybinding UI)
//-----------------------------------------------------------------------------
void Key_StartTrapMode( void )
{
if ( s_bTrapMode )
return;
Assert( !s_bDoneTrapping && s_nTrapKeyUp == BUTTON_CODE_INVALID );
s_bDoneTrapping = false;
s_bTrapMode = true;
s_nTrapKeyUp = BUTTON_CODE_INVALID;
}
//-----------------------------------------------------------------------------
// We're done trapping once the first key is hit
//-----------------------------------------------------------------------------
bool Key_CheckDoneTrapping( ButtonCode_t& code )
{
if ( s_bTrapMode )
return false;
if ( !s_bDoneTrapping )
return false;
code = s_nTrapKey;
s_nTrapKey = BUTTON_CODE_INVALID;
// Reset since we retrieved the results
s_bDoneTrapping = false;
return true;
}
#ifndef SWDS
//-----------------------------------------------------------------------------
// Filter out trapped keys
//-----------------------------------------------------------------------------
static bool FilterTrappedKey( ButtonCode_t code, bool bDown )
{
// After we've trapped a key, we want to capture the button up message for that key
if ( s_nTrapKeyUp == code && !bDown )
{
s_nTrapKeyUp = BUTTON_CODE_INVALID;
return true;
}
// Only key down events are trapped
if ( s_bTrapMode && bDown )
{
s_nTrapKey = code;
s_bTrapMode = false;
s_bDoneTrapping = true;
s_nTrapKeyUp = code;
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Lets tools have a whack at key events
//-----------------------------------------------------------------------------
static bool HandleToolKey( const InputEvent_t &event )
{
IToolSystem *toolsys = toolframework->GetTopmostTool();
return toolsys && toolsys->TrapKey( (ButtonCode_t)event.m_nData, ( event.m_nType != IE_ButtonReleased ) );
}
#endif // !SWDS
//-----------------------------------------------------------------------------
// Lets vgui have a whack at key events
//-----------------------------------------------------------------------------
#ifndef SWDS
static bool HandleVGuiKey( const InputEvent_t &event )
{
bool bDown = event.m_nType != IE_ButtonReleased;
ButtonCode_t code = (ButtonCode_t)event.m_nData;
if ( bDown && IsX360() )
{
LogKeyPress( code );
CheckCheatCodes();
}
return EngineVGui()->Key_Event( event );
}
//-----------------------------------------------------------------------------
// Lets the client have a whack at key events
//-----------------------------------------------------------------------------
static bool HandleClientKey( const InputEvent_t &event )
{
bool bDown = event.m_nType != IE_ButtonReleased;
ButtonCode_t code = (ButtonCode_t)event.m_nData;
if ( g_ClientDLL && g_ClientDLL->IN_KeyEvent( bDown ? 1 : 0, code, s_pKeyInfo[ code ].m_pKeyBinding ) == 0 )
return true;
return false;
}
#endif
//-----------------------------------------------------------------------------
// Lets the engine have a whack at key events
//-----------------------------------------------------------------------------
#ifndef SWDS
static bool HandleEngineKey( const InputEvent_t &event )
{
bool bDown = event.m_nType != IE_ButtonReleased;
ButtonCode_t code = (ButtonCode_t)event.m_nData;
// Warn about unbound keys
if ( IsPC() && bDown )
{
if ( IsJoystickCode( code ) && !IsJoystickAxisCode( code ) && !IsSteamControllerCode( code ) && !s_pKeyInfo[code].m_pKeyBinding )
{
ConDMsg( "%s is unbound.\n", g_pInputSystem->ButtonCodeToString( code ) );
}
}
// Allow the client to handle mouse wheel events while the game has focus, without having to bind keys.
#if !defined( SWDS )
if ( ( code == MOUSE_WHEEL_UP || code == MOUSE_WHEEL_DOWN ) && g_pClientReplay )
{
g_ClientDLL->IN_OnMouseWheeled( code == MOUSE_WHEEL_UP ? 1 : -1 );
}
#endif
// key up events only generate commands if the game key binding is
// a button command (leading + sign). These will occur even in console mode,
// to keep the character from continuing an action started before a console
// switch. Button commands include the kenum as a parameter, so multiple
// downs can be matched with ups
char *kb = s_pKeyInfo[ code ].m_pKeyBinding;
if ( !kb || !kb[0] )
return false;
char cmd[1024];
if ( !bDown )
{
if ( kb[0] == '+' )
{
Q_snprintf( cmd, sizeof( cmd ), "-%s %i\n", kb+1, code );
Cbuf_AddText( cmd );
return true;
}
return false;
}
// Send to the interpreter
if (kb[0] == '+')
{
// button commands add keynum as a parm
Q_snprintf( cmd, sizeof( cmd ), "%s %i\n", kb, code );
Cbuf_AddText( cmd );
return true;
}
// Swallow console toggle if any modifier keys are down if it's bound to toggleconsole (the default)
if ( !Q_stricmp( kb, "toggleconsole" ) )
{
if ( s_pKeyInfo[KEY_LALT].m_bKeyDown || s_pKeyInfo[KEY_LSHIFT].m_bKeyDown || s_pKeyInfo[KEY_LCONTROL].m_bKeyDown ||
s_pKeyInfo[KEY_RALT].m_bKeyDown || s_pKeyInfo[KEY_RSHIFT].m_bKeyDown || s_pKeyInfo[KEY_RCONTROL].m_bKeyDown )
return false;
}
Cbuf_AddText( kb );
Cbuf_AddText( "\n" );
return true;
}
#endif // !SWDS
//-----------------------------------------------------------------------------
// Helper function to make sure key down/key up events go to the right places
//-----------------------------------------------------------------------------
#ifndef SWDS
typedef bool (*FilterKeyFunc_t)( const InputEvent_t &event );
static bool FilterKey( const InputEvent_t &event, KeyUpTarget_t target, FilterKeyFunc_t func )
{
bool bDown = event.m_nType != IE_ButtonReleased;
ButtonCode_t code = (ButtonCode_t)event.m_nData;
// Don't pass the key up to tools if some other system wants it
if ( !bDown && !ShouldPassKeyUpToTarget( code, target ) )
return false;
bool bFiltered = func( event );
// If we filtered it, then we need to get the key up event
if ( bDown )
{
if ( bFiltered )
{
Assert( s_pKeyInfo[code].m_nKeyUpTarget == KEY_UP_ANYTARGET );
s_pKeyInfo[code].m_nKeyUpTarget = target;
}
}
else // Up case
{
if ( s_pKeyInfo[code].m_nKeyUpTarget == target )
{
s_pKeyInfo[code].m_nKeyUpTarget = KEY_UP_ANYTARGET;
bFiltered = true;
}
else
{
// NOTE: It is illegal to trap up key events. The system will do it for us
Assert( !bFiltered );
}
}
return bFiltered;
}
#endif // !SWDS
//-----------------------------------------------------------------------------
// Called by the system between frames for both key up and key down events
//-----------------------------------------------------------------------------
void Key_Event( const InputEvent_t &event )
{
#ifdef SWDS
return;
#else
ASSERT_NO_REENTRY();
bool bDown = event.m_nType != IE_ButtonReleased;
ButtonCode_t code = (ButtonCode_t)event.m_nData;
#ifdef LINUX
// We're getting some crashes referencing s_pKeyInfo[ code ]. Let's try to
// hard error here and see if we can get code and type at http://minidump.
if ( code < 0 || code >= ARRAYSIZE( s_pKeyInfo ) )
{
Error( "Key_Event: invalid code! type:%d code:%d\n", event.m_nType, code );
}
#endif
// Don't handle key ups if the key's already up.
// NOTE: This should already be taken care of by the input system
Assert( s_pKeyInfo[code].m_bKeyDown != bDown );
if ( s_pKeyInfo[code].m_bKeyDown == bDown )
return;
s_pKeyInfo[code].m_bKeyDown = bDown;
// Deal with trapped keys
if ( FilterTrappedKey( code, bDown ) )
return;
// Keep vgui's notion of which keys are down up-to-date regardless of filtering
// Necessary because vgui has multiple input contexts, so vgui can't directly
// ask the input system for this information.
EngineVGui()->UpdateButtonState( event );
// Let tools have a whack at keys
if ( FilterKey( event, KEY_UP_TOOLS, HandleToolKey ) )
return;
// Let vgui have a whack at keys
if ( FilterKey( event, KEY_UP_VGUI, HandleVGuiKey ) )
return;
// Let the client have a whack at keys
if ( FilterKey( event, KEY_UP_CLIENT, HandleClientKey ) )
return;
// Finally, let the engine deal. Here's where keybindings occur.
FilterKey( event, KEY_UP_ENGINE, HandleEngineKey );
#endif
}
|