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
26 cvar_t con_closeontoggleconsole = {CVAR_SAVE, "con_closeontoggleconsole","1", "allows toggleconsole binds to close the console as well"};
29 key up events are sent even if in console mode
32 char key_line[MAX_INPUTLINE];
34 qboolean key_insert = true; // insert key toggle (for editing)
36 int key_consoleactive;
37 char *keybindings[MAX_BINDMAPS][MAX_KEYS];
39 char history_savedline[MAX_INPUTLINE];
41 #define HIST_TEXTSIZE 262144
42 #define HIST_MAXLINES 4096
44 extern cvar_t con_textsize;
47 static void Key_History_Init()
50 ConBuffer_Init(&history, HIST_TEXTSIZE, HIST_MAXLINES, zonemempool);
52 historyfile = FS_OpenRealFile("darkplaces_history.txt", "rb", false); // rb to handle unix line endings on windows too
55 char buf[MAX_INPUTLINE];
62 c = FS_Getc(historyfile);
63 if(c < 0 || c == 0 || c == '\r' || c == '\n')
68 ConBuffer_AddLine(&history, buf, bufpos, 0);
76 if(bufpos < MAX_INPUTLINE - 1)
81 FS_Close(historyfile);
87 static void Key_History_Shutdown()
89 // TODO write history to a file
91 qfile_t *historyfile = FS_OpenRealFile("darkplaces_history.txt", "w", false);
95 for(i = 0; i < CONBUFFER_LINES_COUNT(&history); ++i)
96 FS_Printf(historyfile, "%s\n", ConBuffer_GetLine(&history, i));
97 FS_Close(historyfile);
100 ConBuffer_Shutdown(&history);
103 static void Key_History_Push()
105 if(key_line[1]) // empty?
106 if(strcmp(key_line, "]quit")) // putting these into the history just sucks
107 if(strncmp(key_line, "]quit ", 6)) // putting these into the history just sucks
108 ConBuffer_AddLine(&history, key_line + 1, strlen(key_line) - 1, 0);
109 Con_Printf("%s\n", key_line); // don't mark empty lines as history
113 static void Key_History_Up()
115 if(history_line == -1) // editing the "new" line
116 strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
118 if(history_line == -1)
120 history_line = CONBUFFER_LINES_COUNT(&history) - 1;
121 if(history_line != -1)
123 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
124 key_linepos = strlen(key_line);
127 else if(history_line > 0)
129 --history_line; // this also does -1 -> 0, so it is good
130 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
131 key_linepos = strlen(key_line);
135 static void Key_History_Down()
137 if(history_line == -1) // editing the "new" line
140 if(history_line < CONBUFFER_LINES_COUNT(&history) - 1)
143 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
148 strlcpy(key_line + 1, history_savedline, sizeof(key_line) - 1);
151 key_linepos = strlen(key_line);
154 static int key_bmap, key_bmap2;
155 static unsigned char keydown[MAX_KEYS]; // 0 = up, 1 = down, 2 = repeating
157 typedef struct keyname_s
164 static const keyname_t keynames[] = {
167 {"ESCAPE", K_ESCAPE},
170 // spacer so it lines up with keys.h
172 {"BACKSPACE", K_BACKSPACE},
173 {"UPARROW", K_UPARROW},
174 {"DOWNARROW", K_DOWNARROW},
175 {"LEFTARROW", K_LEFTARROW},
176 {"RIGHTARROW", K_RIGHTARROW},
204 {"NUMLOCK", K_NUMLOCK},
205 {"CAPSLOCK", K_CAPSLOCK},
206 {"SCROLLOCK", K_SCROLLOCK},
208 {"KP_INS", K_KP_INS },
210 {"KP_END", K_KP_END },
212 {"KP_DOWNARROW", K_KP_DOWNARROW },
214 {"KP_PGDN", K_KP_PGDN },
216 {"KP_LEFTARROW", K_KP_LEFTARROW },
219 {"KP_RIGHTARROW", K_KP_RIGHTARROW },
221 {"KP_HOME", K_KP_HOME },
223 {"KP_UPARROW", K_KP_UPARROW },
225 {"KP_PGUP", K_KP_PGUP },
227 {"KP_DEL", K_KP_DEL },
228 {"KP_PERIOD", K_KP_PERIOD},
229 {"KP_SLASH", K_KP_SLASH },
230 {"KP_DIVIDE", K_KP_DIVIDE},
231 {"KP_MULTIPLY", K_KP_MULTIPLY},
232 {"KP_MINUS", K_KP_MINUS},
233 {"KP_PLUS", K_KP_PLUS},
234 {"KP_ENTER", K_KP_ENTER},
235 {"KP_EQUALS", K_KP_EQUALS},
239 {"MOUSE1", K_MOUSE1},
241 {"MOUSE2", K_MOUSE2},
242 {"MOUSE3", K_MOUSE3},
243 {"MWHEELUP", K_MWHEELUP},
244 {"MWHEELDOWN", K_MWHEELDOWN},
245 {"MOUSE4", K_MOUSE4},
246 {"MOUSE5", K_MOUSE5},
247 {"MOUSE6", K_MOUSE6},
248 {"MOUSE7", K_MOUSE7},
249 {"MOUSE8", K_MOUSE8},
250 {"MOUSE9", K_MOUSE9},
251 {"MOUSE10", K_MOUSE10},
252 {"MOUSE11", K_MOUSE11},
253 {"MOUSE12", K_MOUSE12},
254 {"MOUSE13", K_MOUSE13},
255 {"MOUSE14", K_MOUSE14},
256 {"MOUSE15", K_MOUSE15},
257 {"MOUSE16", K_MOUSE16},
317 {"SEMICOLON", ';'}, // because a raw semicolon separates commands
321 {"APOSTROPHE", '\''},
322 {"BACKSLASH", '\\'}, // because a raw backslash is used for special characters
328 ==============================================================================
330 LINE TYPING INTO THE CONSOLE
332 ==============================================================================
336 Key_ClearEditLine (int edit_line)
338 memset (key_line, '\0', sizeof(key_line));
345 Interactive line editing and console scrollback
349 Key_Console (int key, int ascii)
351 // LordHavoc: copied most of this from Q2 to improve keyboard handling
378 case K_KP_RIGHTARROW:
398 if ((key == 'v' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
401 if ((cbd = Sys_GetClipboardData()) != 0)
408 if (*p == '\r' && *(p+1) == '\n')
413 else if (*p == '\n' || *p == '\r' || *p == '\b')
418 strtok(cbd, "\n\r\b");
420 i = (int)strlen(cbd);
421 if (i + key_linepos >= MAX_INPUTLINE)
422 i= MAX_INPUTLINE - key_linepos - 1;
425 // terencehill: insert the clipboard text between the characters of the line
426 char *temp = (char *) Z_Malloc(MAX_INPUTLINE);
429 if ( key_linepos < (int)strlen(key_line) )
430 strlcpy(temp, key_line + key_linepos, (int)strlen(key_line) - key_linepos +1);
431 key_line[key_linepos] = 0;
432 strlcat(key_line, cbd, sizeof(key_line));
434 strlcat(key_line, temp, sizeof(key_line));
443 if (key == 'l' && keydown[K_CTRL])
445 Cbuf_AddText ("clear\n");
449 if (key == 'u' && keydown[K_CTRL]) // like vi/readline ^u: delete currently edited line
458 if (key == 'q' && keydown[K_CTRL]) // like zsh ^q: push line to history, don't execute, and clear
468 if (key == K_ENTER || key == K_KP_ENTER)
470 Cbuf_AddText (key_line+1); // skip the ]
474 key_line[1] = 0; // EvilTypeGuy: null terminate
476 // force an update, because the command may take some time
477 if (cls.state == ca_disconnected)
484 if(keydown[K_CTRL]) // append to the cvar its value
486 int cvar_len, cvar_str_len, chars_to_move;
488 char cvar[MAX_INPUTLINE];
489 const char *cvar_str;
491 // go to the start of the variable
494 k = key_line[key_linepos];
495 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
500 // save the variable name in cvar
501 for(cvar_len=0; (k = key_line[key_linepos + cvar_len]) != 0; cvar_len++)
503 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
511 // go to the end of the cvar
512 key_linepos += cvar_len;
514 // save the content of the variable in cvar_str
515 cvar_str = Cvar_VariableString(cvar);
516 cvar_str_len = strlen(cvar_str);
520 // insert space and cvar_str in key_line
521 chars_to_move = strlen(&key_line[key_linepos]);
522 if (key_linepos + 1 + cvar_str_len + chars_to_move < MAX_INPUTLINE)
525 memmove(&key_line[key_linepos + 1 + cvar_str_len], &key_line[key_linepos], chars_to_move);
526 key_line[key_linepos++] = ' ';
527 memcpy(&key_line[key_linepos], cvar_str, cvar_str_len);
528 key_linepos += cvar_str_len;
529 key_line[key_linepos + chars_to_move] = 0;
532 Con_Printf("Couldn't append cvar value, edit line too long.\n");
535 // Enhanced command completion
536 // by EvilTypeGuy eviltypeguy@qeradiant.com
537 // Thanks to Fett, Taniwha
538 Con_CompleteCommandLine();
542 // Advanced Console Editing by Radix radix@planetquake.com
543 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
545 // Enhanced by terencehill
547 // move cursor to the previous character
548 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
552 if(keydown[K_CTRL]) // move cursor to the previous word
558 if(pos) // skip all "; ' after the word
562 if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
570 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
573 key_linepos = pos + 1;
575 else if(keydown[K_SHIFT]) // move cursor to the previous character ignoring colors
580 if(pos-1 > 0 && key_line[pos-1] == STRING_COLOR_TAG && isdigit(key_line[pos]))
582 else if(pos-4 > 0 && key_line[pos-4] == STRING_COLOR_TAG && key_line[pos-3] == STRING_COLOR_RGB_TAG_CHAR
583 && isxdigit(key_line[pos-2]) && isxdigit(key_line[pos-1]) && isxdigit(key_line[pos]))
587 if(pos-1 > 0 && key_line[pos-1] == STRING_COLOR_TAG && key_line[pos] == STRING_COLOR_TAG) // consider ^^ as a character
592 key_linepos = pos + 1;
599 // delete char before cursor
600 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
604 strlcpy(key_line + key_linepos - 1, key_line + key_linepos, sizeof(key_line) + 1 - key_linepos);
610 // delete char on cursor
611 if (key == K_DEL || key == K_KP_DEL)
614 linelen = strlen(key_line);
615 if (key_linepos < (int)linelen)
616 memmove(key_line + key_linepos, key_line + key_linepos + 1, linelen - key_linepos);
621 // move cursor to the next character
622 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
624 if (key_linepos >= (int)strlen(key_line))
626 if(keydown[K_CTRL]) // move cursor to the next word
630 len = (int)strlen(key_line);
636 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
640 if (pos < len) // skip all "; ' after the word
644 if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
649 else if(keydown[K_SHIFT]) // move cursor to the next character ignoring colors
652 len = (int)strlen(key_line);
655 // go beyond all initial consecutive color tags, if any
657 while (key_line[pos] == STRING_COLOR_TAG)
659 if(isdigit(key_line[pos+1]))
661 else if(key_line[pos+1] == STRING_COLOR_RGB_TAG_CHAR && isxdigit(key_line[pos+2]) && isxdigit(key_line[pos+3]) && isxdigit(key_line[pos+4]))
668 if (key_line[pos] == STRING_COLOR_TAG && key_line[pos+1] == STRING_COLOR_TAG) // consider ^^ as a character
672 // now go beyond all next consecutive color tags, if any
674 while (key_line[pos] == STRING_COLOR_TAG)
676 if(isdigit(key_line[pos+1]))
678 else if(key_line[pos+1] == STRING_COLOR_RGB_TAG_CHAR && isxdigit(key_line[pos+2]) && isxdigit(key_line[pos+3]) && isxdigit(key_line[pos+4]))
690 if (key == K_INS || key == K_KP_INS) // toggle insert mode
696 // End Advanced Console Editing
698 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
704 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
709 // ~1.0795 = 82/76 using con_textsize 64 76 is height of the char, 6 is the distance between 2 lines
710 if (key == K_PGUP || key == K_KP_PGUP)
714 con_backscroll += (int)floor((vid_conheight.integer >> 2) / con_textsize.integer)-1;
717 con_backscroll += (int)floor((vid_conheight.integer >> 1) / con_textsize.integer)-3;
721 if (key == K_PGDN || key == K_KP_PGDN)
725 con_backscroll -= (int)floor((vid_conheight.integer >> 2) / con_textsize.integer)-1;
728 con_backscroll -= (int)floor((vid_conheight.integer >> 1) / con_textsize.integer)-3;
732 if (key == K_MWHEELUP)
736 else if(keydown[K_SHIFT])
737 con_backscroll += (int)floor((vid_conheight.integer >> 2) / con_textsize.integer)-1;
743 if (key == K_MWHEELDOWN)
747 else if(keydown[K_SHIFT])
748 con_backscroll -= (int)floor((vid_conheight.integer >> 2) / con_textsize.integer)-1;
757 if (key == '+' || key == K_KP_PLUS)
759 if (con_textsize.integer < 128)
760 Cvar_SetValueQuick(&con_textsize, con_textsize.integer + 1);
764 if (key == '-' || key == K_KP_MINUS)
766 if (con_textsize.integer > 1)
767 Cvar_SetValueQuick(&con_textsize, con_textsize.integer - 1);
771 if (key == '0' || key == K_KP_INS)
773 Cvar_SetValueQuick(&con_textsize, atoi(Cvar_VariableDefString("con_textsize")));
778 if (key == K_HOME || key == K_KP_HOME)
781 con_backscroll = INT_MAX;
787 if (key == K_END || key == K_KP_END)
792 key_linepos = (int)strlen(key_line);
800 if (key_linepos < MAX_INPUTLINE-1)
803 len = (int)strlen(&key_line[key_linepos]);
804 // check insert mode, or always insert if at end of line
805 if (key_insert || len == 0)
807 // can't use strcpy to move string to right
809 memmove(&key_line[key_linepos + 1], &key_line[key_linepos], len);
811 key_line[key_linepos] = ascii;
816 //============================================================================
819 char chat_buffer[MAX_INPUTLINE];
820 unsigned int chat_bufferlen = 0;
822 extern int Nicks_CompleteChatLine(char *buffer, size_t size, unsigned int pos);
825 Key_Message (int key, int ascii)
828 if (key == K_ENTER || ascii == 10 || ascii == 13)
831 Cmd_ExecuteString(chat_buffer, src_command); // not Cbuf_AddText to allow semiclons in args; however, this allows no variables then. Use aliases!
833 Cmd_ForwardStringToServer(va("%s %s", chat_mode ? "say_team" : "say ", chat_buffer));
841 // TODO add support for arrow keys and simple editing
843 if (key == K_ESCAPE) {
850 if (key == K_BACKSPACE) {
851 if (chat_bufferlen) {
853 chat_buffer[chat_bufferlen] = 0;
859 chat_bufferlen = Nicks_CompleteChatLine(chat_buffer, sizeof(chat_buffer), chat_bufferlen);
863 if (chat_bufferlen == sizeof (chat_buffer) - 1)
867 return; // non printable
869 chat_buffer[chat_bufferlen++] = ascii;
870 chat_buffer[chat_bufferlen] = 0;
873 //============================================================================
878 Returns a key number to be used to index keybindings[] by looking at
879 the given string. Single ascii characters return themselves, while
880 the K_* names are matched up.
884 Key_StringToKeynum (const char *str)
891 return tolower(str[0]);
893 for (kn = keynames; kn->name; kn++) {
894 if (!strcasecmp (str, kn->name))
902 Returns a string (either a single ascii char, or a K_* name) for the
904 FIXME: handle quote special (general escape sequence?)
908 Key_KeynumToString (int keynum)
911 static char tinystr[2];
913 // -1 is an invalid code
915 return "<KEY NOT FOUND>";
917 // search overrides first, because some characters are special
918 for (kn = keynames; kn->name; kn++)
919 if (keynum == kn->keynum)
922 // if it is printable, output it as a single character
923 if (keynum > 32 && keynum < 256)
930 // if it is not overridden and not printable, we don't know what to do with it
931 return "<UNKNOWN KEYNUM>";
936 Key_SetBinding (int keynum, int bindmap, const char *binding)
941 if (keynum == -1 || keynum >= MAX_KEYS)
945 if (keybindings[bindmap][keynum]) {
946 Z_Free (keybindings[bindmap][keynum]);
947 keybindings[bindmap][keynum] = NULL;
949 if(!binding[0]) // make "" binds be removed --blub
951 // allocate memory for new binding
952 l = strlen (binding);
953 newbinding = (char *)Z_Malloc (l + 1);
954 memcpy (newbinding, binding, l + 1);
956 keybindings[bindmap][keynum] = newbinding;
960 Key_In_Unbind_f (void)
963 char *errchar = NULL;
965 if (Cmd_Argc () != 3) {
966 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
970 m = strtol(Cmd_Argv (1), &errchar, 0);
971 if ((m < 0) || (m >= 8) || (errchar && *errchar)) {
972 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
976 b = Key_StringToKeynum (Cmd_Argv (2));
978 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
982 Key_SetBinding (b, m, "");
989 char cmd[MAX_INPUTLINE];
990 char *errchar = NULL;
994 if (c != 3 && c != 4) {
995 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
999 m = strtol(Cmd_Argv (1), &errchar, 0);
1000 if ((m < 0) || (m >= 8) || (errchar && *errchar)) {
1001 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1005 b = Key_StringToKeynum (Cmd_Argv (2));
1006 if (b == -1 || b >= MAX_KEYS) {
1007 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
1012 if (keybindings[m][b])
1013 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
1015 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
1018 // copy the rest of the command line
1019 cmd[0] = 0; // start out with a null string
1020 for (i = 3; i < c; i++) {
1021 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
1023 strlcat (cmd, " ", sizeof (cmd));
1026 Key_SetBinding (b, m, cmd);
1030 Key_In_Bindmap_f (void)
1033 char *errchar = NULL;
1038 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
1042 m1 = strtol(Cmd_Argv (1), &errchar, 0);
1043 if ((m1 < 0) || (m1 >= 8) || (errchar && *errchar)) {
1044 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1048 m2 = strtol(Cmd_Argv (2), &errchar, 0);
1049 if ((m2 < 0) || (m2 >= 8) || (errchar && *errchar)) {
1050 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(2));
1063 if (Cmd_Argc () != 2) {
1064 Con_Print("unbind <key> : remove commands from a key\n");
1068 b = Key_StringToKeynum (Cmd_Argv (1));
1070 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
1074 Key_SetBinding (b, 0, "");
1078 Key_Unbindall_f (void)
1082 for (j = 0; j < 8; j++)
1083 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1084 if (keybindings[j][i])
1085 Key_SetBinding (i, j, "");
1089 Key_PrintBindList(int j)
1091 char bindbuf[MAX_INPUTLINE];
1095 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1097 p = keybindings[j][i];
1100 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\");
1102 Con_Printf("^2%s ^7= \"%s\"\n", Key_KeynumToString (i), bindbuf);
1104 Con_Printf("^3bindmap %d: ^2%s ^7= \"%s\"\n", j, Key_KeynumToString (i), bindbuf);
1110 Key_In_BindList_f (void)
1113 char *errchar = NULL;
1117 m = strtol(Cmd_Argv(1), &errchar, 0);
1118 if ((m < 0) || (m >= 8) || (errchar && *errchar)) {
1119 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1122 Key_PrintBindList(m);
1126 for (m = 0; m < MAX_BINDMAPS; m++)
1127 Key_PrintBindList(m);
1132 Key_BindList_f (void)
1134 Key_PrintBindList(0);
1141 char cmd[MAX_INPUTLINE];
1145 if (c != 2 && c != 3) {
1146 Con_Print("bind <key> [command] : attach a command to a key\n");
1149 b = Key_StringToKeynum (Cmd_Argv (1));
1150 if (b == -1 || b >= MAX_KEYS) {
1151 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
1156 if (keybindings[0][b])
1157 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
1159 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
1162 // copy the rest of the command line
1163 cmd[0] = 0; // start out with a null string
1164 for (i = 2; i < c; i++) {
1165 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
1167 strlcat (cmd, " ", sizeof (cmd));
1170 Key_SetBinding (b, 0, cmd);
1175 Writes lines containing "bind key value"
1179 Key_WriteBindings (qfile_t *f)
1182 char bindbuf[MAX_INPUTLINE];
1185 for (j = 0; j < MAX_BINDMAPS; j++)
1187 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1189 p = keybindings[j][i];
1192 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\");
1194 FS_Printf(f, "bind %s \"%s\"\n", Key_KeynumToString (i), bindbuf);
1196 FS_Printf(f, "in_bind %d %s \"%s\"\n", j, Key_KeynumToString (i), bindbuf);
1212 // register our functions
1214 Cmd_AddCommand ("in_bind", Key_In_Bind_f, "binds a command to the specified key in the selected bindmap");
1215 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f, "removes command on the specified key in the selected bindmap");
1216 Cmd_AddCommand ("in_bindlist", Key_In_BindList_f, "bindlist: displays bound keys for all bindmaps, or the given bindmap");
1217 Cmd_AddCommand ("in_bindmap", Key_In_Bindmap_f, "selects active foreground and background (used only if a key is not bound in the foreground) bindmaps for typing");
1219 Cmd_AddCommand ("bind", Key_Bind_f, "binds a command to the specified key in bindmap 0");
1220 Cmd_AddCommand ("unbind", Key_Unbind_f, "removes a command on the specified key in bindmap 0");
1221 Cmd_AddCommand ("bindlist", Key_BindList_f, "bindlist: displays bound keys for bindmap 0 bindmaps");
1222 Cmd_AddCommand ("unbindall", Key_Unbindall_f, "removes all commands from all keys in all bindmaps (leaving only shift-escape and escape)");
1224 Cvar_RegisterVariable (&con_closeontoggleconsole);
1230 Key_History_Shutdown();
1233 const char *Key_GetBind (int key)
1236 if (key < 0 || key >= MAX_KEYS)
1238 bind = keybindings[key_bmap][key];
1240 bind = keybindings[key_bmap2][key];
1244 qboolean CL_VM_InputEvent (qboolean down, int key, int ascii);
1248 Called by the system between frames for both key up and key down events
1249 Should NOT be called during an interrupt!
1252 static char tbl_keyascii[MAX_KEYS];
1253 static keydest_t tbl_keydest[MAX_KEYS];
1256 Key_Event (int key, int ascii, qboolean down)
1260 keydest_t keydest = key_dest;
1262 if (key < 0 || key >= MAX_KEYS)
1266 bind = keybindings[key_bmap][key];
1268 bind = keybindings[key_bmap2][key];
1270 if (developer.integer >= 1000)
1271 Con_Printf("Key_Event(%i, '%c', %s) keydown %i bind \"%s\"\n", key, ascii, down ? "down" : "up", keydown[key], bind ? bind : "");
1273 if(key_consoleactive)
1274 keydest = key_console;
1278 // increment key repeat count each time a down is received so that things
1279 // which want to ignore key repeat can ignore it
1280 keydown[key] = min(keydown[key] + 1, 2);
1281 if(keydown[key] == 1) {
1282 tbl_keyascii[key] = ascii;
1283 tbl_keydest[key] = keydest;
1285 ascii = tbl_keyascii[key];
1286 keydest = tbl_keydest[key];
1291 // clear repeat count now that the key is released
1293 keydest = tbl_keydest[key];
1294 ascii = tbl_keyascii[key];
1297 if(keydest == key_void)
1300 // key_consoleactive is a flag not a key_dest because the console is a
1301 // high priority overlay ontop of the normal screen (designed as a safety
1302 // feature so that developers and users can rescue themselves from a bad
1305 // this also means that toggling the console on/off does not lose the old
1308 // specially handle escape (togglemenu) and shift-escape (toggleconsole)
1309 // engine bindings, these are not handled as normal binds so that the user
1310 // can recover from a completely empty bindmap
1311 if (key == K_ESCAPE)
1313 // ignore key repeats on escape
1314 if (keydown[key] > 1)
1317 // escape does these things:
1318 // key_consoleactive - close console
1319 // key_message - abort messagemode
1320 // key_menu - go to parent menu (or key_game)
1321 // key_game - open menu
1323 // in all modes shift-escape toggles console
1324 if (keydown[K_SHIFT])
1328 Con_ToggleConsole_f ();
1329 tbl_keydest[key] = key_void; // esc release should go nowhere (especially not to key_menu or key_game)
1339 if(key_consoleactive & KEY_CONSOLEACTIVE_FORCED)
1341 key_consoleactive &= ~KEY_CONSOLEACTIVE_USER;
1345 Con_ToggleConsole_f();
1351 Key_Message (key, ascii); // that'll close the message input
1355 case key_menu_grabbed:
1356 MR_KeyEvent (key, ascii, down);
1360 // csqc has priority over toggle menu if it wants to (e.g. handling escape for UI stuff in-game.. :sick:)
1361 q = CL_VM_InputEvent(down, key, ascii);
1367 Con_Printf ("Key_Event: Bad key_dest\n");
1372 // send function keydowns to interpreter no matter what mode is (unless the menu has specifically grabbed the keyboard, for rebinding keys)
1373 if (keydest != key_menu_grabbed)
1374 if (key >= K_F1 && key <= K_F12)
1378 if(keydown[key] == 1 && down)
1380 // button commands add keynum as a parm
1382 Cbuf_AddText (va("%s %i\n", bind, key));
1385 Cbuf_AddText (bind);
1386 Cbuf_AddText ("\n");
1388 } else if(bind[0] == '+' && !down && keydown[key] == 0)
1389 Cbuf_AddText(va("-%s %i\n", bind + 1, key));
1394 // send input to console if it wants it
1395 if (keydest == key_console)
1399 // con_closeontoggleconsole enables toggleconsole keys to close the
1400 // console, as long as they are not the color prefix character
1401 // (special exemption for german keyboard layouts)
1402 if (con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && (key_consoleactive & KEY_CONSOLEACTIVE_USER) && ascii != STRING_COLOR_TAG)
1404 Con_ToggleConsole_f ();
1407 Key_Console (key, ascii);
1411 // handle toggleconsole in menu too
1412 if (keydest == key_menu)
1414 if (down && con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && ascii != STRING_COLOR_TAG)
1416 Con_ToggleConsole_f ();
1417 tbl_keydest[key] = key_void; // key release should go nowhere (especially not to key_menu or key_game)
1422 // ignore binds while a video is played, let the video system handle the key event
1423 if (cl_videoplaying)
1425 CL_Video_KeyEvent (key, ascii, keydown[key] != 0);
1429 // anything else is a key press into the game, chat line, or menu
1434 Key_Message (key, ascii);
1437 case key_menu_grabbed:
1438 MR_KeyEvent (key, ascii, down);
1441 q = CL_VM_InputEvent(down, key, ascii);
1442 // ignore key repeats on binds and only send the bind if the event hasnt been already processed by csqc
1445 if(keydown[key] == 1 && down)
1447 // button commands add keynum as a parm
1449 Cbuf_AddText (va("%s %i\n", bind, key));
1452 Cbuf_AddText (bind);
1453 Cbuf_AddText ("\n");
1455 } else if(bind[0] == '+' && !down && keydown[key] == 0)
1456 Cbuf_AddText(va("-%s %i\n", bind + 1, key));
1460 Con_Printf ("Key_Event: Bad key_dest\n");
1470 Key_ClearStates (void)
1472 memset(keydown, 0, sizeof(keydown));