4 Copyright (C) 1996-1997 Id Software, Inc.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to:
20 Free Software Foundation, Inc.
21 59 Temple Place - Suite 330
22 Boston, MA 02111-1307, USA
25 static const char rcsid[] =
31 extern void SCR_UpdateScreen (void);
34 key up events are sent even if in console mode
37 #define MAX_INPUTLINE 256
38 char key_lines[32][MAX_INPUTLINE];
40 static int ctrl_down = false;
41 static int key_lastpress;
42 int key_insert; // insert key toggle (for editing)
48 int key_consoleactive;
50 static int key_count; // incremented every key event
51 static int key_bmap, key_bmap2;
53 char *keybindings[8][1024];
54 static qboolean consolekeys[1024]; // if true, can't be rebound while in
56 static qboolean menubound[1024]; // if true, can't be rebound while in
58 static unsigned int key_repeats[1024]; // if > 1, it is autorepeating
59 static qboolean keydown[1024];
66 static const keyname_t keynames[] = {
71 {"BACKSPACE", K_BACKSPACE},
72 {"UPARROW", K_UPARROW},
73 {"DOWNARROW", K_DOWNARROW},
74 {"LEFTARROW", K_LEFTARROW},
75 {"RIGHTARROW", K_RIGHTARROW},
103 {"MWHEELUP", K_MWHEELUP},
104 {"MWHEELDOWN", K_MWHEELDOWN},
106 {"MOUSE1", K_MOUSE1},
107 {"MOUSE2", K_MOUSE2},
108 {"MOUSE3", K_MOUSE3},
109 {"MOUSE4", K_MOUSE4},
110 {"MOUSE5", K_MOUSE5},
111 {"MOUSE6", K_MOUSE6},
112 {"MOUSE7", K_MOUSE7},
113 {"MOUSE8", K_MOUSE8},
114 {"MOUSE9", K_MOUSE9},
115 {"MOUSE10", K_MOUSE10},
116 {"MOUSE11", K_MOUSE11},
117 {"MOUSE12", K_MOUSE12},
118 {"MOUSE13", K_MOUSE13},
119 {"MOUSE14", K_MOUSE14},
120 {"MOUSE15", K_MOUSE15},
121 {"MOUSE16", K_MOUSE16},
123 {"NUMLOCK", K_NUMLOCK},
124 {"CAPSLOCK", K_CAPSLOCK},
125 {"SCROLLOCK", K_SCROLLOCK},
127 {"KP_HOME", K_KP_HOME },
128 {"KP_UPARROW", K_KP_UPARROW },
129 {"KP_PGUP", K_KP_PGUP },
130 {"KP_LEFTARROW", K_KP_LEFTARROW },
131 {"KP_RIGHTARROW", K_KP_RIGHTARROW },
132 {"KP_END", K_KP_END },
133 {"KP_DOWNARROW", K_KP_DOWNARROW },
134 {"KP_PGDN", K_KP_PGDN },
135 {"KP_INS", K_KP_INS },
136 {"KP_DEL", K_KP_DEL },
137 {"KP_SLASH", K_KP_SLASH },
149 {"KP_PERIOD", K_KP_PERIOD},
150 {"KP_DIVIDE", K_KP_DIVIDE},
151 {"KP_MULTIPLY", K_KP_MULTIPLY},
152 {"KP_MINUS", K_KP_MINUS},
153 {"KP_PLUS", K_KP_PLUS},
154 {"KP_ENTER", K_KP_ENTER},
155 {"KP_EQUALS", K_KP_EQUALS},
207 {"SEMICOLON", ';'}, // because a raw semicolon seperates commands
211 {"APOSTROPHE", '\''},
217 ==============================================================================
219 LINE TYPING INTO THE CONSOLE
221 ==============================================================================
225 Key_ClearEditLine (int edit_line)
227 memset (key_lines[edit_line], '\0', MAX_INPUTLINE);
228 key_lines[edit_line][0] = ']';
234 Interactive line editing and console scrollback
238 Key_Console (int key, char ascii)
240 // LordHavoc: copied most of this from Q2 to improve keyboard handling
267 case K_KP_RIGHTARROW:
287 if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
290 if ((cbd = Sys_GetClipboardData()) != 0)
293 strtok(cbd, "\n\r\b");
295 if (i + key_linepos >= MAX_INPUTLINE)
296 i= MAX_INPUTLINE - key_linepos;
300 strcat(key_lines[edit_line], cbd);
312 Cbuf_AddText ("clear\n");
317 if (key == K_ENTER || key == K_KP_ENTER)
319 Cbuf_AddText (key_lines[edit_line]+1); // skip the ]
321 Con_Printf("%s\n",key_lines[edit_line]);
322 edit_line = (edit_line + 1) & 31;
323 history_line = edit_line;
324 key_lines[edit_line][0] = ']';
325 key_lines[edit_line][1] = 0; // EvilTypeGuy: null terminate
327 // force an update, because the command may take some time
328 if (cls.state == ca_disconnected)
338 // Enhanced command completion
339 // by EvilTypeGuy eviltypeguy@qeradiant.com
340 // Thanks to Fett, Taniwha
341 Con_CompleteCommandLine();
345 // Advanced Console Editing by Radix radix@planetquake.com
346 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
348 // left arrow will just move left one without erasing, backspace will
349 // actually erase charcter
350 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
357 // delete char before cursor
358 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
362 strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
368 // delete char on cursor
369 if (key == K_DEL || key == K_KP_DEL)
371 if ((size_t)key_linepos < strlen(key_lines[edit_line]))
372 strcpy(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1);
377 // if we're at the end, get one character from previous line,
378 // otherwise just go right one
379 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
381 if (strlen(key_lines[edit_line]) == (size_t)key_linepos)
383 if (strlen(key_lines[(edit_line + 31) & 31]) <= (size_t)key_linepos)
384 return; // no character to get
386 key_lines[edit_line][key_linepos] = key_lines[(edit_line + 31) & 31][key_linepos];
388 key_lines[edit_line][key_linepos] = 0;
396 if (key == K_INS || key == K_KP_INS) // toggle insert mode
402 // End Advanced Console Editing
404 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
408 history_line = (history_line - 1) & 31;
409 } while (history_line != edit_line
410 && !key_lines[history_line][1]);
411 if (history_line == edit_line)
412 history_line = (edit_line+1)&31;
413 strcpy(key_lines[edit_line], key_lines[history_line]);
414 key_linepos = strlen(key_lines[edit_line]);
418 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
420 if (history_line == edit_line) return;
423 history_line = (history_line + 1) & 31;
425 while (history_line != edit_line
426 && !key_lines[history_line][1]);
427 if (history_line == edit_line)
429 key_lines[edit_line][0] = ']';
434 strcpy(key_lines[edit_line], key_lines[history_line]);
435 key_linepos = strlen(key_lines[edit_line]);
440 if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
442 con_backscroll += ((int) scr_conlines >> 4);
443 if (con_backscroll > con_totallines - (vid.conheight>>3) - 1)
444 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
448 if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
450 con_backscroll -= ((int) scr_conlines >> 4);
451 if (con_backscroll < 0)
456 if (key == K_HOME || key == K_KP_HOME)
458 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
462 if (key == K_END || key == K_KP_END)
469 if (ascii < 32 || ascii > 126)
472 if (key_linepos < MAX_INPUTLINE-1)
476 if (key_insert) // check insert mode
478 // can't do strcpy to move string to right
479 i = strlen(key_lines[edit_line]) - 1;
484 for (; i >= key_linepos; i--)
485 key_lines[edit_line][i + 1] = key_lines[edit_line][i];
488 // only null terminate if at the end
489 i = key_lines[edit_line][key_linepos];
490 key_lines[edit_line][key_linepos] = ascii;
494 key_lines[edit_line][key_linepos] = 0;
498 //============================================================================
501 char chat_buffer[MAX_INPUTLINE];
502 unsigned int chat_bufferlen = 0;
505 Key_Message (int key, char ascii)
508 if (key == K_ENTER) {
510 Cbuf_AddText ("say_team \"");
512 Cbuf_AddText ("say \"");
513 Cbuf_AddText (chat_buffer);
514 Cbuf_AddText ("\"\n");
522 if (key == K_ESCAPE) {
529 if (key == K_BACKSPACE) {
530 if (chat_bufferlen) {
532 chat_buffer[chat_bufferlen] = 0;
537 if (chat_bufferlen == sizeof (chat_buffer) - 1)
541 return; // non printable
543 chat_buffer[chat_bufferlen++] = ascii;
544 chat_buffer[chat_bufferlen] = 0;
547 //============================================================================
552 Returns a key number to be used to index keybindings[] by looking at
553 the given string. Single ascii characters return themselves, while
554 the K_* names are matched up.
558 Key_StringToKeynum (const char *str)
565 return tolower(str[0]);
567 for (kn = keynames; kn->name; kn++) {
568 if (!strcasecmp (str, kn->name))
576 Returns a string (either a single ascii char, or a K_* name) for the
578 FIXME: handle quote special (general escape sequence?)
582 Key_KeynumToString (int keynum)
585 static char tinystr[2];
588 return "<KEY NOT FOUND>";
589 if (keynum > 32 && keynum < 127) { // printable ascii
595 for (kn = keynames; kn->name; kn++)
596 if (keynum == kn->keynum)
599 return "<UNKNOWN KEYNUM>";
604 Key_SetBinding (int keynum, int bindmap, const char *binding)
613 if (keybindings[bindmap][keynum]) {
614 Z_Free (keybindings[bindmap][keynum]);
615 keybindings[bindmap][keynum] = NULL;
617 // allocate memory for new binding
618 l = strlen (binding);
619 new = Z_Malloc (l + 1);
620 strcpy (new, binding);
622 keybindings[bindmap][keynum] = new;
626 Key_In_Unbind_f (void)
630 if (Cmd_Argc () != 3) {
631 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
635 m = strtol(Cmd_Argv (1), NULL, 0);
636 if ((m < 0) || (m >= 8)) {
637 Con_Printf("%d isn't a valid bindmap\n", m);
641 b = Key_StringToKeynum (Cmd_Argv (2));
643 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
647 Key_SetBinding (b, m, "");
658 if (c != 3 && c != 4) {
659 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
663 m = strtol(Cmd_Argv (1), NULL, 0);
664 if ((m < 0) || (m >= 8)) {
665 Con_Printf("%d isn't a valid bindmap\n", m);
669 b = Key_StringToKeynum (Cmd_Argv (2));
671 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
676 if (keybindings[m][b])
677 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
679 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
682 // copy the rest of the command line
683 cmd[0] = 0; // start out with a null string
684 for (i = 3; i < c; i++) {
685 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
687 strlcat (cmd, " ", sizeof (cmd));
690 Key_SetBinding (b, m, cmd);
694 Key_In_Bindmap_f (void)
701 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
705 m1 = strtol(Cmd_Argv (1), NULL, 0);
706 if ((m1 < 0) || (m1 >= 8)) {
707 Con_Printf("%d isn't a valid bindmap\n", m1);
711 m2 = strtol(Cmd_Argv (2), NULL, 0);
712 if ((m2 < 0) || (m2 >= 8)) {
713 Con_Printf("%d isn't a valid bindmap\n", m2);
726 if (Cmd_Argc () != 2) {
727 Con_Print("unbind <key> : remove commands from a key\n");
731 b = Key_StringToKeynum (Cmd_Argv (1));
733 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
737 Key_SetBinding (b, 0, "");
741 Key_Unbindall_f (void)
745 for (j = 0; j < 8; j++)
746 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
747 if (keybindings[j][i])
748 Key_SetBinding (i, j, "");
760 if (c != 2 && c != 3) {
761 Con_Print("bind <key> [command] : attach a command to a key\n");
764 b = Key_StringToKeynum (Cmd_Argv (1));
766 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
771 if (keybindings[0][b])
772 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
774 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
777 // copy the rest of the command line
778 cmd[0] = 0; // start out with a null string
779 for (i = 2; i < c; i++) {
780 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
782 strlcat (cmd, " ", sizeof (cmd));
785 Key_SetBinding (b, 0, cmd);
790 Writes lines containing "bind key value"
794 Key_WriteBindings (qfile_t *f)
798 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
799 if (keybindings[0][i])
800 FS_Printf(f, "bind %s \"%s\"\n",
801 Key_KeynumToString (i), keybindings[0][i]);
802 for (j = 1; j < 8; j++)
803 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
804 if (keybindings[j][i])
805 FS_Printf(f, "in_bind %d %s \"%s\"\n",
806 j, Key_KeynumToString (i), keybindings[j][i]);
815 for (i = 0; i < 32; i++) {
816 key_lines[i][0] = ']';
822 // init ascii characters in console mode
824 for (i = 32; i < 128; i++)
825 consolekeys[i] = true;
826 consolekeys[K_ENTER] = true; consolekeys[K_KP_ENTER] = true;
827 consolekeys[K_TAB] = true;
828 consolekeys[K_LEFTARROW] = true; consolekeys[K_KP_LEFTARROW] = true;
829 consolekeys[K_RIGHTARROW] = true; consolekeys[K_KP_RIGHTARROW] = true;
830 consolekeys[K_UPARROW] = true; consolekeys[K_KP_UPARROW] = true;
831 consolekeys[K_DOWNARROW] = true; consolekeys[K_KP_DOWNARROW] = true;
832 consolekeys[K_BACKSPACE] = true;
833 consolekeys[K_DEL] = true; consolekeys[K_KP_DEL] = true;
834 consolekeys[K_INS] = true; consolekeys[K_KP_INS] = true;
835 consolekeys[K_HOME] = true; consolekeys[K_KP_HOME] = true;
836 consolekeys[K_END] = true; consolekeys[K_KP_END] = true;
837 consolekeys[K_PGUP] = true; consolekeys[K_KP_PGUP] = true;
838 consolekeys[K_PGDN] = true; consolekeys[K_KP_PGDN] = true;
839 consolekeys[K_SHIFT] = true;
840 consolekeys[K_MWHEELUP] = true;
841 consolekeys[K_MWHEELDOWN] = true;
842 consolekeys[K_KP_PLUS] = true;
843 consolekeys[K_KP_MINUS] = true;
844 consolekeys[K_KP_DIVIDE] = true;
845 consolekeys[K_KP_MULTIPLY] = true;
846 consolekeys['`'] = false;
847 consolekeys['~'] = false;
849 menubound[K_ESCAPE] = true;
850 for (i = 0; i < 12; i++)
851 menubound[K_F1 + i] = true;
854 // register our functions
856 Cmd_AddCommand ("in_bind", Key_In_Bind_f);
857 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f);
858 Cmd_AddCommand ("in_bindmap", Key_In_Bindmap_f);
860 Cmd_AddCommand ("bind", Key_Bind_f);
861 Cmd_AddCommand ("unbind", Key_Unbind_f);
862 Cmd_AddCommand ("unbindall", Key_Unbindall_f);
868 Called by the system between frames for both key up and key down events
869 Should NOT be called during an interrupt!
873 Key_Event (int key, char ascii, qboolean down)
881 key_repeats[key] = 0;
885 if (key_count <= 0) {
886 return; // just catching keys for Con_NotifyBox
889 // update auto-repeat status
892 if (key_repeats[key] > 1) {
893 if ((key_consoleactive && !consolekeys[key]) ||
894 (!key_consoleactive && key_dest == key_game &&
895 (cls.state == ca_connected && cls.signon == SIGNONS)))
896 return; // ignore most autorepeats
904 // handle escape specially, so the user can never unbind it
906 if (key == K_ESCAPE) {
911 Key_Message (key, ascii);
914 MR_Keydown (key, ascii);
920 if(UI_Callback_IsSlotUsed(key_dest - 3))
921 UI_Callback_KeyDown (key, ascii);
923 Sys_Error ("Bad key_dest");
928 // console key is hardcoded, so the user can never unbind it
929 if (key == '`' || key == '~')
932 Con_ToggleConsole_f ();
938 if (!(kb = keybindings[key_bmap][key]))
939 kb = keybindings[key_bmap2][key];
940 if (kb && !strncmp(kb, "toggleconsole", strlen("toggleconsole")))
948 if (key_consoleactive && consolekeys[key] && down)
949 Key_Console (key, ascii);
953 // key up events only generate commands if the game key binding is a button
954 // command (leading + sign). These will occur even in console mode, to
955 // keep the character from continuing an action started before a console
956 // switch. Button commands include the kenum as a parameter, so multiple
957 // downs can be matched with ups
960 if (!(kb = keybindings[key_bmap][key]))
961 kb = keybindings[key_bmap2][key];
963 if (kb && kb[0] == '+') {
964 snprintf (cmd, sizeof(cmd), "-%s %i\n", kb + 1, key);
971 // during demo playback, most keys bring up the main menu
973 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game) {
979 // if not a consolekey, send to the interpreter no matter what mode is
981 if ((key_dest == key_menu && menubound[key])
982 || (key_consoleactive && !consolekeys[key])
983 || (key_dest == key_game &&
984 ((cls.state == ca_connected) || !consolekeys[key]))) {
985 if (!(kb = keybindings[key_bmap][key]))
986 kb = keybindings[key_bmap2][key];
988 if (kb[0] == '+') { // button commands add keynum as a parm
989 snprintf (cmd, sizeof(cmd), "%s %i\n", kb, key);
1000 return; // other systems only care about key
1005 Key_Message (key, ascii);
1008 MR_Keydown (key, ascii);
1012 Key_Console (key, ascii);
1015 if(UI_Callback_IsSlotUsed(key_dest - 3))
1016 UI_Callback_KeyDown (key, ascii);
1018 Sys_Error ("Bad key_dest");
1029 Key_ClearStates (void)
1033 for (i = 0; i < (int)(sizeof(keydown)/sizeof(keydown[0])); i++)