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},
195 {"SEMICOLON", ';'}, // because a raw semicolon seperates commands
199 {"APOSTROPHE", '\''},
205 ==============================================================================
207 LINE TYPING INTO THE CONSOLE
209 ==============================================================================
213 Key_ClearEditLine (int edit_line)
215 memset (key_lines[edit_line], '\0', MAX_INPUTLINE);
216 key_lines[edit_line][0] = ']';
222 Interactive line editing and console scrollback
226 Key_Console (int key, char ascii)
228 // LordHavoc: copied most of this from Q2 to improve keyboard handling
255 case K_KP_RIGHTARROW:
275 // LordHavoc: FIXME: implement this sometime
277 if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
280 if ((cbd = Sys_GetClipboardData()) != 0)
283 strtok(cbd, "\n\r\b");
285 if (i + key_linepos >= MAX_INPUTLINE)
286 i= MAX_INPUTLINE - key_linepos;
290 strcat(key_lines[edit_line], cbd);
303 Cbuf_AddText ("clear\n");
308 if (key == K_ENTER || key == K_KP_ENTER)
310 Cbuf_AddText (key_lines[edit_line]+1); // skip the ]
312 Con_Printf("%s\n",key_lines[edit_line]);
313 edit_line = (edit_line + 1) & 31;
314 history_line = 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)
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 (strlen(key_lines[edit_line]) == (size_t)key_linepos)
374 if (strlen(key_lines[(edit_line + 31) & 31]) <= (size_t)key_linepos)
375 return; // no character to get
377 key_lines[edit_line][key_linepos] = key_lines[(edit_line + 31) & 31][key_linepos];
379 key_lines[edit_line][key_linepos] = 0;
387 if (key == K_INS || key == K_KP_INS) // toggle insert mode
393 // End Advanced Console Editing
395 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
399 history_line = (history_line - 1) & 31;
400 } while (history_line != edit_line
401 && !key_lines[history_line][1]);
402 if (history_line == edit_line)
403 history_line = (edit_line+1)&31;
404 strcpy(key_lines[edit_line], key_lines[history_line]);
405 key_linepos = strlen(key_lines[edit_line]);
409 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
411 if (history_line == edit_line) return;
414 history_line = (history_line + 1) & 31;
416 while (history_line != edit_line
417 && !key_lines[history_line][1]);
418 if (history_line == edit_line)
420 key_lines[edit_line][0] = ']';
425 strcpy(key_lines[edit_line], key_lines[history_line]);
426 key_linepos = strlen(key_lines[edit_line]);
431 if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
433 con_backscroll += ((int) scr_conlines >> 4);
434 if (con_backscroll > con_totallines - (vid.conheight>>3) - 1)
435 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
439 if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
441 con_backscroll -= ((int) scr_conlines >> 4);
442 if (con_backscroll < 0)
447 if (key == K_HOME || key == K_KP_HOME)
449 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
453 if (key == K_END || key == K_KP_END)
460 if (ascii < 32 || ascii > 126)
463 if (key_linepos < MAX_INPUTLINE-1)
467 if (key_insert) // check insert mode
469 // can't do strcpy to move string to right
470 i = strlen(key_lines[edit_line]) - 1;
475 for (; i >= key_linepos; i--)
476 key_lines[edit_line][i + 1] = key_lines[edit_line][i];
479 // only null terminate if at the end
480 i = key_lines[edit_line][key_linepos];
481 key_lines[edit_line][key_linepos] = ascii;
485 key_lines[edit_line][key_linepos] = 0;
489 //============================================================================
492 char chat_buffer[MAX_INPUTLINE];
493 unsigned int chat_bufferlen = 0;
496 Key_Message (int key, char ascii)
499 if (key == K_ENTER) {
501 Cbuf_AddText ("say_team \"");
503 Cbuf_AddText ("say \"");
504 Cbuf_AddText (chat_buffer);
505 Cbuf_AddText ("\"\n");
513 if (key == K_ESCAPE) {
520 if (key == K_BACKSPACE) {
521 if (chat_bufferlen) {
523 chat_buffer[chat_bufferlen] = 0;
528 if (chat_bufferlen == sizeof (chat_buffer) - 1)
532 return; // non printable
534 chat_buffer[chat_bufferlen++] = ascii;
535 chat_buffer[chat_bufferlen] = 0;
538 //============================================================================
543 Returns a key number to be used to index keybindings[] by looking at
544 the given string. Single ascii characters return themselves, while
545 the K_* names are matched up.
549 Key_StringToKeynum (const char *str)
556 return tolower(str[0]);
558 for (kn = keynames; kn->name; kn++) {
559 if (!strcasecmp (str, kn->name))
567 Returns a string (either a single ascii char, or a K_* name) for the
569 FIXME: handle quote special (general escape sequence?)
573 Key_KeynumToString (int keynum)
576 static char tinystr[2];
579 return "<KEY NOT FOUND>";
580 if (keynum > 32 && keynum < 127) { // printable ascii
586 for (kn = keynames; kn->name; kn++)
587 if (keynum == kn->keynum)
590 return "<UNKNOWN KEYNUM>";
595 Key_SetBinding (int keynum, int bindmap, const char *binding)
604 if (keybindings[bindmap][keynum]) {
605 Z_Free (keybindings[bindmap][keynum]);
606 keybindings[bindmap][keynum] = NULL;
608 // allocate memory for new binding
609 l = strlen (binding);
610 new = Z_Malloc (l + 1);
611 strcpy (new, binding);
613 keybindings[bindmap][keynum] = new;
617 Key_In_Unbind_f (void)
621 if (Cmd_Argc () != 3) {
622 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
626 m = strtol(Cmd_Argv (1), NULL, 0);
627 if ((m < 0) || (m >= 8)) {
628 Con_Printf("%d isn't a valid bindmap\n", m);
632 b = Key_StringToKeynum (Cmd_Argv (2));
634 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
638 Key_SetBinding (b, m, "");
649 if (c != 3 && c != 4) {
650 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
654 m = strtol(Cmd_Argv (1), NULL, 0);
655 if ((m < 0) || (m >= 8)) {
656 Con_Printf("%d isn't a valid bindmap\n", m);
660 b = Key_StringToKeynum (Cmd_Argv (2));
662 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
667 if (keybindings[m][b])
668 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
670 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
673 // copy the rest of the command line
674 cmd[0] = 0; // start out with a null string
675 for (i = 3; i < c; i++) {
676 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
678 strlcat (cmd, " ", sizeof (cmd));
681 Key_SetBinding (b, m, cmd);
685 Key_In_Bindmap_f (void)
692 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
696 m1 = strtol(Cmd_Argv (1), NULL, 0);
697 if ((m1 < 0) || (m1 >= 8)) {
698 Con_Printf("%d isn't a valid bindmap\n", m1);
702 m2 = strtol(Cmd_Argv (2), NULL, 0);
703 if ((m2 < 0) || (m2 >= 8)) {
704 Con_Printf("%d isn't a valid bindmap\n", m2);
717 if (Cmd_Argc () != 2) {
718 Con_Print("unbind <key> : remove commands from a key\n");
722 b = Key_StringToKeynum (Cmd_Argv (1));
724 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
728 Key_SetBinding (b, 0, "");
732 Key_Unbindall_f (void)
736 for (j = 0; j < 8; j++)
737 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
738 if (keybindings[j][i])
739 Key_SetBinding (i, j, "");
751 if (c != 2 && c != 3) {
752 Con_Print("bind <key> [command] : attach a command to a key\n");
755 b = Key_StringToKeynum (Cmd_Argv (1));
757 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
762 if (keybindings[0][b])
763 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
765 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
768 // copy the rest of the command line
769 cmd[0] = 0; // start out with a null string
770 for (i = 2; i < c; i++) {
771 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
773 strlcat (cmd, " ", sizeof (cmd));
776 Key_SetBinding (b, 0, cmd);
781 Writes lines containing "bind key value"
785 Key_WriteBindings (qfile_t *f)
789 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
790 if (keybindings[0][i])
791 FS_Printf(f, "bind %s \"%s\"\n",
792 Key_KeynumToString (i), keybindings[0][i]);
793 for (j = 1; j < 8; j++)
794 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
795 if (keybindings[j][i])
796 FS_Printf(f, "in_bind %d %s \"%s\"\n",
797 j, Key_KeynumToString (i), keybindings[j][i]);
806 for (i = 0; i < 32; i++) {
807 key_lines[i][0] = ']';
813 // init ascii characters in console mode
815 for (i = 32; i < 128; i++)
816 consolekeys[i] = true;
817 consolekeys[K_ENTER] = true; consolekeys[K_KP_ENTER] = true;
818 consolekeys[K_TAB] = true;
819 consolekeys[K_LEFTARROW] = true; consolekeys[K_KP_LEFTARROW] = true;
820 consolekeys[K_RIGHTARROW] = true; consolekeys[K_KP_RIGHTARROW] = true;
821 consolekeys[K_UPARROW] = true; consolekeys[K_KP_UPARROW] = true;
822 consolekeys[K_DOWNARROW] = true; consolekeys[K_KP_DOWNARROW] = true;
823 consolekeys[K_BACKSPACE] = true;
824 consolekeys[K_DEL] = true; consolekeys[K_KP_DEL] = true;
825 consolekeys[K_INS] = true; consolekeys[K_KP_INS] = true;
826 consolekeys[K_HOME] = true; consolekeys[K_KP_HOME] = true;
827 consolekeys[K_END] = true; consolekeys[K_KP_END] = true;
828 consolekeys[K_PGUP] = true; consolekeys[K_KP_PGUP] = true;
829 consolekeys[K_PGDN] = true; consolekeys[K_KP_PGDN] = true;
830 consolekeys[K_SHIFT] = true;
831 consolekeys[K_MWHEELUP] = true;
832 consolekeys[K_MWHEELDOWN] = true;
833 consolekeys[K_KP_PLUS] = true;
834 consolekeys[K_KP_MINUS] = true;
835 consolekeys[K_KP_DIVIDE] = true;
836 consolekeys[K_KP_MULTIPLY] = true;
837 consolekeys['`'] = false;
838 consolekeys['~'] = false;
840 menubound[K_ESCAPE] = true;
841 for (i = 0; i < 12; i++)
842 menubound[K_F1 + i] = true;
845 // register our functions
847 Cmd_AddCommand ("in_bind", Key_In_Bind_f);
848 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f);
849 Cmd_AddCommand ("in_bindmap", Key_In_Bindmap_f);
851 Cmd_AddCommand ("bind", Key_Bind_f);
852 Cmd_AddCommand ("unbind", Key_Unbind_f);
853 Cmd_AddCommand ("unbindall", Key_Unbindall_f);
859 Called by the system between frames for both key up and key down events
860 Should NOT be called during an interrupt!
864 Key_Event (int key, char ascii, qboolean down)
872 key_repeats[key] = 0;
876 if (key_count <= 0) {
877 return; // just catching keys for Con_NotifyBox
880 // update auto-repeat status
883 if (key_repeats[key] > 1) {
884 if ((key_consoleactive && !consolekeys[key]) ||
885 (key_dest == key_game &&
886 (cls.state == ca_connected && cls.signon == SIGNONS)))
887 return; // ignore most autorepeats
895 // handle escape specially, so the user can never unbind it
897 if (key == K_ESCAPE) {
902 Key_Message (key, ascii);
905 MR_Keydown (key, ascii);
911 if(UI_Callback_IsSlotUsed(key_dest - 3))
912 UI_Callback_KeyDown (key, ascii);
914 Sys_Error ("Bad key_dest");
919 // console key is hardcoded, so the user can never unbind it
920 if (key == '`' || key == '~')
923 Con_ToggleConsole_f ();
929 if (!(kb = keybindings[key_bmap][key]))
930 kb = keybindings[key_bmap2][key];
931 if (kb && !strncmp(kb, "toggleconsole", strlen("toggleconsole")))
939 if (key_consoleactive && consolekeys[key] && down)
940 Key_Console (key, ascii);
944 // key up events only generate commands if the game key binding is a button
945 // command (leading + sign). These will occur even in console mode, to
946 // keep the character from continuing an action started before a console
947 // switch. Button commands include the kenum as a parameter, so multiple
948 // downs can be matched with ups
951 if (!(kb = keybindings[key_bmap][key]))
952 kb = keybindings[key_bmap2][key];
954 if (kb && kb[0] == '+') {
955 snprintf (cmd, sizeof(cmd), "-%s %i\n", kb + 1, key);
962 // during demo playback, most keys bring up the main menu
964 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game) {
970 // if not a consolekey, send to the interpreter no matter what mode is
972 if ((key_dest == key_menu && menubound[key])
973 || (key_consoleactive && !consolekeys[key])
974 || (key_dest == key_game &&
975 ((cls.state == ca_connected) || !consolekeys[key]))) {
976 if (!(kb = keybindings[key_bmap][key]))
977 kb = keybindings[key_bmap2][key];
979 if (kb[0] == '+') { // button commands add keynum as a parm
980 snprintf (cmd, sizeof(cmd), "%s %i\n", kb, key);
991 return; // other systems only care about key
996 Key_Message (key, ascii);
999 MR_Keydown (key, ascii);
1003 Key_Console (key, ascii);
1006 if(UI_Callback_IsSlotUsed(key_dest - 3))
1007 UI_Callback_KeyDown (key, ascii);
1009 Sys_Error ("Bad key_dest");
1020 Key_ClearStates (void)
1024 for (i = 0; i < (int)(sizeof(keydown)/sizeof(keydown[0])); i++)