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
31 char key_lines[32][MAX_INPUTLINE];
33 static int ctrl_down = false;
34 static int key_lastpress;
35 int key_insert; // insert key toggle (for editing)
41 int key_consoleactive;
43 static int key_count; // incremented every key event
44 static int key_bmap, key_bmap2;
46 char *keybindings[8][1024];
47 static qboolean consolekeys[1024]; // if true, can't be rebound while in
49 static qboolean menubound[1024]; // if true, can't be rebound while in
51 static unsigned int key_repeats[1024]; // if > 1, it is autorepeating
52 static qboolean keydown[1024];
59 static const keyname_t keynames[] = {
64 {"BACKSPACE", K_BACKSPACE},
65 {"UPARROW", K_UPARROW},
66 {"DOWNARROW", K_DOWNARROW},
67 {"LEFTARROW", K_LEFTARROW},
68 {"RIGHTARROW", K_RIGHTARROW},
96 {"MWHEELUP", K_MWHEELUP},
97 {"MWHEELDOWN", K_MWHEELDOWN},
100 {"MOUSE2", K_MOUSE2},
101 {"MOUSE3", K_MOUSE3},
102 {"MOUSE4", K_MOUSE4},
103 {"MOUSE5", K_MOUSE5},
104 {"MOUSE6", K_MOUSE6},
105 {"MOUSE7", K_MOUSE7},
106 {"MOUSE8", K_MOUSE8},
107 {"MOUSE9", K_MOUSE9},
108 {"MOUSE10", K_MOUSE10},
109 {"MOUSE11", K_MOUSE11},
110 {"MOUSE12", K_MOUSE12},
111 {"MOUSE13", K_MOUSE13},
112 {"MOUSE14", K_MOUSE14},
113 {"MOUSE15", K_MOUSE15},
114 {"MOUSE16", K_MOUSE16},
116 {"NUMLOCK", K_NUMLOCK},
117 {"CAPSLOCK", K_CAPSLOCK},
118 {"SCROLLOCK", K_SCROLLOCK},
120 {"KP_HOME", K_KP_HOME },
121 {"KP_UPARROW", K_KP_UPARROW },
122 {"KP_PGUP", K_KP_PGUP },
123 {"KP_LEFTARROW", K_KP_LEFTARROW },
124 {"KP_RIGHTARROW", K_KP_RIGHTARROW },
125 {"KP_END", K_KP_END },
126 {"KP_DOWNARROW", K_KP_DOWNARROW },
127 {"KP_PGDN", K_KP_PGDN },
128 {"KP_INS", K_KP_INS },
129 {"KP_DEL", K_KP_DEL },
130 {"KP_SLASH", K_KP_SLASH },
142 {"KP_PERIOD", K_KP_PERIOD},
143 {"KP_DIVIDE", K_KP_DIVIDE},
144 {"KP_MULTIPLY", K_KP_MULTIPLY},
145 {"KP_MINUS", K_KP_MINUS},
146 {"KP_PLUS", K_KP_PLUS},
147 {"KP_ENTER", K_KP_ENTER},
148 {"KP_EQUALS", K_KP_EQUALS},
200 {"SEMICOLON", ';'}, // because a raw semicolon seperates commands
204 {"APOSTROPHE", '\''},
210 ==============================================================================
212 LINE TYPING INTO THE CONSOLE
214 ==============================================================================
218 Key_ClearEditLine (int edit_line)
220 memset (key_lines[edit_line], '\0', MAX_INPUTLINE);
221 key_lines[edit_line][0] = ']';
227 Interactive line editing and console scrollback
231 Key_Console (int key, char ascii)
233 // LordHavoc: copied most of this from Q2 to improve keyboard handling
260 case K_KP_RIGHTARROW:
280 if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
283 if ((cbd = Sys_GetClipboardData()) != 0)
286 strtok(cbd, "\n\r\b");
288 if (i + key_linepos >= MAX_INPUTLINE)
289 i= MAX_INPUTLINE - key_linepos;
293 strcat(key_lines[edit_line], cbd);
305 Cbuf_AddText ("clear\n");
310 if (key == K_ENTER || key == K_KP_ENTER)
312 Cbuf_AddText (key_lines[edit_line]+1); // skip the ]
314 Con_Printf("%s\n",key_lines[edit_line]);
315 edit_line = (edit_line + 1) & 31;
316 history_line = edit_line;
317 key_lines[edit_line][0] = ']';
318 key_lines[edit_line][1] = 0; // EvilTypeGuy: null terminate
320 // force an update, because the command may take some time
321 if (cls.state == ca_disconnected)
331 // Enhanced command completion
332 // by EvilTypeGuy eviltypeguy@qeradiant.com
333 // Thanks to Fett, Taniwha
334 Con_CompleteCommandLine();
338 // Advanced Console Editing by Radix radix@planetquake.com
339 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
341 // left arrow will just move left one without erasing, backspace will
342 // actually erase charcter
343 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
350 // delete char before cursor
351 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
355 strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
361 // delete char on cursor
362 if (key == K_DEL || key == K_KP_DEL)
364 if ((size_t)key_linepos < strlen(key_lines[edit_line]))
365 strcpy(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1);
370 // if we're at the end, get one character from previous line,
371 // otherwise just go right one
372 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
374 if (strlen(key_lines[edit_line]) == (size_t)key_linepos)
376 if (strlen(key_lines[(edit_line + 31) & 31]) <= (size_t)key_linepos)
377 return; // no character to get
379 key_lines[edit_line][key_linepos] = key_lines[(edit_line + 31) & 31][key_linepos];
381 key_lines[edit_line][key_linepos] = 0;
389 if (key == K_INS || key == K_KP_INS) // toggle insert mode
395 // End Advanced Console Editing
397 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
401 history_line = (history_line - 1) & 31;
402 } while (history_line != edit_line
403 && !key_lines[history_line][1]);
404 if (history_line == edit_line)
405 history_line = (edit_line+1)&31;
406 strcpy(key_lines[edit_line], key_lines[history_line]);
407 key_linepos = strlen(key_lines[edit_line]);
411 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
413 if (history_line == edit_line) return;
416 history_line = (history_line + 1) & 31;
418 while (history_line != edit_line
419 && !key_lines[history_line][1]);
420 if (history_line == edit_line)
422 key_lines[edit_line][0] = ']';
427 strcpy(key_lines[edit_line], key_lines[history_line]);
428 key_linepos = strlen(key_lines[edit_line]);
433 if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
435 con_backscroll += ((int) scr_conlines >> 4);
436 if (con_backscroll > con_totallines - (vid.conheight>>3) - 1)
437 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
441 if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
443 con_backscroll -= ((int) scr_conlines >> 4);
444 if (con_backscroll < 0)
449 if (key == K_HOME || key == K_KP_HOME)
451 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
455 if (key == K_END || key == K_KP_END)
462 if (ascii < 32 || ascii > 126)
465 if (key_linepos < MAX_INPUTLINE-1)
469 if (key_insert) // check insert mode
471 // can't do strcpy to move string to right
472 i = strlen(key_lines[edit_line]) - 1;
477 for (; i >= key_linepos; i--)
478 key_lines[edit_line][i + 1] = key_lines[edit_line][i];
481 // only null terminate if at the end
482 i = key_lines[edit_line][key_linepos];
483 key_lines[edit_line][key_linepos] = ascii;
487 key_lines[edit_line][key_linepos] = 0;
491 //============================================================================
494 char chat_buffer[MAX_INPUTLINE];
495 unsigned int chat_bufferlen = 0;
498 Key_Message (int key, char ascii)
503 Cmd_ForwardStringToServer(va("%s %s", chat_team ? "say_team" : "say ", chat_buffer));
511 if (key == K_ESCAPE) {
518 if (key == K_BACKSPACE) {
519 if (chat_bufferlen) {
521 chat_buffer[chat_bufferlen] = 0;
526 if (chat_bufferlen == sizeof (chat_buffer) - 1)
530 return; // non printable
532 chat_buffer[chat_bufferlen++] = ascii;
533 chat_buffer[chat_bufferlen] = 0;
536 //============================================================================
541 Returns a key number to be used to index keybindings[] by looking at
542 the given string. Single ascii characters return themselves, while
543 the K_* names are matched up.
547 Key_StringToKeynum (const char *str)
554 return tolower(str[0]);
556 for (kn = keynames; kn->name; kn++) {
557 if (!strcasecmp (str, kn->name))
565 Returns a string (either a single ascii char, or a K_* name) for the
567 FIXME: handle quote special (general escape sequence?)
571 Key_KeynumToString (int keynum)
574 static char tinystr[2];
577 return "<KEY NOT FOUND>";
578 if (keynum > 32 && keynum < 127) { // printable ascii
584 for (kn = keynames; kn->name; kn++)
585 if (keynum == kn->keynum)
588 return "<UNKNOWN KEYNUM>";
593 Key_SetBinding (int keynum, int bindmap, const char *binding)
602 if (keybindings[bindmap][keynum]) {
603 Z_Free (keybindings[bindmap][keynum]);
604 keybindings[bindmap][keynum] = NULL;
606 // allocate memory for new binding
607 l = strlen (binding);
608 new = Z_Malloc (l + 1);
609 strcpy (new, binding);
611 keybindings[bindmap][keynum] = new;
615 Key_In_Unbind_f (void)
619 if (Cmd_Argc () != 3) {
620 Con_Print("in_unbind <bindmap> <key> : remove commands from 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));
636 Key_SetBinding (b, m, "");
647 if (c != 3 && c != 4) {
648 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
652 m = strtol(Cmd_Argv (1), NULL, 0);
653 if ((m < 0) || (m >= 8)) {
654 Con_Printf("%d isn't a valid bindmap\n", m);
658 b = Key_StringToKeynum (Cmd_Argv (2));
660 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
665 if (keybindings[m][b])
666 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
668 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
671 // copy the rest of the command line
672 cmd[0] = 0; // start out with a null string
673 for (i = 3; i < c; i++) {
674 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
676 strlcat (cmd, " ", sizeof (cmd));
679 Key_SetBinding (b, m, cmd);
683 Key_In_Bindmap_f (void)
690 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
694 m1 = strtol(Cmd_Argv (1), NULL, 0);
695 if ((m1 < 0) || (m1 >= 8)) {
696 Con_Printf("%d isn't a valid bindmap\n", m1);
700 m2 = strtol(Cmd_Argv (2), NULL, 0);
701 if ((m2 < 0) || (m2 >= 8)) {
702 Con_Printf("%d isn't a valid bindmap\n", m2);
715 if (Cmd_Argc () != 2) {
716 Con_Print("unbind <key> : remove commands from a key\n");
720 b = Key_StringToKeynum (Cmd_Argv (1));
722 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
726 Key_SetBinding (b, 0, "");
730 Key_Unbindall_f (void)
734 for (j = 0; j < 8; j++)
735 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
736 if (keybindings[j][i])
737 Key_SetBinding (i, j, "");
749 if (c != 2 && c != 3) {
750 Con_Print("bind <key> [command] : attach a command to a key\n");
753 b = Key_StringToKeynum (Cmd_Argv (1));
755 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
760 if (keybindings[0][b])
761 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
763 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
766 // copy the rest of the command line
767 cmd[0] = 0; // start out with a null string
768 for (i = 2; i < c; i++) {
769 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
771 strlcat (cmd, " ", sizeof (cmd));
774 Key_SetBinding (b, 0, cmd);
779 Writes lines containing "bind key value"
783 Key_WriteBindings (qfile_t *f)
787 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
788 if (keybindings[0][i])
789 FS_Printf(f, "bind \"%s\" \"%s\"\n",
790 Key_KeynumToString (i), keybindings[0][i]);
791 for (j = 1; j < 8; j++)
792 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
793 if (keybindings[j][i])
794 FS_Printf(f, "in_bind %d \"%s\" \"%s\"\n",
795 j, Key_KeynumToString (i), keybindings[j][i]);
804 for (i = 0; i < 32; i++) {
805 key_lines[i][0] = ']';
811 // init ascii characters in console mode
813 for (i = 32; i < 128; i++)
814 consolekeys[i] = true;
815 consolekeys[K_ENTER] = true; consolekeys[K_KP_ENTER] = true;
816 consolekeys[K_TAB] = true;
817 consolekeys[K_LEFTARROW] = true; consolekeys[K_KP_LEFTARROW] = true;
818 consolekeys[K_RIGHTARROW] = true; consolekeys[K_KP_RIGHTARROW] = true;
819 consolekeys[K_UPARROW] = true; consolekeys[K_KP_UPARROW] = true;
820 consolekeys[K_DOWNARROW] = true; consolekeys[K_KP_DOWNARROW] = true;
821 consolekeys[K_BACKSPACE] = true;
822 consolekeys[K_DEL] = true; consolekeys[K_KP_DEL] = true;
823 consolekeys[K_INS] = true; consolekeys[K_KP_INS] = true;
824 consolekeys[K_HOME] = true; consolekeys[K_KP_HOME] = true;
825 consolekeys[K_END] = true; consolekeys[K_KP_END] = true;
826 consolekeys[K_PGUP] = true; consolekeys[K_KP_PGUP] = true;
827 consolekeys[K_PGDN] = true; consolekeys[K_KP_PGDN] = true;
828 consolekeys[K_SHIFT] = true;
829 consolekeys[K_MWHEELUP] = true;
830 consolekeys[K_MWHEELDOWN] = true;
831 consolekeys[K_KP_PLUS] = true;
832 consolekeys[K_KP_MINUS] = true;
833 consolekeys[K_KP_DIVIDE] = true;
834 consolekeys[K_KP_MULTIPLY] = true;
835 consolekeys['`'] = false;
836 consolekeys['~'] = false;
838 menubound[K_ESCAPE] = true;
839 for (i = 0; i < 12; i++)
840 menubound[K_F1 + i] = true;
843 // register our functions
845 Cmd_AddCommand ("in_bind", Key_In_Bind_f);
846 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f);
847 Cmd_AddCommand ("in_bindmap", Key_In_Bindmap_f);
849 Cmd_AddCommand ("bind", Key_Bind_f);
850 Cmd_AddCommand ("unbind", Key_Unbind_f);
851 Cmd_AddCommand ("unbindall", Key_Unbindall_f);
857 Called by the system between frames for both key up and key down events
858 Should NOT be called during an interrupt!
862 Key_Event (int key, char ascii, qboolean down)
870 key_repeats[key] = 0;
874 if (key_count <= 0) {
875 return; // just catching keys for Con_NotifyBox
878 // update auto-repeat status
881 if (key_repeats[key] > 1) {
882 if ((key_consoleactive && !consolekeys[key]) ||
883 (!key_consoleactive && key_dest == key_game &&
884 (cls.state == ca_connected && cls.signon == SIGNONS)))
885 return; // ignore most autorepeats
893 // handle escape specially, so the user can never unbind it
895 if (key == K_ESCAPE) {
900 Key_Message (key, ascii);
903 MR_Keydown (key, ascii);
909 if(UI_Callback_IsSlotUsed(key_dest - 3))
910 UI_Callback_KeyDown (key, ascii);
912 Sys_Error ("Bad key_dest");
917 // console key is hardcoded, so the user can never unbind it
918 if (key == '`' || key == '~')
921 Con_ToggleConsole_f ();
927 if (!(kb = keybindings[key_bmap][key]))
928 kb = keybindings[key_bmap2][key];
929 if (kb && !strncmp(kb, "toggleconsole", strlen("toggleconsole")))
937 if (key_consoleactive && consolekeys[key] && down)
938 Key_Console (key, ascii);
942 // key up events only generate commands if the game key binding is a button
943 // command (leading + sign). These will occur even in console mode, to
944 // keep the character from continuing an action started before a console
945 // switch. Button commands include the kenum as a parameter, so multiple
946 // downs can be matched with ups
949 if (!(kb = keybindings[key_bmap][key]))
950 kb = keybindings[key_bmap2][key];
952 if (kb && kb[0] == '+') {
953 snprintf (cmd, sizeof(cmd), "-%s %i\n", kb + 1, key);
960 // during demo playback, most keys bring up the main menu
962 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game) {
968 // if not a consolekey, send to the interpreter no matter what mode is
970 if ((key_dest == key_menu && menubound[key])
971 || (key_consoleactive && !consolekeys[key])
972 || (key_dest == key_game &&
973 ((cls.state == ca_connected) || !consolekeys[key]))) {
974 if (!(kb = keybindings[key_bmap][key]))
975 kb = keybindings[key_bmap2][key];
977 if (kb[0] == '+') { // button commands add keynum as a parm
978 snprintf (cmd, sizeof(cmd), "%s %i\n", kb, key);
989 return; // other systems only care about key
994 Key_Message (key, ascii);
997 MR_Keydown (key, ascii);
1000 Key_Console (key, ascii);
1003 if(UI_Callback_IsSlotUsed(key_dest - 3))
1004 UI_Callback_KeyDown (key, ascii);
1006 Sys_Error ("Bad key_dest");
1017 Key_ClearStates (void)
1021 for (i = 0; i < (int)(sizeof(keydown)/sizeof(keydown[0])); i++)