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 static int ctrl_down = false;
36 static int key_lastpress;
37 int key_insert = true; // insert key toggle (for editing)
40 int key_consoleactive;
42 static int key_count; // incremented every key event
43 static int key_bmap, key_bmap2;
45 char *keybindings[8][1024];
46 static qboolean consolekeys[1024]; // if true, can't be rebound while in
48 static qboolean menubound[1024]; // if true, can't be rebound while in
50 static unsigned int key_repeats[1024]; // if > 1, it is autorepeating
51 static qboolean keydown[1024];
58 static const keyname_t keynames[] = {
63 {"BACKSPACE", K_BACKSPACE},
64 {"UPARROW", K_UPARROW},
65 {"DOWNARROW", K_DOWNARROW},
66 {"LEFTARROW", K_LEFTARROW},
67 {"RIGHTARROW", K_RIGHTARROW},
95 {"MWHEELUP", K_MWHEELUP},
96 {"MWHEELDOWN", K_MWHEELDOWN},
100 {"MOUSE3", K_MOUSE3},
101 {"MOUSE4", K_MOUSE4},
102 {"MOUSE5", K_MOUSE5},
103 {"MOUSE6", K_MOUSE6},
104 {"MOUSE7", K_MOUSE7},
105 {"MOUSE8", K_MOUSE8},
106 {"MOUSE9", K_MOUSE9},
107 {"MOUSE10", K_MOUSE10},
108 {"MOUSE11", K_MOUSE11},
109 {"MOUSE12", K_MOUSE12},
110 {"MOUSE13", K_MOUSE13},
111 {"MOUSE14", K_MOUSE14},
112 {"MOUSE15", K_MOUSE15},
113 {"MOUSE16", K_MOUSE16},
115 {"NUMLOCK", K_NUMLOCK},
116 {"CAPSLOCK", K_CAPSLOCK},
117 {"SCROLLOCK", K_SCROLLOCK},
119 {"KP_HOME", K_KP_HOME },
120 {"KP_UPARROW", K_KP_UPARROW },
121 {"KP_PGUP", K_KP_PGUP },
122 {"KP_LEFTARROW", K_KP_LEFTARROW },
123 {"KP_RIGHTARROW", K_KP_RIGHTARROW },
124 {"KP_END", K_KP_END },
125 {"KP_DOWNARROW", K_KP_DOWNARROW },
126 {"KP_PGDN", K_KP_PGDN },
127 {"KP_INS", K_KP_INS },
128 {"KP_DEL", K_KP_DEL },
129 {"KP_SLASH", K_KP_SLASH },
141 {"KP_PERIOD", K_KP_PERIOD},
142 {"KP_DIVIDE", K_KP_DIVIDE},
143 {"KP_MULTIPLY", K_KP_MULTIPLY},
144 {"KP_MINUS", K_KP_MINUS},
145 {"KP_PLUS", K_KP_PLUS},
146 {"KP_ENTER", K_KP_ENTER},
147 {"KP_EQUALS", K_KP_EQUALS},
199 {"SEMICOLON", ';'}, // because a raw semicolon seperates commands
203 {"APOSTROPHE", '\''},
209 ==============================================================================
211 LINE TYPING INTO THE CONSOLE
213 ==============================================================================
217 Key_ClearEditLine (int edit_line)
219 memset (key_lines[edit_line], '\0', MAX_INPUTLINE);
220 key_lines[edit_line][0] = ']';
226 Interactive line editing and console scrollback
230 Key_Console (int key, char ascii)
232 // LordHavoc: copied most of this from Q2 to improve keyboard handling
259 case K_KP_RIGHTARROW:
279 if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
282 if ((cbd = Sys_GetClipboardData()) != 0)
285 strtok(cbd, "\n\r\b");
287 if (i + key_linepos >= MAX_INPUTLINE)
288 i= MAX_INPUTLINE - key_linepos;
292 strcat(key_lines[edit_line], cbd);
304 Cbuf_AddText ("clear\n");
309 if (key == K_ENTER || key == K_KP_ENTER)
311 Cbuf_AddText (key_lines[edit_line]+1); // skip the ]
313 Con_Printf("%s\n",key_lines[edit_line]);
314 // LordHavoc: redesigned edit_line/history_line
316 history_line = edit_line;
317 memmove(key_lines[0], key_lines[1], sizeof(key_lines[0]) * edit_line);
318 key_lines[edit_line][0] = ']';
319 key_lines[edit_line][1] = 0; // EvilTypeGuy: null terminate
321 // force an update, because the command may take some time
322 if (cls.state == ca_disconnected)
329 // Enhanced command completion
330 // by EvilTypeGuy eviltypeguy@qeradiant.com
331 // Thanks to Fett, Taniwha
332 Con_CompleteCommandLine();
336 // Advanced Console Editing by Radix radix@planetquake.com
337 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
339 // left arrow will just move left one without erasing, backspace will
340 // actually erase charcter
341 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
348 // delete char before cursor
349 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
353 strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
359 // delete char on cursor
360 if (key == K_DEL || key == K_KP_DEL)
362 if ((size_t)key_linepos < strlen(key_lines[edit_line]))
363 strcpy(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1);
368 // if we're at the end, get one character from previous line,
369 // otherwise just go right one
370 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
372 if ((size_t)key_linepos < strlen(key_lines[edit_line]))
378 if (key == K_INS || key == K_KP_INS) // toggle insert mode
384 // End Advanced Console Editing
386 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
388 if (history_line > 0 && key_lines[history_line-1][1])
391 strcpy(key_lines[edit_line], key_lines[history_line]);
392 key_linepos = strlen(key_lines[edit_line]);
397 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
400 if (history_line >= edit_line)
402 history_line = edit_line;
403 key_lines[edit_line][0] = ']';
404 key_lines[edit_line][1] = 0;
409 strcpy(key_lines[edit_line], key_lines[history_line]);
410 key_linepos = strlen(key_lines[edit_line]);
415 if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
417 con_backscroll += ((int) vid_conheight.integer >> 5);
418 if (con_backscroll > con_totallines - (vid_conheight.integer>>3) - 1)
419 con_backscroll = con_totallines - (vid_conheight.integer>>3) - 1;
423 if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
425 con_backscroll -= ((int) vid_conheight.integer >> 5);
426 if (con_backscroll < 0)
431 if (key == K_HOME || key == K_KP_HOME)
433 con_backscroll = con_totallines - (vid_conheight.integer>>3) - 1;
437 if (key == K_END || key == K_KP_END)
447 if (key_linepos < MAX_INPUTLINE-1)
450 len = strlen(&key_lines[edit_line][key_linepos]);
451 // check insert mode, or always insert if at end of line
452 if (key_insert || len == 0)
454 // can't use strcpy to move string to right
456 memmove(&key_lines[edit_line][key_linepos + 1], &key_lines[edit_line][key_linepos], len);
458 key_lines[edit_line][key_linepos] = ascii;
463 //============================================================================
466 char chat_buffer[MAX_INPUTLINE];
467 unsigned int chat_bufferlen = 0;
470 Key_Message (int key, char ascii)
475 Cmd_ForwardStringToServer(va("%s %s", chat_team ? "say_team" : "say ", chat_buffer));
483 if (key == K_ESCAPE) {
490 if (key == K_BACKSPACE) {
491 if (chat_bufferlen) {
493 chat_buffer[chat_bufferlen] = 0;
498 if (chat_bufferlen == sizeof (chat_buffer) - 1)
502 return; // non printable
504 chat_buffer[chat_bufferlen++] = ascii;
505 chat_buffer[chat_bufferlen] = 0;
508 //============================================================================
513 Returns a key number to be used to index keybindings[] by looking at
514 the given string. Single ascii characters return themselves, while
515 the K_* names are matched up.
519 Key_StringToKeynum (const char *str)
526 return tolower(str[0]);
528 for (kn = keynames; kn->name; kn++) {
529 if (!strcasecmp (str, kn->name))
537 Returns a string (either a single ascii char, or a K_* name) for the
539 FIXME: handle quote special (general escape sequence?)
543 Key_KeynumToString (int keynum)
546 static char tinystr[2];
549 return "<KEY NOT FOUND>";
550 if (keynum > 32 && keynum < 127) { // printable ascii
556 for (kn = keynames; kn->name; kn++)
557 if (keynum == kn->keynum)
560 return "<UNKNOWN KEYNUM>";
565 Key_SetBinding (int keynum, int bindmap, const char *binding)
574 if (keybindings[bindmap][keynum]) {
575 Z_Free (keybindings[bindmap][keynum]);
576 keybindings[bindmap][keynum] = NULL;
578 // allocate memory for new binding
579 l = strlen (binding);
580 new = Z_Malloc (l + 1);
581 strcpy (new, binding);
583 keybindings[bindmap][keynum] = new;
587 Key_In_Unbind_f (void)
591 if (Cmd_Argc () != 3) {
592 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
596 m = strtol(Cmd_Argv (1), NULL, 0);
597 if ((m < 0) || (m >= 8)) {
598 Con_Printf("%d isn't a valid bindmap\n", m);
602 b = Key_StringToKeynum (Cmd_Argv (2));
604 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
608 Key_SetBinding (b, m, "");
619 if (c != 3 && c != 4) {
620 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
624 m = strtol(Cmd_Argv (1), NULL, 0);
625 if ((m < 0) || (m >= 8)) {
626 Con_Printf("%d isn't a valid bindmap\n", m);
630 b = Key_StringToKeynum (Cmd_Argv (2));
632 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
637 if (keybindings[m][b])
638 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
640 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
643 // copy the rest of the command line
644 cmd[0] = 0; // start out with a null string
645 for (i = 3; i < c; i++) {
646 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
648 strlcat (cmd, " ", sizeof (cmd));
651 Key_SetBinding (b, m, cmd);
655 Key_In_Bindmap_f (void)
662 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
666 m1 = strtol(Cmd_Argv (1), NULL, 0);
667 if ((m1 < 0) || (m1 >= 8)) {
668 Con_Printf("%d isn't a valid bindmap\n", m1);
672 m2 = strtol(Cmd_Argv (2), NULL, 0);
673 if ((m2 < 0) || (m2 >= 8)) {
674 Con_Printf("%d isn't a valid bindmap\n", m2);
687 if (Cmd_Argc () != 2) {
688 Con_Print("unbind <key> : remove commands from a key\n");
692 b = Key_StringToKeynum (Cmd_Argv (1));
694 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
698 Key_SetBinding (b, 0, "");
702 Key_Unbindall_f (void)
706 for (j = 0; j < 8; j++)
707 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
708 if (keybindings[j][i])
709 Key_SetBinding (i, j, "");
721 if (c != 2 && c != 3) {
722 Con_Print("bind <key> [command] : attach a command to a key\n");
725 b = Key_StringToKeynum (Cmd_Argv (1));
727 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
732 if (keybindings[0][b])
733 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
735 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
738 // copy the rest of the command line
739 cmd[0] = 0; // start out with a null string
740 for (i = 2; i < c; i++) {
741 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
743 strlcat (cmd, " ", sizeof (cmd));
746 Key_SetBinding (b, 0, cmd);
751 Writes lines containing "bind key value"
755 Key_WriteBindings (qfile_t *f)
759 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
760 if (keybindings[0][i])
761 FS_Printf(f, "bind \"%s\" \"%s\"\n",
762 Key_KeynumToString (i), keybindings[0][i]);
763 for (j = 1; j < 8; j++)
764 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
765 if (keybindings[j][i])
766 FS_Printf(f, "in_bind %d \"%s\" \"%s\"\n",
767 j, Key_KeynumToString (i), keybindings[j][i]);
776 for (i = 0; i < 32; i++) {
777 key_lines[i][0] = ']';
783 // init ascii characters in console mode
785 for (i = 32; i < 128; i++)
786 consolekeys[i] = true;
787 consolekeys[K_ENTER] = true; consolekeys[K_KP_ENTER] = true;
788 consolekeys[K_TAB] = true;
789 consolekeys[K_LEFTARROW] = true; consolekeys[K_KP_LEFTARROW] = true;
790 consolekeys[K_RIGHTARROW] = true; consolekeys[K_KP_RIGHTARROW] = true;
791 consolekeys[K_UPARROW] = true; consolekeys[K_KP_UPARROW] = true;
792 consolekeys[K_DOWNARROW] = true; consolekeys[K_KP_DOWNARROW] = true;
793 consolekeys[K_BACKSPACE] = true;
794 consolekeys[K_DEL] = true; consolekeys[K_KP_DEL] = true;
795 consolekeys[K_INS] = true; consolekeys[K_KP_INS] = true;
796 consolekeys[K_HOME] = true; consolekeys[K_KP_HOME] = true;
797 consolekeys[K_END] = true; consolekeys[K_KP_END] = true;
798 consolekeys[K_PGUP] = true; consolekeys[K_KP_PGUP] = true;
799 consolekeys[K_PGDN] = true; consolekeys[K_KP_PGDN] = true;
800 consolekeys[K_SHIFT] = true;
801 consolekeys[K_MWHEELUP] = true;
802 consolekeys[K_MWHEELDOWN] = true;
803 consolekeys[K_KP_PLUS] = true;
804 consolekeys[K_KP_MINUS] = true;
805 consolekeys[K_KP_DIVIDE] = true;
806 consolekeys[K_KP_MULTIPLY] = true;
807 consolekeys['`'] = false;
808 consolekeys['~'] = false;
810 menubound[K_ESCAPE] = true;
811 for (i = 0; i < 12; i++)
812 menubound[K_F1 + i] = true;
815 // register our functions
817 Cmd_AddCommand ("in_bind", Key_In_Bind_f);
818 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f);
819 Cmd_AddCommand ("in_bindmap", Key_In_Bindmap_f);
821 Cmd_AddCommand ("bind", Key_Bind_f);
822 Cmd_AddCommand ("unbind", Key_Unbind_f);
823 Cmd_AddCommand ("unbindall", Key_Unbindall_f);
829 Called by the system between frames for both key up and key down events
830 Should NOT be called during an interrupt!
834 Key_Event (int key, char ascii, qboolean down)
837 #define USERPLAYING() ( !key_consoleactive && key_dest == key_game && (cls.state == ca_connected && cls.signon == SIGNONS) )
839 static qboolean shift_down = false;
842 bind = keybindings[ key_bmap ][ key ];
844 bind = keybindings[ key_bmap2 ][ key ];
848 keydown[ key ] = down;
850 // update key repeats
852 key_repeats[ key ]++;
853 if( key_repeats[ key ] > 1 ) {
854 if( (key_consoleactive && !consolekeys[key]) || USERPLAYING() )
855 return; // ignore most autorepeats
858 key_repeats[ key ] = 0;
861 if( key == K_SHIFT ) {
866 if( bind && bind[ 0 ] == '+') {
867 Cbuf_AddText( va( "-%s %i\n", bind + 1, key) );
870 // handle ESCAPE specially, so unbinding wont help
871 if( key == K_ESCAPE ) {
872 // shift-escape is a safety measure for users who cant toggle the console otherwise
874 Con_ToggleConsole_f();
879 Key_Message( key, ascii );
882 MR_Keydown( key, ascii );
888 Sys_Error( "Bad key_dest" );
893 if( !(key_consoleactive && consolekeys[ key ]) && bind && !strncmp( bind, "toggleconsole", strlen( "toggleconsole" ) ) ) {
894 Cbuf_AddText( bind );
895 Cbuf_AddText( "\n" );
897 // during demo playback, all keys ingame bring up the main menu
898 if( cls.demoplayback && !key_consoleactive && key_dest == key_game ) {
903 // menu bind/function keys or normal binds
904 if( (key_dest == key_menu && menubound[key]) || USERPLAYING() ) {
906 if( bind[0] == '+' ) { // button commands add keynum as a parm
907 Cbuf_AddText( va( "%s %i\n", bind, key ) );
909 Cbuf_AddText( bind );
910 Cbuf_AddText( "\n" );
917 // either console or game state key functions
918 if( key_consoleactive ) {
919 Key_Console( key, ascii );
923 Key_Message( key, ascii );
926 MR_Keydown( key, ascii );
932 Sys_Error( "Bad key_dest" );
943 key_repeats[key] = 0;
947 if (key_count <= 0) {
948 return; // just catching keys for Con_NotifyBox
951 // update auto-repeat status
954 if (key_repeats[key] > 1) {
955 if ((key_consoleactive && !consolekeys[key]) ||
956 (!key_consoleactive && key_dest == key_game &&
957 (cls.state == ca_connected && cls.signon == SIGNONS)))
958 return; // ignore most autorepeats
966 // handle escape specially, so the user can never unbind it
968 if (key == K_ESCAPE) {
971 // ctrl-escape is a safety measure
974 Con_ToggleConsole_f ();
979 Key_Message (key, ascii);
982 MR_Keydown (key, ascii);
988 if(UI_Callback_IsSlotUsed(key_dest - 3))
989 UI_Callback_KeyDown (key, ascii);
991 Sys_Error ("Bad key_dest");
998 if (!(kb = keybindings[key_bmap][key]))
999 kb = keybindings[key_bmap2][key];
1000 if (kb && !strncmp(kb, "toggleconsole", strlen("toggleconsole")))
1003 Cbuf_AddText ("\n");
1008 if (key_consoleactive && consolekeys[key] && down)
1009 Key_Console (key, ascii);
1013 // key up events only generate commands if the game key binding is a button
1014 // command (leading + sign). These will occur even in console mode, to
1015 // keep the character from continuing an action started before a console
1016 // switch. Button commands include the kenum as a parameter, so multiple
1017 // downs can be matched with ups
1020 if (!(kb = keybindings[key_bmap][key]))
1021 kb = keybindings[key_bmap2][key];
1023 if (kb && kb[0] == '+') {
1024 dpsnprintf (cmd, sizeof(cmd), "-%s %i\n", kb + 1, key);
1031 // during demo playback, most keys bring up the main menu
1033 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game) {
1039 // if not a consolekey, send to the interpreter no matter what mode is
1041 if ((key_dest == key_menu && menubound[key])
1042 || (key_consoleactive && !consolekeys[key])
1043 || (key_dest == key_game &&
1044 ((cls.state == ca_connected) || !consolekeys[key]))) {
1045 if (!(kb = keybindings[key_bmap][key]))
1046 kb = keybindings[key_bmap2][key];
1048 if (kb[0] == '+') { // button commands add keynum as a parm
1049 dpsnprintf (cmd, sizeof(cmd), "%s %i\n", kb, key);
1053 Cbuf_AddText ("\n");
1060 return; // other systems only care about key
1065 Key_Message (key, ascii);
1068 MR_Keydown (key, ascii);
1071 Key_Console (key, ascii);
1074 if(UI_Callback_IsSlotUsed(key_dest - 3))
1075 UI_Callback_KeyDown (key, ascii);
1077 Sys_Error ("Bad key_dest");
1089 Key_ClearStates (void)
1093 for (i = 0; i < (int)(sizeof(keydown)/sizeof(keydown[0])); i++)