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 if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
278 if ((cbd = Sys_GetClipboardData()) != 0)
281 strtok(cbd, "\n\r\b");
283 if (i + key_linepos >= MAX_INPUTLINE)
284 i= MAX_INPUTLINE - key_linepos;
288 strcat(key_lines[edit_line], cbd);
300 Cbuf_AddText ("clear\n");
305 if (key == K_ENTER || key == K_KP_ENTER)
307 Cbuf_AddText (key_lines[edit_line]+1); // skip the ]
309 Con_Printf("%s\n",key_lines[edit_line]);
310 edit_line = (edit_line + 1) & 31;
311 history_line = edit_line;
312 key_lines[edit_line][0] = ']';
313 key_lines[edit_line][1] = 0; // EvilTypeGuy: null terminate
315 // force an update, because the command may take some time
316 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 (strlen(key_lines[edit_line]) == (size_t)key_linepos)
371 if (strlen(key_lines[(edit_line + 31) & 31]) <= (size_t)key_linepos)
372 return; // no character to get
374 key_lines[edit_line][key_linepos] = key_lines[(edit_line + 31) & 31][key_linepos];
376 key_lines[edit_line][key_linepos] = 0;
384 if (key == K_INS || key == K_KP_INS) // toggle insert mode
390 // End Advanced Console Editing
392 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
396 history_line = (history_line - 1) & 31;
397 } while (history_line != edit_line
398 && !key_lines[history_line][1]);
399 if (history_line == edit_line)
400 history_line = (edit_line+1)&31;
401 strcpy(key_lines[edit_line], key_lines[history_line]);
402 key_linepos = strlen(key_lines[edit_line]);
406 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
408 if (history_line == edit_line) return;
411 history_line = (history_line + 1) & 31;
413 while (history_line != edit_line
414 && !key_lines[history_line][1]);
415 if (history_line == edit_line)
417 key_lines[edit_line][0] = ']';
422 strcpy(key_lines[edit_line], key_lines[history_line]);
423 key_linepos = strlen(key_lines[edit_line]);
428 if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
430 con_backscroll += ((int) scr_conlines >> 4);
431 if (con_backscroll > con_totallines - (vid.conheight>>3) - 1)
432 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
436 if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
438 con_backscroll -= ((int) scr_conlines >> 4);
439 if (con_backscroll < 0)
444 if (key == K_HOME || key == K_KP_HOME)
446 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
450 if (key == K_END || key == K_KP_END)
457 if (ascii < 32 || ascii > 126)
460 if (key_linepos < MAX_INPUTLINE-1)
464 if (key_insert) // check insert mode
466 // can't do strcpy to move string to right
467 i = strlen(key_lines[edit_line]) - 1;
472 for (; i >= key_linepos; i--)
473 key_lines[edit_line][i + 1] = key_lines[edit_line][i];
476 // only null terminate if at the end
477 i = key_lines[edit_line][key_linepos];
478 key_lines[edit_line][key_linepos] = ascii;
482 key_lines[edit_line][key_linepos] = 0;
486 //============================================================================
489 char chat_buffer[MAX_INPUTLINE];
490 unsigned int chat_bufferlen = 0;
493 Key_Message (int key, char ascii)
496 if (key == K_ENTER) {
498 Cbuf_AddText ("say_team \"");
500 Cbuf_AddText ("say \"");
501 Cbuf_AddText (chat_buffer);
502 Cbuf_AddText ("\"\n");
510 if (key == K_ESCAPE) {
517 if (key == K_BACKSPACE) {
518 if (chat_bufferlen) {
520 chat_buffer[chat_bufferlen] = 0;
525 if (chat_bufferlen == sizeof (chat_buffer) - 1)
529 return; // non printable
531 chat_buffer[chat_bufferlen++] = ascii;
532 chat_buffer[chat_bufferlen] = 0;
535 //============================================================================
540 Returns a key number to be used to index keybindings[] by looking at
541 the given string. Single ascii characters return themselves, while
542 the K_* names are matched up.
546 Key_StringToKeynum (const char *str)
553 return tolower(str[0]);
555 for (kn = keynames; kn->name; kn++) {
556 if (!strcasecmp (str, kn->name))
564 Returns a string (either a single ascii char, or a K_* name) for the
566 FIXME: handle quote special (general escape sequence?)
570 Key_KeynumToString (int keynum)
573 static char tinystr[2];
576 return "<KEY NOT FOUND>";
577 if (keynum > 32 && keynum < 127) { // printable ascii
583 for (kn = keynames; kn->name; kn++)
584 if (keynum == kn->keynum)
587 return "<UNKNOWN KEYNUM>";
592 Key_SetBinding (int keynum, int bindmap, const char *binding)
601 if (keybindings[bindmap][keynum]) {
602 Z_Free (keybindings[bindmap][keynum]);
603 keybindings[bindmap][keynum] = NULL;
605 // allocate memory for new binding
606 l = strlen (binding);
607 new = Z_Malloc (l + 1);
608 strcpy (new, binding);
610 keybindings[bindmap][keynum] = new;
614 Key_In_Unbind_f (void)
618 if (Cmd_Argc () != 3) {
619 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
623 m = strtol(Cmd_Argv (1), NULL, 0);
624 if ((m < 0) || (m >= 8)) {
625 Con_Printf("%d isn't a valid bindmap\n", m);
629 b = Key_StringToKeynum (Cmd_Argv (2));
631 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
635 Key_SetBinding (b, m, "");
646 if (c != 3 && c != 4) {
647 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
651 m = strtol(Cmd_Argv (1), NULL, 0);
652 if ((m < 0) || (m >= 8)) {
653 Con_Printf("%d isn't a valid bindmap\n", m);
657 b = Key_StringToKeynum (Cmd_Argv (2));
659 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
664 if (keybindings[m][b])
665 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
667 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
670 // copy the rest of the command line
671 cmd[0] = 0; // start out with a null string
672 for (i = 3; i < c; i++) {
673 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
675 strlcat (cmd, " ", sizeof (cmd));
678 Key_SetBinding (b, m, cmd);
682 Key_In_Bindmap_f (void)
689 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
693 m1 = strtol(Cmd_Argv (1), NULL, 0);
694 if ((m1 < 0) || (m1 >= 8)) {
695 Con_Printf("%d isn't a valid bindmap\n", m1);
699 m2 = strtol(Cmd_Argv (2), NULL, 0);
700 if ((m2 < 0) || (m2 >= 8)) {
701 Con_Printf("%d isn't a valid bindmap\n", m2);
714 if (Cmd_Argc () != 2) {
715 Con_Print("unbind <key> : remove commands from a key\n");
719 b = Key_StringToKeynum (Cmd_Argv (1));
721 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
725 Key_SetBinding (b, 0, "");
729 Key_Unbindall_f (void)
733 for (j = 0; j < 8; j++)
734 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
735 if (keybindings[j][i])
736 Key_SetBinding (i, j, "");
748 if (c != 2 && c != 3) {
749 Con_Print("bind <key> [command] : attach a command to a key\n");
752 b = Key_StringToKeynum (Cmd_Argv (1));
754 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
759 if (keybindings[0][b])
760 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
762 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
765 // copy the rest of the command line
766 cmd[0] = 0; // start out with a null string
767 for (i = 2; i < c; i++) {
768 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
770 strlcat (cmd, " ", sizeof (cmd));
773 Key_SetBinding (b, 0, cmd);
778 Writes lines containing "bind key value"
782 Key_WriteBindings (qfile_t *f)
786 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
787 if (keybindings[0][i])
788 FS_Printf(f, "bind %s \"%s\"\n",
789 Key_KeynumToString (i), keybindings[0][i]);
790 for (j = 1; j < 8; j++)
791 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
792 if (keybindings[j][i])
793 FS_Printf(f, "in_bind %d %s \"%s\"\n",
794 j, Key_KeynumToString (i), keybindings[j][i]);
803 for (i = 0; i < 32; i++) {
804 key_lines[i][0] = ']';
810 // init ascii characters in console mode
812 for (i = 32; i < 128; i++)
813 consolekeys[i] = true;
814 consolekeys[K_ENTER] = true; consolekeys[K_KP_ENTER] = true;
815 consolekeys[K_TAB] = true;
816 consolekeys[K_LEFTARROW] = true; consolekeys[K_KP_LEFTARROW] = true;
817 consolekeys[K_RIGHTARROW] = true; consolekeys[K_KP_RIGHTARROW] = true;
818 consolekeys[K_UPARROW] = true; consolekeys[K_KP_UPARROW] = true;
819 consolekeys[K_DOWNARROW] = true; consolekeys[K_KP_DOWNARROW] = true;
820 consolekeys[K_BACKSPACE] = true;
821 consolekeys[K_DEL] = true; consolekeys[K_KP_DEL] = true;
822 consolekeys[K_INS] = true; consolekeys[K_KP_INS] = true;
823 consolekeys[K_HOME] = true; consolekeys[K_KP_HOME] = true;
824 consolekeys[K_END] = true; consolekeys[K_KP_END] = true;
825 consolekeys[K_PGUP] = true; consolekeys[K_KP_PGUP] = true;
826 consolekeys[K_PGDN] = true; consolekeys[K_KP_PGDN] = true;
827 consolekeys[K_SHIFT] = true;
828 consolekeys[K_MWHEELUP] = true;
829 consolekeys[K_MWHEELDOWN] = true;
830 consolekeys[K_KP_PLUS] = true;
831 consolekeys[K_KP_MINUS] = true;
832 consolekeys[K_KP_DIVIDE] = true;
833 consolekeys[K_KP_MULTIPLY] = true;
834 consolekeys['`'] = false;
835 consolekeys['~'] = false;
837 menubound[K_ESCAPE] = true;
838 for (i = 0; i < 12; i++)
839 menubound[K_F1 + i] = true;
842 // register our functions
844 Cmd_AddCommand ("in_bind", Key_In_Bind_f);
845 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f);
846 Cmd_AddCommand ("in_bindmap", Key_In_Bindmap_f);
848 Cmd_AddCommand ("bind", Key_Bind_f);
849 Cmd_AddCommand ("unbind", Key_Unbind_f);
850 Cmd_AddCommand ("unbindall", Key_Unbindall_f);
856 Called by the system between frames for both key up and key down events
857 Should NOT be called during an interrupt!
861 Key_Event (int key, char ascii, qboolean down)
869 key_repeats[key] = 0;
873 if (key_count <= 0) {
874 return; // just catching keys for Con_NotifyBox
877 // update auto-repeat status
880 if (key_repeats[key] > 1) {
881 if ((key_consoleactive && !consolekeys[key]) ||
882 (!key_consoleactive && key_dest == key_game &&
883 (cls.state == ca_connected && cls.signon == SIGNONS)))
884 return; // ignore most autorepeats
892 // handle escape specially, so the user can never unbind it
894 if (key == K_ESCAPE) {
899 Key_Message (key, ascii);
902 MR_Keydown (key, ascii);
908 if(UI_Callback_IsSlotUsed(key_dest - 3))
909 UI_Callback_KeyDown (key, ascii);
911 Sys_Error ("Bad key_dest");
916 // console key is hardcoded, so the user can never unbind it
917 if (key == '`' || key == '~')
920 Con_ToggleConsole_f ();
926 if (!(kb = keybindings[key_bmap][key]))
927 kb = keybindings[key_bmap2][key];
928 if (kb && !strncmp(kb, "toggleconsole", strlen("toggleconsole")))
936 if (key_consoleactive && consolekeys[key] && down)
937 Key_Console (key, ascii);
941 // key up events only generate commands if the game key binding is a button
942 // command (leading + sign). These will occur even in console mode, to
943 // keep the character from continuing an action started before a console
944 // switch. Button commands include the kenum as a parameter, so multiple
945 // downs can be matched with ups
948 if (!(kb = keybindings[key_bmap][key]))
949 kb = keybindings[key_bmap2][key];
951 if (kb && kb[0] == '+') {
952 snprintf (cmd, sizeof(cmd), "-%s %i\n", kb + 1, key);
959 // during demo playback, most keys bring up the main menu
961 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game) {
967 // if not a consolekey, send to the interpreter no matter what mode is
969 if ((key_dest == key_menu && menubound[key])
970 || (key_consoleactive && !consolekeys[key])
971 || (key_dest == key_game &&
972 ((cls.state == ca_connected) || !consolekeys[key]))) {
973 if (!(kb = keybindings[key_bmap][key]))
974 kb = keybindings[key_bmap2][key];
976 if (kb[0] == '+') { // button commands add keynum as a parm
977 snprintf (cmd, sizeof(cmd), "%s %i\n", kb, key);
988 return; // other systems only care about key
993 Key_Message (key, ascii);
996 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++)