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 cvar_t con_closeontoggleconsole = {CVAR_SAVE, "con_closeontoggleconsole","1", "allows toggleconsole binds to close the console as well"};
30 key up events are sent even if in console mode
33 char key_line[MAX_INPUTLINE];
35 qboolean key_insert = true; // insert key toggle (for editing)
37 int key_consoleactive;
38 char *keybindings[MAX_BINDMAPS][MAX_KEYS];
40 char history_savedline[MAX_INPUTLINE];
42 #define HIST_TEXTSIZE 262144
43 #define HIST_MAXLINES 4096
45 extern cvar_t con_textsize;
48 static void Key_History_Init(void)
51 ConBuffer_Init(&history, HIST_TEXTSIZE, HIST_MAXLINES, zonemempool);
53 historyfile = FS_OpenRealFile("darkplaces_history.txt", "rb", false); // rb to handle unix line endings on windows too
56 char buf[MAX_INPUTLINE];
63 c = FS_Getc(historyfile);
64 if(c < 0 || c == 0 || c == '\r' || c == '\n')
69 ConBuffer_AddLine(&history, buf, bufpos, 0);
77 if(bufpos < MAX_INPUTLINE - 1)
82 FS_Close(historyfile);
88 static void Key_History_Shutdown(void)
90 // TODO write history to a file
92 qfile_t *historyfile = FS_OpenRealFile("darkplaces_history.txt", "w", false);
96 for(i = 0; i < CONBUFFER_LINES_COUNT(&history); ++i)
97 FS_Printf(historyfile, "%s\n", ConBuffer_GetLine(&history, i));
98 FS_Close(historyfile);
101 ConBuffer_Shutdown(&history);
104 static void Key_History_Push(void)
106 if(key_line[1]) // empty?
107 if(strcmp(key_line, "]quit")) // putting these into the history just sucks
108 if(strncmp(key_line, "]quit ", 6)) // putting these into the history just sucks
109 ConBuffer_AddLine(&history, key_line + 1, strlen(key_line) - 1, 0);
110 Con_Printf("%s\n", key_line); // don't mark empty lines as history
114 static void Key_History_Up(void)
116 if(history_line == -1) // editing the "new" line
117 strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
119 if(history_line == -1)
121 history_line = CONBUFFER_LINES_COUNT(&history) - 1;
122 if(history_line != -1)
124 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
125 key_linepos = strlen(key_line);
128 else if(history_line > 0)
130 --history_line; // this also does -1 -> 0, so it is good
131 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
132 key_linepos = strlen(key_line);
136 static void Key_History_Down(void)
138 if(history_line == -1) // editing the "new" line
141 if(history_line < CONBUFFER_LINES_COUNT(&history) - 1)
144 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
149 strlcpy(key_line + 1, history_savedline, sizeof(key_line) - 1);
152 key_linepos = strlen(key_line);
155 static int key_bmap, key_bmap2;
156 static unsigned char keydown[MAX_KEYS]; // 0 = up, 1 = down, 2 = repeating
158 typedef struct keyname_s
165 static const keyname_t keynames[] = {
168 {"ESCAPE", K_ESCAPE},
171 // spacer so it lines up with keys.h
173 {"BACKSPACE", K_BACKSPACE},
174 {"UPARROW", K_UPARROW},
175 {"DOWNARROW", K_DOWNARROW},
176 {"LEFTARROW", K_LEFTARROW},
177 {"RIGHTARROW", K_RIGHTARROW},
205 {"NUMLOCK", K_NUMLOCK},
206 {"CAPSLOCK", K_CAPSLOCK},
207 {"SCROLLOCK", K_SCROLLOCK},
209 {"KP_INS", K_KP_INS },
211 {"KP_END", K_KP_END },
213 {"KP_DOWNARROW", K_KP_DOWNARROW },
215 {"KP_PGDN", K_KP_PGDN },
217 {"KP_LEFTARROW", K_KP_LEFTARROW },
220 {"KP_RIGHTARROW", K_KP_RIGHTARROW },
222 {"KP_HOME", K_KP_HOME },
224 {"KP_UPARROW", K_KP_UPARROW },
226 {"KP_PGUP", K_KP_PGUP },
228 {"KP_DEL", K_KP_DEL },
229 {"KP_PERIOD", K_KP_PERIOD},
230 {"KP_SLASH", K_KP_SLASH },
231 {"KP_DIVIDE", K_KP_DIVIDE},
232 {"KP_MULTIPLY", K_KP_MULTIPLY},
233 {"KP_MINUS", K_KP_MINUS},
234 {"KP_PLUS", K_KP_PLUS},
235 {"KP_ENTER", K_KP_ENTER},
236 {"KP_EQUALS", K_KP_EQUALS},
240 {"MOUSE1", K_MOUSE1},
242 {"MOUSE2", K_MOUSE2},
243 {"MOUSE3", K_MOUSE3},
244 {"MWHEELUP", K_MWHEELUP},
245 {"MWHEELDOWN", K_MWHEELDOWN},
246 {"MOUSE4", K_MOUSE4},
247 {"MOUSE5", K_MOUSE5},
248 {"MOUSE6", K_MOUSE6},
249 {"MOUSE7", K_MOUSE7},
250 {"MOUSE8", K_MOUSE8},
251 {"MOUSE9", K_MOUSE9},
252 {"MOUSE10", K_MOUSE10},
253 {"MOUSE11", K_MOUSE11},
254 {"MOUSE12", K_MOUSE12},
255 {"MOUSE13", K_MOUSE13},
256 {"MOUSE14", K_MOUSE14},
257 {"MOUSE15", K_MOUSE15},
258 {"MOUSE16", K_MOUSE16},
318 {"SEMICOLON", ';'}, // because a raw semicolon separates commands
322 {"APOSTROPHE", '\''},
323 {"BACKSLASH", '\\'}, // because a raw backslash is used for special characters
329 ==============================================================================
331 LINE TYPING INTO THE CONSOLE
333 ==============================================================================
337 Key_ClearEditLine (int edit_line)
339 memset (key_line, '\0', sizeof(key_line));
346 Interactive line editing and console scrollback
350 Key_Console (int key, int unicode)
352 // LordHavoc: copied most of this from Q2 to improve keyboard handling
379 case K_KP_RIGHTARROW:
399 if ((key == 'v' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
402 if ((cbd = Sys_GetClipboardData()) != 0)
409 if (*p == '\r' && *(p+1) == '\n')
414 else if (*p == '\n' || *p == '\r' || *p == '\b')
419 strtok(cbd, "\n\r\b");
421 i = (int)strlen(cbd);
422 if (i + key_linepos >= MAX_INPUTLINE)
423 i= MAX_INPUTLINE - key_linepos - 1;
426 // terencehill: insert the clipboard text between the characters of the line
428 char *temp = (char *) Z_Malloc(MAX_INPUTLINE);
431 if ( key_linepos < (int)strlen(key_line) )
432 strlcpy(temp, key_line + key_linepos, (int)strlen(key_line) - key_linepos +1);
433 key_line[key_linepos] = 0;
434 strlcat(key_line, cbd, sizeof(key_line));
436 strlcat(key_line, temp, sizeof(key_line));
440 // blub: I'm changing this to use memmove() like the rest of the code does.
442 memmove(key_line + key_linepos + i, key_line + key_linepos, sizeof(key_line) - key_linepos - i);
443 memcpy(key_line + key_linepos, cbd, i);
451 if (key == 'l' && keydown[K_CTRL])
453 Cbuf_AddText ("clear\n");
457 if (key == 'u' && keydown[K_CTRL]) // like vi/readline ^u: delete currently edited line
466 if (key == 'q' && keydown[K_CTRL]) // like zsh ^q: push line to history, don't execute, and clear
476 if (key == K_ENTER || key == K_KP_ENTER)
478 Cbuf_AddText (key_line+1); // skip the ]
482 key_line[1] = 0; // EvilTypeGuy: null terminate
484 // force an update, because the command may take some time
485 if (cls.state == ca_disconnected)
492 if(keydown[K_CTRL]) // append to the cvar its value
494 int cvar_len, cvar_str_len, chars_to_move;
496 char cvar[MAX_INPUTLINE];
497 const char *cvar_str;
499 // go to the start of the variable
502 k = key_line[key_linepos];
503 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
508 // save the variable name in cvar
509 for(cvar_len=0; (k = key_line[key_linepos + cvar_len]) != 0; cvar_len++)
511 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
519 // go to the end of the cvar
520 key_linepos += cvar_len;
522 // save the content of the variable in cvar_str
523 cvar_str = Cvar_VariableString(cvar);
524 cvar_str_len = strlen(cvar_str);
528 // insert space and cvar_str in key_line
529 chars_to_move = strlen(&key_line[key_linepos]);
530 if (key_linepos + 1 + cvar_str_len + chars_to_move < MAX_INPUTLINE)
533 memmove(&key_line[key_linepos + 1 + cvar_str_len], &key_line[key_linepos], chars_to_move);
534 key_line[key_linepos++] = ' ';
535 memcpy(&key_line[key_linepos], cvar_str, cvar_str_len);
536 key_linepos += cvar_str_len;
537 key_line[key_linepos + chars_to_move] = 0;
540 Con_Printf("Couldn't append cvar value, edit line too long.\n");
543 // Enhanced command completion
544 // by EvilTypeGuy eviltypeguy@qeradiant.com
545 // Thanks to Fett, Taniwha
546 Con_CompleteCommandLine();
550 // Advanced Console Editing by Radix radix@planetquake.com
551 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
553 // Enhanced by terencehill
555 // move cursor to the previous character
556 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
560 if(keydown[K_CTRL]) // move cursor to the previous word
566 if(pos) // skip all "; ' after the word
570 if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
578 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
581 key_linepos = pos + 1;
583 else if(keydown[K_SHIFT]) // move cursor to the previous character ignoring colors
587 pos = u8_prevbyte(key_line, key_linepos);
589 if(pos-1 > 0 && key_line[pos-1] == STRING_COLOR_TAG && isdigit(key_line[pos]))
591 else if(pos-4 > 0 && key_line[pos-4] == STRING_COLOR_TAG && key_line[pos-3] == STRING_COLOR_RGB_TAG_CHAR
592 && isxdigit(key_line[pos-2]) && isxdigit(key_line[pos-1]) && isxdigit(key_line[pos]))
596 if(pos-1 > 0 && key_line[pos-1] == STRING_COLOR_TAG && key_line[pos] == STRING_COLOR_TAG) // consider ^^ as a character
601 // we need to move to the beginning of the character when in a wide character:
602 u8_charidx(key_line, pos + 1, &inchar);
603 key_linepos = pos + 1 - inchar;
607 key_linepos = u8_prevbyte(key_line, key_linepos);
612 // delete char before cursor
613 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
617 int newpos = u8_prevbyte(key_line, key_linepos);
618 strlcpy(key_line + newpos, key_line + key_linepos, sizeof(key_line) + 1 - key_linepos);
619 key_linepos = newpos;
624 // delete char on cursor
625 if (key == K_DEL || key == K_KP_DEL)
628 linelen = strlen(key_line);
629 if (key_linepos < (int)linelen)
630 memmove(key_line + key_linepos, key_line + key_linepos + u8_bytelen(key_line + key_linepos, 1), linelen - key_linepos);
635 // move cursor to the next character
636 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
638 if (key_linepos >= (int)strlen(key_line))
640 if(keydown[K_CTRL]) // move cursor to the next word
644 len = (int)strlen(key_line);
650 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
654 if (pos < len) // skip all "; ' after the word
658 if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
663 else if(keydown[K_SHIFT]) // move cursor to the next character ignoring colors
666 len = (int)strlen(key_line);
669 // go beyond all initial consecutive color tags, if any
671 while (key_line[pos] == STRING_COLOR_TAG)
673 if(isdigit(key_line[pos+1]))
675 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]))
682 if (key_line[pos] == STRING_COLOR_TAG && key_line[pos+1] == STRING_COLOR_TAG) // consider ^^ as a character
684 pos += u8_bytelen(key_line + pos, 1);
686 // now go beyond all next consecutive color tags, if any
688 while (key_line[pos] == STRING_COLOR_TAG)
690 if(isdigit(key_line[pos+1]))
692 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]))
700 key_linepos += u8_bytelen(key_line + key_linepos, 1);
704 if (key == K_INS || key == K_KP_INS) // toggle insert mode
710 // End Advanced Console Editing
712 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
718 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
723 // ~1.0795 = 82/76 using con_textsize 64 76 is height of the char, 6 is the distance between 2 lines
724 if (key == K_PGUP || key == K_KP_PGUP)
728 con_backscroll += (int)floor((vid_conheight.integer >> 2) / con_textsize.integer)-1;
731 con_backscroll += (int)floor((vid_conheight.integer >> 1) / con_textsize.integer)-3;
735 if (key == K_PGDN || key == K_KP_PGDN)
739 con_backscroll -= (int)floor((vid_conheight.integer >> 2) / con_textsize.integer)-1;
742 con_backscroll -= (int)floor((vid_conheight.integer >> 1) / con_textsize.integer)-3;
746 if (key == K_MWHEELUP)
750 else if(keydown[K_SHIFT])
751 con_backscroll += (int)floor((vid_conheight.integer >> 2) / con_textsize.integer)-1;
757 if (key == K_MWHEELDOWN)
761 else if(keydown[K_SHIFT])
762 con_backscroll -= (int)floor((vid_conheight.integer >> 2) / con_textsize.integer)-1;
771 if (key == '+' || key == K_KP_PLUS)
773 if (con_textsize.integer < 128)
774 Cvar_SetValueQuick(&con_textsize, con_textsize.integer + 1);
778 if (key == '-' || key == K_KP_MINUS)
780 if (con_textsize.integer > 1)
781 Cvar_SetValueQuick(&con_textsize, con_textsize.integer - 1);
785 if (key == '0' || key == K_KP_INS)
787 Cvar_SetValueQuick(&con_textsize, atoi(Cvar_VariableDefString("con_textsize")));
792 if (key == K_HOME || key == K_KP_HOME)
795 con_backscroll = INT_MAX;
801 if (key == K_END || key == K_KP_END)
806 key_linepos = (int)strlen(key_line);
814 if (key_linepos < MAX_INPUTLINE-1)
819 blen = u8_fromchar(unicode, buf, sizeof(buf));
822 len = (int)strlen(&key_line[key_linepos]);
823 // check insert mode, or always insert if at end of line
824 if (key_insert || len == 0)
826 // can't use strcpy to move string to right
828 //memmove(&key_line[key_linepos + u8_bytelen(key_line + key_linepos, 1)], &key_line[key_linepos], len);
829 memmove(&key_line[key_linepos + blen], &key_line[key_linepos], len);
831 memcpy(key_line + key_linepos, buf, blen);
833 //key_linepos += u8_fromchar(unicode, key_line + key_linepos, sizeof(key_line) - key_linepos - 1);
834 //key_line[key_linepos] = ascii;
839 //============================================================================
842 char chat_buffer[MAX_INPUTLINE];
843 unsigned int chat_bufferlen = 0;
845 extern int Nicks_CompleteChatLine(char *buffer, size_t size, unsigned int pos);
848 Key_Message (int key, int ascii)
850 if (key == K_ENTER || ascii == 10 || ascii == 13)
853 Cmd_ExecuteString(chat_buffer, src_command); // not Cbuf_AddText to allow semiclons in args; however, this allows no variables then. Use aliases!
855 Cmd_ForwardStringToServer(va("%s %s", chat_mode ? "say_team" : "say ", chat_buffer));
863 // TODO add support for arrow keys and simple editing
865 if (key == K_ESCAPE) {
872 if (key == K_BACKSPACE) {
873 if (chat_bufferlen) {
874 chat_bufferlen = u8_prevbyte(chat_buffer, chat_bufferlen);
875 chat_buffer[chat_bufferlen] = 0;
881 chat_bufferlen = Nicks_CompleteChatLine(chat_buffer, sizeof(chat_buffer), chat_bufferlen);
885 if (chat_bufferlen == sizeof (chat_buffer) - 1)
889 return; // non printable
891 chat_bufferlen += u8_fromchar(ascii, chat_buffer+chat_bufferlen, sizeof(chat_buffer) - chat_bufferlen - 1);
893 //chat_buffer[chat_bufferlen++] = ascii;
894 //chat_buffer[chat_bufferlen] = 0;
897 //============================================================================
902 Returns a key number to be used to index keybindings[] by looking at
903 the given string. Single ascii characters return themselves, while
904 the K_* names are matched up.
908 Key_StringToKeynum (const char *str)
915 return tolower(str[0]);
917 for (kn = keynames; kn->name; kn++) {
918 if (!strcasecmp (str, kn->name))
926 Returns a string (either a single ascii char, or a K_* name) for the
928 FIXME: handle quote special (general escape sequence?)
932 Key_KeynumToString (int keynum)
935 static char tinystr[2];
937 // -1 is an invalid code
939 return "<KEY NOT FOUND>";
941 // search overrides first, because some characters are special
942 for (kn = keynames; kn->name; kn++)
943 if (keynum == kn->keynum)
946 // if it is printable, output it as a single character
947 if (keynum > 32 && keynum < 256)
954 // if it is not overridden and not printable, we don't know what to do with it
955 return "<UNKNOWN KEYNUM>";
960 Key_SetBinding (int keynum, int bindmap, const char *binding)
965 if (keynum == -1 || keynum >= MAX_KEYS)
969 if (keybindings[bindmap][keynum]) {
970 Z_Free (keybindings[bindmap][keynum]);
971 keybindings[bindmap][keynum] = NULL;
973 if(!binding[0]) // make "" binds be removed --blub
975 // allocate memory for new binding
976 l = strlen (binding);
977 newbinding = (char *)Z_Malloc (l + 1);
978 memcpy (newbinding, binding, l + 1);
980 keybindings[bindmap][keynum] = newbinding;
984 Key_In_Unbind_f (void)
987 char *errchar = NULL;
989 if (Cmd_Argc () != 3) {
990 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
994 m = strtol(Cmd_Argv (1), &errchar, 0);
995 if ((m < 0) || (m >= 8) || (errchar && *errchar)) {
996 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1000 b = Key_StringToKeynum (Cmd_Argv (2));
1002 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
1006 Key_SetBinding (b, m, "");
1010 Key_In_Bind_f (void)
1013 char cmd[MAX_INPUTLINE];
1014 char *errchar = NULL;
1018 if (c != 3 && c != 4) {
1019 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
1023 m = strtol(Cmd_Argv (1), &errchar, 0);
1024 if ((m < 0) || (m >= 8) || (errchar && *errchar)) {
1025 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1029 b = Key_StringToKeynum (Cmd_Argv (2));
1030 if (b == -1 || b >= MAX_KEYS) {
1031 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
1036 if (keybindings[m][b])
1037 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
1039 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
1042 // copy the rest of the command line
1043 cmd[0] = 0; // start out with a null string
1044 for (i = 3; i < c; i++) {
1045 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
1047 strlcat (cmd, " ", sizeof (cmd));
1050 Key_SetBinding (b, m, cmd);
1054 Key_In_Bindmap_f (void)
1057 char *errchar = NULL;
1062 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
1066 m1 = strtol(Cmd_Argv (1), &errchar, 0);
1067 if ((m1 < 0) || (m1 >= 8) || (errchar && *errchar)) {
1068 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1072 m2 = strtol(Cmd_Argv (2), &errchar, 0);
1073 if ((m2 < 0) || (m2 >= 8) || (errchar && *errchar)) {
1074 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(2));
1087 if (Cmd_Argc () != 2) {
1088 Con_Print("unbind <key> : remove commands from a key\n");
1092 b = Key_StringToKeynum (Cmd_Argv (1));
1094 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
1098 Key_SetBinding (b, 0, "");
1102 Key_Unbindall_f (void)
1106 for (j = 0; j < 8; j++)
1107 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1108 if (keybindings[j][i])
1109 Key_SetBinding (i, j, "");
1113 Key_PrintBindList(int j)
1115 char bindbuf[MAX_INPUTLINE];
1119 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1121 p = keybindings[j][i];
1124 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\");
1126 Con_Printf("^2%s ^7= \"%s\"\n", Key_KeynumToString (i), bindbuf);
1128 Con_Printf("^3bindmap %d: ^2%s ^7= \"%s\"\n", j, Key_KeynumToString (i), bindbuf);
1134 Key_In_BindList_f (void)
1137 char *errchar = NULL;
1141 m = strtol(Cmd_Argv(1), &errchar, 0);
1142 if ((m < 0) || (m >= 8) || (errchar && *errchar)) {
1143 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1146 Key_PrintBindList(m);
1150 for (m = 0; m < MAX_BINDMAPS; m++)
1151 Key_PrintBindList(m);
1156 Key_BindList_f (void)
1158 Key_PrintBindList(0);
1165 char cmd[MAX_INPUTLINE];
1169 if (c != 2 && c != 3) {
1170 Con_Print("bind <key> [command] : attach a command to a key\n");
1173 b = Key_StringToKeynum (Cmd_Argv (1));
1174 if (b == -1 || b >= MAX_KEYS) {
1175 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
1180 if (keybindings[0][b])
1181 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
1183 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
1186 // copy the rest of the command line
1187 cmd[0] = 0; // start out with a null string
1188 for (i = 2; i < c; i++) {
1189 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
1191 strlcat (cmd, " ", sizeof (cmd));
1194 Key_SetBinding (b, 0, cmd);
1199 Writes lines containing "bind key value"
1203 Key_WriteBindings (qfile_t *f)
1206 char bindbuf[MAX_INPUTLINE];
1209 for (j = 0; j < MAX_BINDMAPS; j++)
1211 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1213 p = keybindings[j][i];
1216 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\");
1218 FS_Printf(f, "bind %s \"%s\"\n", Key_KeynumToString (i), bindbuf);
1220 FS_Printf(f, "in_bind %d %s \"%s\"\n", j, Key_KeynumToString (i), bindbuf);
1236 // register our functions
1238 Cmd_AddCommand ("in_bind", Key_In_Bind_f, "binds a command to the specified key in the selected bindmap");
1239 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f, "removes command on the specified key in the selected bindmap");
1240 Cmd_AddCommand ("in_bindlist", Key_In_BindList_f, "bindlist: displays bound keys for all bindmaps, or the given bindmap");
1241 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");
1243 Cmd_AddCommand ("bind", Key_Bind_f, "binds a command to the specified key in bindmap 0");
1244 Cmd_AddCommand ("unbind", Key_Unbind_f, "removes a command on the specified key in bindmap 0");
1245 Cmd_AddCommand ("bindlist", Key_BindList_f, "bindlist: displays bound keys for bindmap 0 bindmaps");
1246 Cmd_AddCommand ("unbindall", Key_Unbindall_f, "removes all commands from all keys in all bindmaps (leaving only shift-escape and escape)");
1248 Cvar_RegisterVariable (&con_closeontoggleconsole);
1254 Key_History_Shutdown();
1257 const char *Key_GetBind (int key)
1260 if (key < 0 || key >= MAX_KEYS)
1262 bind = keybindings[key_bmap][key];
1264 bind = keybindings[key_bmap2][key];
1268 qboolean CL_VM_InputEvent (qboolean down, int key, int ascii);
1272 Called by the system between frames for both key up and key down events
1273 Should NOT be called during an interrupt!
1276 static char tbl_keyascii[MAX_KEYS];
1277 static keydest_t tbl_keydest[MAX_KEYS];
1280 Key_Event (int key, int ascii, qboolean down)
1284 keydest_t keydest = key_dest;
1286 if (key < 0 || key >= MAX_KEYS)
1290 bind = keybindings[key_bmap][key];
1292 bind = keybindings[key_bmap2][key];
1294 if (developer.integer >= 1000)
1295 Con_Printf("Key_Event(%i, '%c', %s) keydown %i bind \"%s\"\n", key, ascii, down ? "down" : "up", keydown[key], bind ? bind : "");
1297 if(key_consoleactive)
1298 keydest = key_console;
1302 // increment key repeat count each time a down is received so that things
1303 // which want to ignore key repeat can ignore it
1304 keydown[key] = min(keydown[key] + 1, 2);
1305 if(keydown[key] == 1) {
1306 tbl_keyascii[key] = ascii;
1307 tbl_keydest[key] = keydest;
1309 ascii = tbl_keyascii[key];
1310 keydest = tbl_keydest[key];
1315 // clear repeat count now that the key is released
1317 keydest = tbl_keydest[key];
1318 ascii = tbl_keyascii[key];
1321 if(keydest == key_void)
1324 // key_consoleactive is a flag not a key_dest because the console is a
1325 // high priority overlay ontop of the normal screen (designed as a safety
1326 // feature so that developers and users can rescue themselves from a bad
1329 // this also means that toggling the console on/off does not lose the old
1332 // specially handle escape (togglemenu) and shift-escape (toggleconsole)
1333 // engine bindings, these are not handled as normal binds so that the user
1334 // can recover from a completely empty bindmap
1335 if (key == K_ESCAPE)
1337 // ignore key repeats on escape
1338 if (keydown[key] > 1)
1341 // escape does these things:
1342 // key_consoleactive - close console
1343 // key_message - abort messagemode
1344 // key_menu - go to parent menu (or key_game)
1345 // key_game - open menu
1347 // in all modes shift-escape toggles console
1348 if (keydown[K_SHIFT])
1352 Con_ToggleConsole_f ();
1353 tbl_keydest[key] = key_void; // esc release should go nowhere (especially not to key_menu or key_game)
1363 if(key_consoleactive & KEY_CONSOLEACTIVE_FORCED)
1365 key_consoleactive &= ~KEY_CONSOLEACTIVE_USER;
1369 Con_ToggleConsole_f();
1375 Key_Message (key, ascii); // that'll close the message input
1379 case key_menu_grabbed:
1380 MR_KeyEvent (key, ascii, down);
1384 // csqc has priority over toggle menu if it wants to (e.g. handling escape for UI stuff in-game.. :sick:)
1385 q = CL_VM_InputEvent(down, key, ascii);
1391 Con_Printf ("Key_Event: Bad key_dest\n");
1396 // send function keydowns to interpreter no matter what mode is (unless the menu has specifically grabbed the keyboard, for rebinding keys)
1397 if (keydest != key_menu_grabbed)
1398 if (key >= K_F1 && key <= K_F12)
1402 if(keydown[key] == 1 && down)
1404 // button commands add keynum as a parm
1406 Cbuf_AddText (va("%s %i\n", bind, key));
1409 Cbuf_AddText (bind);
1410 Cbuf_AddText ("\n");
1412 } else if(bind[0] == '+' && !down && keydown[key] == 0)
1413 Cbuf_AddText(va("-%s %i\n", bind + 1, key));
1418 // send input to console if it wants it
1419 if (keydest == key_console)
1423 // con_closeontoggleconsole enables toggleconsole keys to close the
1424 // console, as long as they are not the color prefix character
1425 // (special exemption for german keyboard layouts)
1426 if (con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && (key_consoleactive & KEY_CONSOLEACTIVE_USER) && ascii != STRING_COLOR_TAG)
1428 Con_ToggleConsole_f ();
1431 Key_Console (key, ascii);
1435 // handle toggleconsole in menu too
1436 if (keydest == key_menu)
1438 if (down && con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && ascii != STRING_COLOR_TAG)
1440 Con_ToggleConsole_f ();
1441 tbl_keydest[key] = key_void; // key release should go nowhere (especially not to key_menu or key_game)
1446 // ignore binds while a video is played, let the video system handle the key event
1447 if (cl_videoplaying)
1449 CL_Video_KeyEvent (key, ascii, keydown[key] != 0);
1453 // anything else is a key press into the game, chat line, or menu
1458 Key_Message (key, ascii);
1461 case key_menu_grabbed:
1462 MR_KeyEvent (key, ascii, down);
1465 q = CL_VM_InputEvent(down, key, ascii);
1466 // ignore key repeats on binds and only send the bind if the event hasnt been already processed by csqc
1469 if(keydown[key] == 1 && down)
1471 // button commands add keynum as a parm
1473 Cbuf_AddText (va("%s %i\n", bind, key));
1476 Cbuf_AddText (bind);
1477 Cbuf_AddText ("\n");
1479 } else if(bind[0] == '+' && !down && keydown[key] == 0)
1480 Cbuf_AddText(va("-%s %i\n", bind + 1, key));
1484 Con_Printf ("Key_Event: Bad key_dest\n");
1494 Key_ClearStates (void)
1496 memset(keydown, 0, sizeof(keydown));