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 = true; // 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)
328 // Enhanced command completion
329 // by EvilTypeGuy eviltypeguy@qeradiant.com
330 // Thanks to Fett, Taniwha
331 Con_CompleteCommandLine();
335 // Advanced Console Editing by Radix radix@planetquake.com
336 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
338 // left arrow will just move left one without erasing, backspace will
339 // actually erase charcter
340 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
347 // delete char before cursor
348 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
352 strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
358 // delete char on cursor
359 if (key == K_DEL || key == K_KP_DEL)
361 if ((size_t)key_linepos < strlen(key_lines[edit_line]))
362 strcpy(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1);
367 // if we're at the end, get one character from previous line,
368 // otherwise just go right one
369 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
371 if ((size_t)key_linepos < strlen(key_lines[edit_line]))
377 if (key == K_INS || key == K_KP_INS) // toggle insert mode
383 // End Advanced Console Editing
385 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
389 history_line = (history_line - 1) & 31;
390 } while (history_line != edit_line
391 && !key_lines[history_line][1]);
392 if (history_line == edit_line)
393 history_line = (edit_line+1)&31;
394 strcpy(key_lines[edit_line], key_lines[history_line]);
395 key_linepos = strlen(key_lines[edit_line]);
399 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
401 if (history_line == edit_line) return;
404 history_line = (history_line + 1) & 31;
406 while (history_line != edit_line
407 && !key_lines[history_line][1]);
408 if (history_line == edit_line)
410 key_lines[edit_line][0] = ']';
415 strcpy(key_lines[edit_line], key_lines[history_line]);
416 key_linepos = strlen(key_lines[edit_line]);
421 if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
423 con_backscroll += ((int) vid.conheight >> 5);
424 if (con_backscroll > con_totallines - (vid.conheight>>3) - 1)
425 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
429 if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
431 con_backscroll -= ((int) vid.conheight >> 5);
432 if (con_backscroll < 0)
437 if (key == K_HOME || key == K_KP_HOME)
439 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
443 if (key == K_END || key == K_KP_END)
453 if (key_linepos < MAX_INPUTLINE-1)
457 if (key_insert) // check insert mode
459 // can't do strcpy to move string to right
460 i = strlen(key_lines[edit_line]) - 1;
465 for (; i >= key_linepos; i--)
466 key_lines[edit_line][i + 1] = key_lines[edit_line][i];
469 // only null terminate if at the end
470 i = key_lines[edit_line][key_linepos];
471 key_lines[edit_line][key_linepos] = ascii;
475 key_lines[edit_line][key_linepos] = 0;
479 //============================================================================
482 char chat_buffer[MAX_INPUTLINE];
483 unsigned int chat_bufferlen = 0;
486 Key_Message (int key, char ascii)
491 Cmd_ForwardStringToServer(va("%s %s", chat_team ? "say_team" : "say ", chat_buffer));
499 if (key == K_ESCAPE) {
506 if (key == K_BACKSPACE) {
507 if (chat_bufferlen) {
509 chat_buffer[chat_bufferlen] = 0;
514 if (chat_bufferlen == sizeof (chat_buffer) - 1)
518 return; // non printable
520 chat_buffer[chat_bufferlen++] = ascii;
521 chat_buffer[chat_bufferlen] = 0;
524 //============================================================================
529 Returns a key number to be used to index keybindings[] by looking at
530 the given string. Single ascii characters return themselves, while
531 the K_* names are matched up.
535 Key_StringToKeynum (const char *str)
542 return tolower(str[0]);
544 for (kn = keynames; kn->name; kn++) {
545 if (!strcasecmp (str, kn->name))
553 Returns a string (either a single ascii char, or a K_* name) for the
555 FIXME: handle quote special (general escape sequence?)
559 Key_KeynumToString (int keynum)
562 static char tinystr[2];
565 return "<KEY NOT FOUND>";
566 if (keynum > 32 && keynum < 127) { // printable ascii
572 for (kn = keynames; kn->name; kn++)
573 if (keynum == kn->keynum)
576 return "<UNKNOWN KEYNUM>";
581 Key_SetBinding (int keynum, int bindmap, const char *binding)
590 if (keybindings[bindmap][keynum]) {
591 Z_Free (keybindings[bindmap][keynum]);
592 keybindings[bindmap][keynum] = NULL;
594 // allocate memory for new binding
595 l = strlen (binding);
596 new = Z_Malloc (l + 1);
597 strcpy (new, binding);
599 keybindings[bindmap][keynum] = new;
603 Key_In_Unbind_f (void)
607 if (Cmd_Argc () != 3) {
608 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
612 m = strtol(Cmd_Argv (1), NULL, 0);
613 if ((m < 0) || (m >= 8)) {
614 Con_Printf("%d isn't a valid bindmap\n", m);
618 b = Key_StringToKeynum (Cmd_Argv (2));
620 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
624 Key_SetBinding (b, m, "");
635 if (c != 3 && c != 4) {
636 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
640 m = strtol(Cmd_Argv (1), NULL, 0);
641 if ((m < 0) || (m >= 8)) {
642 Con_Printf("%d isn't a valid bindmap\n", m);
646 b = Key_StringToKeynum (Cmd_Argv (2));
648 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
653 if (keybindings[m][b])
654 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
656 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
659 // copy the rest of the command line
660 cmd[0] = 0; // start out with a null string
661 for (i = 3; i < c; i++) {
662 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
664 strlcat (cmd, " ", sizeof (cmd));
667 Key_SetBinding (b, m, cmd);
671 Key_In_Bindmap_f (void)
678 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
682 m1 = strtol(Cmd_Argv (1), NULL, 0);
683 if ((m1 < 0) || (m1 >= 8)) {
684 Con_Printf("%d isn't a valid bindmap\n", m1);
688 m2 = strtol(Cmd_Argv (2), NULL, 0);
689 if ((m2 < 0) || (m2 >= 8)) {
690 Con_Printf("%d isn't a valid bindmap\n", m2);
703 if (Cmd_Argc () != 2) {
704 Con_Print("unbind <key> : remove commands from a key\n");
708 b = Key_StringToKeynum (Cmd_Argv (1));
710 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
714 Key_SetBinding (b, 0, "");
718 Key_Unbindall_f (void)
722 for (j = 0; j < 8; j++)
723 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
724 if (keybindings[j][i])
725 Key_SetBinding (i, j, "");
737 if (c != 2 && c != 3) {
738 Con_Print("bind <key> [command] : attach a command to a key\n");
741 b = Key_StringToKeynum (Cmd_Argv (1));
743 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
748 if (keybindings[0][b])
749 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
751 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
754 // copy the rest of the command line
755 cmd[0] = 0; // start out with a null string
756 for (i = 2; i < c; i++) {
757 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
759 strlcat (cmd, " ", sizeof (cmd));
762 Key_SetBinding (b, 0, cmd);
767 Writes lines containing "bind key value"
771 Key_WriteBindings (qfile_t *f)
775 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
776 if (keybindings[0][i])
777 FS_Printf(f, "bind \"%s\" \"%s\"\n",
778 Key_KeynumToString (i), keybindings[0][i]);
779 for (j = 1; j < 8; j++)
780 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
781 if (keybindings[j][i])
782 FS_Printf(f, "in_bind %d \"%s\" \"%s\"\n",
783 j, Key_KeynumToString (i), keybindings[j][i]);
792 for (i = 0; i < 32; i++) {
793 key_lines[i][0] = ']';
799 // init ascii characters in console mode
801 for (i = 32; i < 128; i++)
802 consolekeys[i] = true;
803 consolekeys[K_ENTER] = true; consolekeys[K_KP_ENTER] = true;
804 consolekeys[K_TAB] = true;
805 consolekeys[K_LEFTARROW] = true; consolekeys[K_KP_LEFTARROW] = true;
806 consolekeys[K_RIGHTARROW] = true; consolekeys[K_KP_RIGHTARROW] = true;
807 consolekeys[K_UPARROW] = true; consolekeys[K_KP_UPARROW] = true;
808 consolekeys[K_DOWNARROW] = true; consolekeys[K_KP_DOWNARROW] = true;
809 consolekeys[K_BACKSPACE] = true;
810 consolekeys[K_DEL] = true; consolekeys[K_KP_DEL] = true;
811 consolekeys[K_INS] = true; consolekeys[K_KP_INS] = true;
812 consolekeys[K_HOME] = true; consolekeys[K_KP_HOME] = true;
813 consolekeys[K_END] = true; consolekeys[K_KP_END] = true;
814 consolekeys[K_PGUP] = true; consolekeys[K_KP_PGUP] = true;
815 consolekeys[K_PGDN] = true; consolekeys[K_KP_PGDN] = true;
816 consolekeys[K_SHIFT] = true;
817 consolekeys[K_MWHEELUP] = true;
818 consolekeys[K_MWHEELDOWN] = true;
819 consolekeys[K_KP_PLUS] = true;
820 consolekeys[K_KP_MINUS] = true;
821 consolekeys[K_KP_DIVIDE] = true;
822 consolekeys[K_KP_MULTIPLY] = true;
823 consolekeys['`'] = false;
824 consolekeys['~'] = false;
826 menubound[K_ESCAPE] = true;
827 for (i = 0; i < 12; i++)
828 menubound[K_F1 + i] = true;
831 // register our functions
833 Cmd_AddCommand ("in_bind", Key_In_Bind_f);
834 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f);
835 Cmd_AddCommand ("in_bindmap", Key_In_Bindmap_f);
837 Cmd_AddCommand ("bind", Key_Bind_f);
838 Cmd_AddCommand ("unbind", Key_Unbind_f);
839 Cmd_AddCommand ("unbindall", Key_Unbindall_f);
845 Called by the system between frames for both key up and key down events
846 Should NOT be called during an interrupt!
850 Key_Event (int key, char ascii, qboolean down)
858 key_repeats[key] = 0;
862 if (key_count <= 0) {
863 return; // just catching keys for Con_NotifyBox
866 // update auto-repeat status
869 if (key_repeats[key] > 1) {
870 if ((key_consoleactive && !consolekeys[key]) ||
871 (!key_consoleactive && key_dest == key_game &&
872 (cls.state == ca_connected && cls.signon == SIGNONS)))
873 return; // ignore most autorepeats
881 // handle escape specially, so the user can never unbind it
883 if (key == K_ESCAPE) {
888 Key_Message (key, ascii);
891 MR_Keydown (key, ascii);
897 if(UI_Callback_IsSlotUsed(key_dest - 3))
898 UI_Callback_KeyDown (key, ascii);
900 Sys_Error ("Bad key_dest");
907 if (!(kb = keybindings[key_bmap][key]))
908 kb = keybindings[key_bmap2][key];
909 if (kb && !strncmp(kb, "toggleconsole", strlen("toggleconsole")))
917 if (key_consoleactive && consolekeys[key] && down)
918 Key_Console (key, ascii);
922 // key up events only generate commands if the game key binding is a button
923 // command (leading + sign). These will occur even in console mode, to
924 // keep the character from continuing an action started before a console
925 // switch. Button commands include the kenum as a parameter, so multiple
926 // downs can be matched with ups
929 if (!(kb = keybindings[key_bmap][key]))
930 kb = keybindings[key_bmap2][key];
932 if (kb && kb[0] == '+') {
933 dpsnprintf (cmd, sizeof(cmd), "-%s %i\n", kb + 1, key);
940 // during demo playback, most keys bring up the main menu
942 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game) {
948 // if not a consolekey, send to the interpreter no matter what mode is
950 if ((key_dest == key_menu && menubound[key])
951 || (key_consoleactive && !consolekeys[key])
952 || (key_dest == key_game &&
953 ((cls.state == ca_connected) || !consolekeys[key]))) {
954 if (!(kb = keybindings[key_bmap][key]))
955 kb = keybindings[key_bmap2][key];
957 if (kb[0] == '+') { // button commands add keynum as a parm
958 dpsnprintf (cmd, sizeof(cmd), "%s %i\n", kb, key);
969 return; // other systems only care about key
974 Key_Message (key, ascii);
977 MR_Keydown (key, ascii);
980 Key_Console (key, ascii);
983 if(UI_Callback_IsSlotUsed(key_dest - 3))
984 UI_Callback_KeyDown (key, ascii);
986 Sys_Error ("Bad key_dest");
997 Key_ClearStates (void)
1001 for (i = 0; i < (int)(sizeof(keydown)/sizeof(keydown[0])); i++)