2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to:
18 Free Software Foundation, Inc.
19 59 Temple Place - Suite 330
20 Boston, MA 02111-1307, USA
27 key up events are sent even if in console mode
30 #define MAX_INPUTLINE 256
32 int history_line = 31;
33 char key_lines[32][MAX_INPUTLINE];
35 int key_insert = true; // insert key toggle (for editing)
38 int key_consoleactive;
40 static int key_bmap, key_bmap2;
42 char *keybindings[8][1024];
43 static qboolean consolekeys[1024]; // if true, can't be rebound while in
45 static qboolean menubound[1024]; // if true, can't be rebound while in
47 static unsigned int key_repeats[1024]; // if > 1, it is autorepeating
48 static qboolean keydown[1024];
55 static const keyname_t keynames[] = {
60 {"BACKSPACE", K_BACKSPACE},
61 {"UPARROW", K_UPARROW},
62 {"DOWNARROW", K_DOWNARROW},
63 {"LEFTARROW", K_LEFTARROW},
64 {"RIGHTARROW", K_RIGHTARROW},
92 {"MWHEELUP", K_MWHEELUP},
93 {"MWHEELDOWN", K_MWHEELDOWN},
100 {"MOUSE6", K_MOUSE6},
101 {"MOUSE7", K_MOUSE7},
102 {"MOUSE8", K_MOUSE8},
103 {"MOUSE9", K_MOUSE9},
104 {"MOUSE10", K_MOUSE10},
105 {"MOUSE11", K_MOUSE11},
106 {"MOUSE12", K_MOUSE12},
107 {"MOUSE13", K_MOUSE13},
108 {"MOUSE14", K_MOUSE14},
109 {"MOUSE15", K_MOUSE15},
110 {"MOUSE16", K_MOUSE16},
112 {"NUMLOCK", K_NUMLOCK},
113 {"CAPSLOCK", K_CAPSLOCK},
114 {"SCROLLOCK", K_SCROLLOCK},
116 {"KP_HOME", K_KP_HOME },
117 {"KP_UPARROW", K_KP_UPARROW },
118 {"KP_PGUP", K_KP_PGUP },
119 {"KP_LEFTARROW", K_KP_LEFTARROW },
120 {"KP_RIGHTARROW", K_KP_RIGHTARROW },
121 {"KP_END", K_KP_END },
122 {"KP_DOWNARROW", K_KP_DOWNARROW },
123 {"KP_PGDN", K_KP_PGDN },
124 {"KP_INS", K_KP_INS },
125 {"KP_DEL", K_KP_DEL },
126 {"KP_SLASH", K_KP_SLASH },
138 {"KP_PERIOD", K_KP_PERIOD},
139 {"KP_DIVIDE", K_KP_DIVIDE},
140 {"KP_MULTIPLY", K_KP_MULTIPLY},
141 {"KP_MINUS", K_KP_MINUS},
142 {"KP_PLUS", K_KP_PLUS},
143 {"KP_ENTER", K_KP_ENTER},
144 {"KP_EQUALS", K_KP_EQUALS},
196 {"SEMICOLON", ';'}, // because a raw semicolon seperates commands
200 {"APOSTROPHE", '\''},
206 ==============================================================================
208 LINE TYPING INTO THE CONSOLE
210 ==============================================================================
214 Key_ClearEditLine (int edit_line)
216 memset (key_lines[edit_line], '\0', MAX_INPUTLINE);
217 key_lines[edit_line][0] = ']';
223 Interactive line editing and console scrollback
227 Key_Console (int key, char ascii)
229 // LordHavoc: copied most of this from Q2 to improve keyboard handling
256 case K_KP_RIGHTARROW:
276 if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
279 if ((cbd = Sys_GetClipboardData()) != 0)
282 strtok(cbd, "\n\r\b");
284 if (i + key_linepos >= MAX_INPUTLINE)
285 i= MAX_INPUTLINE - key_linepos;
289 strcat(key_lines[edit_line], cbd);
301 Cbuf_AddText ("clear\n");
306 if (key == K_ENTER || key == K_KP_ENTER)
308 Cbuf_AddText (key_lines[edit_line]+1); // skip the ]
310 Con_Printf("%s\n",key_lines[edit_line]);
311 // LordHavoc: redesigned edit_line/history_line
313 history_line = edit_line;
314 memmove(key_lines[0], key_lines[1], sizeof(key_lines[0]) * edit_line);
315 key_lines[edit_line][0] = ']';
316 key_lines[edit_line][1] = 0; // EvilTypeGuy: null terminate
318 // force an update, because the command may take some time
319 if (cls.state == ca_disconnected)
326 // Enhanced command completion
327 // by EvilTypeGuy eviltypeguy@qeradiant.com
328 // Thanks to Fett, Taniwha
329 Con_CompleteCommandLine();
333 // Advanced Console Editing by Radix radix@planetquake.com
334 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
336 // left arrow will just move left one without erasing, backspace will
337 // actually erase charcter
338 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
345 // delete char before cursor
346 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
350 strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
356 // delete char on cursor
357 if (key == K_DEL || key == K_KP_DEL)
359 if ((size_t)key_linepos < strlen(key_lines[edit_line]))
360 strcpy(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1);
365 // if we're at the end, get one character from previous line,
366 // otherwise just go right one
367 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
369 if ((size_t)key_linepos < strlen(key_lines[edit_line]))
375 if (key == K_INS || key == K_KP_INS) // toggle insert mode
381 // End Advanced Console Editing
383 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
385 if (history_line > 0 && key_lines[history_line-1][1])
388 strcpy(key_lines[edit_line], key_lines[history_line]);
389 key_linepos = strlen(key_lines[edit_line]);
394 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
397 if (history_line >= edit_line)
399 history_line = edit_line;
400 key_lines[edit_line][0] = ']';
401 key_lines[edit_line][1] = 0;
406 strcpy(key_lines[edit_line], key_lines[history_line]);
407 key_linepos = strlen(key_lines[edit_line]);
412 if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
414 con_backscroll += ((int) vid_conheight.integer >> 5);
415 if (con_backscroll > con_totallines - (vid_conheight.integer>>3) - 1)
416 con_backscroll = con_totallines - (vid_conheight.integer>>3) - 1;
420 if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
422 con_backscroll -= ((int) vid_conheight.integer >> 5);
423 if (con_backscroll < 0)
428 if (key == K_HOME || key == K_KP_HOME)
430 con_backscroll = con_totallines - (vid_conheight.integer>>3) - 1;
434 if (key == K_END || key == K_KP_END)
444 if (key_linepos < MAX_INPUTLINE-1)
447 len = strlen(&key_lines[edit_line][key_linepos]);
448 // check insert mode, or always insert if at end of line
449 if (key_insert || len == 0)
451 // can't use strcpy to move string to right
453 memmove(&key_lines[edit_line][key_linepos + 1], &key_lines[edit_line][key_linepos], len);
455 key_lines[edit_line][key_linepos] = ascii;
460 //============================================================================
463 char chat_buffer[MAX_INPUTLINE];
464 unsigned int chat_bufferlen = 0;
467 Key_Message (int key, char ascii)
472 Cmd_ForwardStringToServer(va("%s %s", chat_team ? "say_team" : "say ", chat_buffer));
480 if (key == K_ESCAPE) {
487 if (key == K_BACKSPACE) {
488 if (chat_bufferlen) {
490 chat_buffer[chat_bufferlen] = 0;
495 if (chat_bufferlen == sizeof (chat_buffer) - 1)
499 return; // non printable
501 chat_buffer[chat_bufferlen++] = ascii;
502 chat_buffer[chat_bufferlen] = 0;
505 //============================================================================
510 Returns a key number to be used to index keybindings[] by looking at
511 the given string. Single ascii characters return themselves, while
512 the K_* names are matched up.
516 Key_StringToKeynum (const char *str)
523 return tolower(str[0]);
525 for (kn = keynames; kn->name; kn++) {
526 if (!strcasecmp (str, kn->name))
534 Returns a string (either a single ascii char, or a K_* name) for the
536 FIXME: handle quote special (general escape sequence?)
540 Key_KeynumToString (int keynum)
543 static char tinystr[2];
546 return "<KEY NOT FOUND>";
547 if (keynum > 32 && keynum < 127) { // printable ascii
553 for (kn = keynames; kn->name; kn++)
554 if (keynum == kn->keynum)
557 return "<UNKNOWN KEYNUM>";
562 Key_SetBinding (int keynum, int bindmap, const char *binding)
571 if (keybindings[bindmap][keynum]) {
572 Z_Free (keybindings[bindmap][keynum]);
573 keybindings[bindmap][keynum] = NULL;
575 // allocate memory for new binding
576 l = strlen (binding);
577 new = Z_Malloc (l + 1);
578 strcpy (new, binding);
580 keybindings[bindmap][keynum] = new;
584 Key_In_Unbind_f (void)
588 if (Cmd_Argc () != 3) {
589 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
593 m = strtol(Cmd_Argv (1), NULL, 0);
594 if ((m < 0) || (m >= 8)) {
595 Con_Printf("%d isn't a valid bindmap\n", m);
599 b = Key_StringToKeynum (Cmd_Argv (2));
601 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
605 Key_SetBinding (b, m, "");
616 if (c != 3 && c != 4) {
617 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
621 m = strtol(Cmd_Argv (1), NULL, 0);
622 if ((m < 0) || (m >= 8)) {
623 Con_Printf("%d isn't a valid bindmap\n", m);
627 b = Key_StringToKeynum (Cmd_Argv (2));
629 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
634 if (keybindings[m][b])
635 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
637 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
640 // copy the rest of the command line
641 cmd[0] = 0; // start out with a null string
642 for (i = 3; i < c; i++) {
643 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
645 strlcat (cmd, " ", sizeof (cmd));
648 Key_SetBinding (b, m, cmd);
652 Key_In_Bindmap_f (void)
659 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
663 m1 = strtol(Cmd_Argv (1), NULL, 0);
664 if ((m1 < 0) || (m1 >= 8)) {
665 Con_Printf("%d isn't a valid bindmap\n", m1);
669 m2 = strtol(Cmd_Argv (2), NULL, 0);
670 if ((m2 < 0) || (m2 >= 8)) {
671 Con_Printf("%d isn't a valid bindmap\n", m2);
684 if (Cmd_Argc () != 2) {
685 Con_Print("unbind <key> : remove commands from a key\n");
689 b = Key_StringToKeynum (Cmd_Argv (1));
691 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
695 Key_SetBinding (b, 0, "");
699 Key_Unbindall_f (void)
703 for (j = 0; j < 8; j++)
704 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
705 if (keybindings[j][i])
706 Key_SetBinding (i, j, "");
718 if (c != 2 && c != 3) {
719 Con_Print("bind <key> [command] : attach a command to a key\n");
722 b = Key_StringToKeynum (Cmd_Argv (1));
724 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
729 if (keybindings[0][b])
730 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
732 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
735 // copy the rest of the command line
736 cmd[0] = 0; // start out with a null string
737 for (i = 2; i < c; i++) {
738 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
740 strlcat (cmd, " ", sizeof (cmd));
743 Key_SetBinding (b, 0, cmd);
748 Writes lines containing "bind key value"
752 Key_WriteBindings (qfile_t *f)
756 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
757 if (keybindings[0][i])
758 FS_Printf(f, "bind \"%s\" \"%s\"\n",
759 Key_KeynumToString (i), keybindings[0][i]);
760 for (j = 1; j < 8; j++)
761 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
762 if (keybindings[j][i])
763 FS_Printf(f, "in_bind %d \"%s\" \"%s\"\n",
764 j, Key_KeynumToString (i), keybindings[j][i]);
773 for (i = 0; i < 32; i++) {
774 key_lines[i][0] = ']';
780 // init ascii characters in console mode
782 for (i = 32; i < 128; i++)
783 consolekeys[i] = true;
784 consolekeys[K_ENTER] = true; consolekeys[K_KP_ENTER] = true;
785 consolekeys[K_TAB] = true;
786 consolekeys[K_LEFTARROW] = true; consolekeys[K_KP_LEFTARROW] = true;
787 consolekeys[K_RIGHTARROW] = true; consolekeys[K_KP_RIGHTARROW] = true;
788 consolekeys[K_UPARROW] = true; consolekeys[K_KP_UPARROW] = true;
789 consolekeys[K_DOWNARROW] = true; consolekeys[K_KP_DOWNARROW] = true;
790 consolekeys[K_BACKSPACE] = true;
791 consolekeys[K_DEL] = true; consolekeys[K_KP_DEL] = true;
792 consolekeys[K_INS] = true; consolekeys[K_KP_INS] = true;
793 consolekeys[K_HOME] = true; consolekeys[K_KP_HOME] = true;
794 consolekeys[K_END] = true; consolekeys[K_KP_END] = true;
795 consolekeys[K_PGUP] = true; consolekeys[K_KP_PGUP] = true;
796 consolekeys[K_PGDN] = true; consolekeys[K_KP_PGDN] = true;
797 consolekeys[K_SHIFT] = true;
798 consolekeys[K_MWHEELUP] = true;
799 consolekeys[K_MWHEELDOWN] = true;
800 consolekeys[K_KP_PLUS] = true;
801 consolekeys[K_KP_MINUS] = true;
802 consolekeys[K_KP_DIVIDE] = true;
803 consolekeys[K_KP_MULTIPLY] = true;
804 consolekeys['`'] = false;
805 consolekeys['~'] = false;
807 menubound[K_ESCAPE] = true;
808 for (i = 0; i < 12; i++)
809 menubound[K_F1 + i] = true;
812 // register our functions
814 Cmd_AddCommand ("in_bind", Key_In_Bind_f);
815 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f);
816 Cmd_AddCommand ("in_bindmap", Key_In_Bindmap_f);
818 Cmd_AddCommand ("bind", Key_Bind_f);
819 Cmd_AddCommand ("unbind", Key_Unbind_f);
820 Cmd_AddCommand ("unbindall", Key_Unbindall_f);
826 Called by the system between frames for both key up and key down events
827 Should NOT be called during an interrupt!
831 Key_Event (int key, char ascii, qboolean down)
834 #define USERPLAYING() ( !key_consoleactive && key_dest == key_game && (cls.state == ca_connected && cls.signon == SIGNONS) )
836 static qboolean shift_down = false;
839 bind = keybindings[ key_bmap ][ key ];
841 bind = keybindings[ key_bmap2 ][ key ];
845 keydown[ key ] = down;
847 // update key repeats
849 key_repeats[ key ]++;
850 if( key_repeats[ key ] > 1 ) {
851 if( (key_consoleactive && !consolekeys[key]) || USERPLAYING() )
852 return; // ignore most autorepeats
855 key_repeats[ key ] = 0;
858 if( key == K_SHIFT ) {
863 if( bind && bind[ 0 ] == '+') {
864 Cbuf_AddText( va( "-%s %i\n", bind + 1, key) );
867 // handle ESCAPE specially, so unbinding wont help
868 if( key == K_ESCAPE ) {
869 // shift-escape is a safety measure for users who cant toggle the console otherwise
871 Con_ToggleConsole_f();
876 Key_Message( key, ascii );
879 MR_Keydown( key, ascii );
882 if (COM_CheckParm ("-demolooponly"))
890 Sys_Error( "Bad key_dest" );
895 if( !(key_consoleactive && consolekeys[ key ]) && bind && !strncmp( bind, "toggleconsole", strlen( "toggleconsole" ) ) ) {
896 Cbuf_AddText( bind );
897 Cbuf_AddText( "\n" );
898 if( ascii != STRING_COLOR_TAG ) {
902 // during demo playback, all keys ingame bring up the main menu
903 if( cls.demoplayback && !key_consoleactive && key_dest == key_game ) {
904 if (!COM_CheckParm ("-demolooponly"))
909 // menu bind/function keys or normal binds
910 if( (key_dest == key_menu && menubound[key]) || USERPLAYING() ) {
912 if( bind[0] == '+' ) { // button commands add keynum as a parm
913 Cbuf_AddText( va( "%s %i\n", bind, key ) );
915 Cbuf_AddText( bind );
916 Cbuf_AddText( "\n" );
923 // either console or game state key functions
924 if( key_consoleactive ) {
925 Key_Console( key, ascii );
929 Key_Message( key, ascii );
932 MR_Keydown( key, ascii );
938 Sys_Error( "Bad key_dest" );
949 key_repeats[key] = 0;
953 if (key_count <= 0) {
954 return; // just catching keys for Con_NotifyBox
957 // update auto-repeat status
960 if (key_repeats[key] > 1) {
961 if ((key_consoleactive && !consolekeys[key]) ||
962 (!key_consoleactive && key_dest == key_game &&
963 (cls.state == ca_connected && cls.signon == SIGNONS)))
964 return; // ignore most autorepeats
972 // handle escape specially, so the user can never unbind it
974 if (key == K_ESCAPE) {
977 // ctrl-escape is a safety measure
980 Con_ToggleConsole_f ();
985 Key_Message (key, ascii);
988 MR_Keydown (key, ascii);
994 if(UI_Callback_IsSlotUsed(key_dest - 3))
995 UI_Callback_KeyDown (key, ascii);
997 Sys_Error ("Bad key_dest");
1004 if (!(kb = keybindings[key_bmap][key]))
1005 kb = keybindings[key_bmap2][key];
1006 if (kb && !strncmp(kb, "toggleconsole", strlen("toggleconsole")))
1009 Cbuf_AddText ("\n");
1014 if (key_consoleactive && consolekeys[key] && down)
1015 Key_Console (key, ascii);
1019 // key up events only generate commands if the game key binding is a button
1020 // command (leading + sign). These will occur even in console mode, to
1021 // keep the character from continuing an action started before a console
1022 // switch. Button commands include the kenum as a parameter, so multiple
1023 // downs can be matched with ups
1026 if (!(kb = keybindings[key_bmap][key]))
1027 kb = keybindings[key_bmap2][key];
1029 if (kb && kb[0] == '+') {
1030 dpsnprintf (cmd, sizeof(cmd), "-%s %i\n", kb + 1, key);
1037 // during demo playback, most keys bring up the main menu
1039 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game) {
1045 // if not a consolekey, send to the interpreter no matter what mode is
1047 if ((key_dest == key_menu && menubound[key])
1048 || (key_consoleactive && !consolekeys[key])
1049 || (key_dest == key_game &&
1050 ((cls.state == ca_connected) || !consolekeys[key]))) {
1051 if (!(kb = keybindings[key_bmap][key]))
1052 kb = keybindings[key_bmap2][key];
1054 if (kb[0] == '+') { // button commands add keynum as a parm
1055 dpsnprintf (cmd, sizeof(cmd), "%s %i\n", kb, key);
1059 Cbuf_AddText ("\n");
1066 return; // other systems only care about key
1071 Key_Message (key, ascii);
1074 MR_Keydown (key, ascii);
1077 Key_Console (key, ascii);
1080 if(UI_Callback_IsSlotUsed(key_dest - 3))
1081 UI_Callback_KeyDown (key, ascii);
1083 Sys_Error ("Bad key_dest");
1095 Key_ClearStates (void)
1099 for (i = 0; i < (int)(sizeof(keydown)/sizeof(keydown[0])); i++)