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
28 cvar_t con_closeontoggleconsole = {CVAR_SAVE, "con_closeontoggleconsole","1", "allows toggleconsole binds to close the console as well; when set to 2, this even works when not at the start of the line in console input; when set to 3, this works even if the toggleconsole key is the color tag"};
31 key up events are sent even if in console mode
34 char key_line[MAX_INPUTLINE];
36 qboolean key_insert = true; // insert key toggle (for editing)
38 int key_consoleactive;
39 char *keybindings[MAX_BINDMAPS][MAX_KEYS];
42 char history_savedline[MAX_INPUTLINE];
43 char history_searchstring[MAX_INPUTLINE];
44 qboolean history_matchfound = false;
47 extern cvar_t con_textsize;
50 static void Key_History_Init(void)
53 ConBuffer_Init(&history, HIST_TEXTSIZE, HIST_MAXLINES, zonemempool);
55 // not necessary for mobile
56 #ifndef DP_MOBILETOUCH
57 historyfile = FS_OpenRealFile("darkplaces_history.txt", "rb", false); // rb to handle unix line endings on windows too
60 char buf[MAX_INPUTLINE];
67 c = FS_Getc(historyfile);
68 if(c < 0 || c == 0 || c == '\r' || c == '\n')
73 ConBuffer_AddLine(&history, buf, bufpos, 0);
81 if(bufpos < MAX_INPUTLINE - 1)
86 FS_Close(historyfile);
93 static void Key_History_Shutdown(void)
95 // TODO write history to a file
97 // not necessary for mobile
98 #ifndef DP_MOBILETOUCH
99 qfile_t *historyfile = FS_OpenRealFile("darkplaces_history.txt", "w", false);
103 for(i = 0; i < CONBUFFER_LINES_COUNT(&history); ++i)
104 FS_Printf(historyfile, "%s\n", ConBuffer_GetLine(&history, i));
105 FS_Close(historyfile);
109 ConBuffer_Shutdown(&history);
112 static void Key_History_Push(void)
114 if(key_line[1]) // empty?
115 if(strcmp(key_line, "]quit")) // putting these into the history just sucks
116 if(strncmp(key_line, "]quit ", 6)) // putting these into the history just sucks
117 if(strcmp(key_line, "]rcon_password")) // putting these into the history just sucks
118 if(strncmp(key_line, "]rcon_password ", 15)) // putting these into the history just sucks
119 ConBuffer_AddLine(&history, key_line + 1, (int)strlen(key_line) - 1, 0);
120 Con_Printf("%s\n", key_line); // don't mark empty lines as history
122 if (history_matchfound)
123 history_matchfound = false;
126 static qboolean Key_History_Get_foundCommand(void)
128 if (!history_matchfound)
130 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
131 key_linepos = (int)strlen(key_line);
132 history_matchfound = false;
136 static void Key_History_Up(void)
138 if(history_line == -1) // editing the "new" line
139 strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
141 if (Key_History_Get_foundCommand())
144 if(history_line == -1)
146 history_line = CONBUFFER_LINES_COUNT(&history) - 1;
147 if(history_line != -1)
149 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
150 key_linepos = (int)strlen(key_line);
153 else if(history_line > 0)
155 --history_line; // this also does -1 -> 0, so it is good
156 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
157 key_linepos = (int)strlen(key_line);
161 static void Key_History_Down(void)
163 if(history_line == -1) // editing the "new" line
166 if (Key_History_Get_foundCommand())
169 if(history_line < CONBUFFER_LINES_COUNT(&history) - 1)
172 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
177 strlcpy(key_line + 1, history_savedline, sizeof(key_line) - 1);
180 key_linepos = (int)strlen(key_line);
183 static void Key_History_First(void)
185 if(history_line == -1) // editing the "new" line
186 strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
188 if (CONBUFFER_LINES_COUNT(&history) > 0)
191 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
192 key_linepos = (int)strlen(key_line);
196 static void Key_History_Last(void)
198 if(history_line == -1) // editing the "new" line
199 strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
201 if (CONBUFFER_LINES_COUNT(&history) > 0)
203 history_line = CONBUFFER_LINES_COUNT(&history) - 1;
204 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
205 key_linepos = (int)strlen(key_line);
209 static void Key_History_Find_Backwards(void)
212 const char *partial = key_line + 1;
214 size_t digits = strlen(va(vabuf, sizeof(vabuf), "%i", HIST_MAXLINES));
216 if (history_line == -1) // editing the "new" line
217 strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
219 if (strcmp(key_line + 1, history_searchstring)) // different string? Start a new search
221 strlcpy(history_searchstring, key_line + 1, sizeof(history_searchstring));
222 i = CONBUFFER_LINES_COUNT(&history) - 1;
224 else if (history_line == -1)
225 i = CONBUFFER_LINES_COUNT(&history) - 1;
227 i = history_line - 1;
231 else if (!( strchr(partial, '*') || strchr(partial, '?') )) // no pattern?
232 partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
235 if (matchpattern_with_separator(ConBuffer_GetLine(&history, i), partial, true, "", false))
237 Con_Printf("^2%*i^7 %s\n", (int)digits, i+1, ConBuffer_GetLine(&history, i));
239 history_matchfound = true;
244 static void Key_History_Find_Forwards(void)
247 const char *partial = key_line + 1;
249 size_t digits = strlen(va(vabuf, sizeof(vabuf), "%i", HIST_MAXLINES));
251 if (history_line == -1) // editing the "new" line
254 if (strcmp(key_line + 1, history_searchstring)) // different string? Start a new search
256 strlcpy(history_searchstring, key_line + 1, sizeof(history_searchstring));
259 else i = history_line + 1;
263 else if (!( strchr(partial, '*') || strchr(partial, '?') )) // no pattern?
264 partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
266 for ( ; i < CONBUFFER_LINES_COUNT(&history); i++)
267 if (matchpattern_with_separator(ConBuffer_GetLine(&history, i), partial, true, "", false))
269 Con_Printf("^2%*i^7 %s\n", (int)digits, i+1, ConBuffer_GetLine(&history, i));
271 history_matchfound = true;
276 static void Key_History_Find_All(void)
278 const char *partial = key_line + 1;
281 size_t digits = strlen(va(vabuf, sizeof(vabuf), "%i", HIST_MAXLINES));
282 Con_Printf("History commands containing \"%s\":\n", key_line + 1);
286 else if (!( strchr(partial, '*') || strchr(partial, '?') )) // no pattern?
287 partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
289 for (i=0; i<CONBUFFER_LINES_COUNT(&history); i++)
290 if (matchpattern_with_separator(ConBuffer_GetLine(&history, i), partial, true, "", false))
292 Con_Printf("%s%*i^7 %s\n", (i == history_line) ? "^2" : "^3", (int)digits, i+1, ConBuffer_GetLine(&history, i));
295 Con_Printf("%i result%s\n\n", count, (count != 1) ? "s" : "");
298 static void Key_History_f(void)
300 char *errchar = NULL;
303 size_t digits = strlen(va(vabuf, sizeof(vabuf), "%i", HIST_MAXLINES));
307 if (!strcmp(Cmd_Argv (1), "-c"))
309 ConBuffer_Clear(&history);
312 i = strtol(Cmd_Argv (1), &errchar, 0);
313 if ((i < 0) || (i > CONBUFFER_LINES_COUNT(&history)) || (errchar && *errchar))
316 i = CONBUFFER_LINES_COUNT(&history) - i;
319 for ( ; i<CONBUFFER_LINES_COUNT(&history); i++)
320 Con_Printf("^3%*i^7 %s\n", (int)digits, i+1, ConBuffer_GetLine(&history, i));
324 static int key_bmap, key_bmap2;
325 static unsigned char keydown[MAX_KEYS]; // 0 = up, 1 = down, 2 = repeating
327 typedef struct keyname_s
334 static const keyname_t keynames[] = {
337 {"ESCAPE", K_ESCAPE},
340 // spacer so it lines up with keys.h
342 {"BACKSPACE", K_BACKSPACE},
343 {"UPARROW", K_UPARROW},
344 {"DOWNARROW", K_DOWNARROW},
345 {"LEFTARROW", K_LEFTARROW},
346 {"RIGHTARROW", K_RIGHTARROW},
374 {"NUMLOCK", K_NUMLOCK},
375 {"CAPSLOCK", K_CAPSLOCK},
376 {"SCROLLOCK", K_SCROLLOCK},
378 {"KP_INS", K_KP_INS },
380 {"KP_END", K_KP_END },
382 {"KP_DOWNARROW", K_KP_DOWNARROW },
384 {"KP_PGDN", K_KP_PGDN },
386 {"KP_LEFTARROW", K_KP_LEFTARROW },
389 {"KP_RIGHTARROW", K_KP_RIGHTARROW },
391 {"KP_HOME", K_KP_HOME },
393 {"KP_UPARROW", K_KP_UPARROW },
395 {"KP_PGUP", K_KP_PGUP },
397 {"KP_DEL", K_KP_DEL },
398 {"KP_PERIOD", K_KP_PERIOD},
399 {"KP_SLASH", K_KP_SLASH },
400 {"KP_DIVIDE", K_KP_DIVIDE},
401 {"KP_MULTIPLY", K_KP_MULTIPLY},
402 {"KP_MINUS", K_KP_MINUS},
403 {"KP_PLUS", K_KP_PLUS},
404 {"KP_ENTER", K_KP_ENTER},
405 {"KP_EQUALS", K_KP_EQUALS},
407 {"PRINTSCREEN", K_PRINTSCREEN},
411 {"MOUSE1", K_MOUSE1},
413 {"MOUSE2", K_MOUSE2},
414 {"MOUSE3", K_MOUSE3},
415 {"MWHEELUP", K_MWHEELUP},
416 {"MWHEELDOWN", K_MWHEELDOWN},
417 {"MOUSE4", K_MOUSE4},
418 {"MOUSE5", K_MOUSE5},
419 {"MOUSE6", K_MOUSE6},
420 {"MOUSE7", K_MOUSE7},
421 {"MOUSE8", K_MOUSE8},
422 {"MOUSE9", K_MOUSE9},
423 {"MOUSE10", K_MOUSE10},
424 {"MOUSE11", K_MOUSE11},
425 {"MOUSE12", K_MOUSE12},
426 {"MOUSE13", K_MOUSE13},
427 {"MOUSE14", K_MOUSE14},
428 {"MOUSE15", K_MOUSE15},
429 {"MOUSE16", K_MOUSE16},
489 {"X360_DPAD_UP", K_X360_DPAD_UP},
490 {"X360_DPAD_DOWN", K_X360_DPAD_DOWN},
491 {"X360_DPAD_LEFT", K_X360_DPAD_LEFT},
492 {"X360_DPAD_RIGHT", K_X360_DPAD_RIGHT},
493 {"X360_START", K_X360_START},
494 {"X360_BACK", K_X360_BACK},
495 {"X360_LEFT_THUMB", K_X360_LEFT_THUMB},
496 {"X360_RIGHT_THUMB", K_X360_RIGHT_THUMB},
497 {"X360_LEFT_SHOULDER", K_X360_LEFT_SHOULDER},
498 {"X360_RIGHT_SHOULDER", K_X360_RIGHT_SHOULDER},
499 {"X360_A", K_X360_A},
500 {"X360_B", K_X360_B},
501 {"X360_X", K_X360_X},
502 {"X360_Y", K_X360_Y},
503 {"X360_LEFT_TRIGGER", K_X360_LEFT_TRIGGER},
504 {"X360_RIGHT_TRIGGER", K_X360_RIGHT_TRIGGER},
505 {"X360_LEFT_THUMB_UP", K_X360_LEFT_THUMB_UP},
506 {"X360_LEFT_THUMB_DOWN", K_X360_LEFT_THUMB_DOWN},
507 {"X360_LEFT_THUMB_LEFT", K_X360_LEFT_THUMB_LEFT},
508 {"X360_LEFT_THUMB_RIGHT", K_X360_LEFT_THUMB_RIGHT},
509 {"X360_RIGHT_THUMB_UP", K_X360_RIGHT_THUMB_UP},
510 {"X360_RIGHT_THUMB_DOWN", K_X360_RIGHT_THUMB_DOWN},
511 {"X360_RIGHT_THUMB_LEFT", K_X360_RIGHT_THUMB_LEFT},
512 {"X360_RIGHT_THUMB_RIGHT", K_X360_RIGHT_THUMB_RIGHT},
514 {"JOY_UP", K_JOY_UP},
515 {"JOY_DOWN", K_JOY_DOWN},
516 {"JOY_LEFT", K_JOY_LEFT},
517 {"JOY_RIGHT", K_JOY_RIGHT},
519 {"SEMICOLON", ';'}, // because a raw semicolon separates commands
523 {"APOSTROPHE", '\''},
524 {"BACKSLASH", '\\'}, // because a raw backslash is used for special characters
526 {"MIDINOTE0", K_MIDINOTE0},
527 {"MIDINOTE1", K_MIDINOTE1},
528 {"MIDINOTE2", K_MIDINOTE2},
529 {"MIDINOTE3", K_MIDINOTE3},
530 {"MIDINOTE4", K_MIDINOTE4},
531 {"MIDINOTE5", K_MIDINOTE5},
532 {"MIDINOTE6", K_MIDINOTE6},
533 {"MIDINOTE7", K_MIDINOTE7},
534 {"MIDINOTE8", K_MIDINOTE8},
535 {"MIDINOTE9", K_MIDINOTE9},
536 {"MIDINOTE10", K_MIDINOTE10},
537 {"MIDINOTE11", K_MIDINOTE11},
538 {"MIDINOTE12", K_MIDINOTE12},
539 {"MIDINOTE13", K_MIDINOTE13},
540 {"MIDINOTE14", K_MIDINOTE14},
541 {"MIDINOTE15", K_MIDINOTE15},
542 {"MIDINOTE16", K_MIDINOTE16},
543 {"MIDINOTE17", K_MIDINOTE17},
544 {"MIDINOTE18", K_MIDINOTE18},
545 {"MIDINOTE19", K_MIDINOTE19},
546 {"MIDINOTE20", K_MIDINOTE20},
547 {"MIDINOTE21", K_MIDINOTE21},
548 {"MIDINOTE22", K_MIDINOTE22},
549 {"MIDINOTE23", K_MIDINOTE23},
550 {"MIDINOTE24", K_MIDINOTE24},
551 {"MIDINOTE25", K_MIDINOTE25},
552 {"MIDINOTE26", K_MIDINOTE26},
553 {"MIDINOTE27", K_MIDINOTE27},
554 {"MIDINOTE28", K_MIDINOTE28},
555 {"MIDINOTE29", K_MIDINOTE29},
556 {"MIDINOTE30", K_MIDINOTE30},
557 {"MIDINOTE31", K_MIDINOTE31},
558 {"MIDINOTE32", K_MIDINOTE32},
559 {"MIDINOTE33", K_MIDINOTE33},
560 {"MIDINOTE34", K_MIDINOTE34},
561 {"MIDINOTE35", K_MIDINOTE35},
562 {"MIDINOTE36", K_MIDINOTE36},
563 {"MIDINOTE37", K_MIDINOTE37},
564 {"MIDINOTE38", K_MIDINOTE38},
565 {"MIDINOTE39", K_MIDINOTE39},
566 {"MIDINOTE40", K_MIDINOTE40},
567 {"MIDINOTE41", K_MIDINOTE41},
568 {"MIDINOTE42", K_MIDINOTE42},
569 {"MIDINOTE43", K_MIDINOTE43},
570 {"MIDINOTE44", K_MIDINOTE44},
571 {"MIDINOTE45", K_MIDINOTE45},
572 {"MIDINOTE46", K_MIDINOTE46},
573 {"MIDINOTE47", K_MIDINOTE47},
574 {"MIDINOTE48", K_MIDINOTE48},
575 {"MIDINOTE49", K_MIDINOTE49},
576 {"MIDINOTE50", K_MIDINOTE50},
577 {"MIDINOTE51", K_MIDINOTE51},
578 {"MIDINOTE52", K_MIDINOTE52},
579 {"MIDINOTE53", K_MIDINOTE53},
580 {"MIDINOTE54", K_MIDINOTE54},
581 {"MIDINOTE55", K_MIDINOTE55},
582 {"MIDINOTE56", K_MIDINOTE56},
583 {"MIDINOTE57", K_MIDINOTE57},
584 {"MIDINOTE58", K_MIDINOTE58},
585 {"MIDINOTE59", K_MIDINOTE59},
586 {"MIDINOTE60", K_MIDINOTE60},
587 {"MIDINOTE61", K_MIDINOTE61},
588 {"MIDINOTE62", K_MIDINOTE62},
589 {"MIDINOTE63", K_MIDINOTE63},
590 {"MIDINOTE64", K_MIDINOTE64},
591 {"MIDINOTE65", K_MIDINOTE65},
592 {"MIDINOTE66", K_MIDINOTE66},
593 {"MIDINOTE67", K_MIDINOTE67},
594 {"MIDINOTE68", K_MIDINOTE68},
595 {"MIDINOTE69", K_MIDINOTE69},
596 {"MIDINOTE70", K_MIDINOTE70},
597 {"MIDINOTE71", K_MIDINOTE71},
598 {"MIDINOTE72", K_MIDINOTE72},
599 {"MIDINOTE73", K_MIDINOTE73},
600 {"MIDINOTE74", K_MIDINOTE74},
601 {"MIDINOTE75", K_MIDINOTE75},
602 {"MIDINOTE76", K_MIDINOTE76},
603 {"MIDINOTE77", K_MIDINOTE77},
604 {"MIDINOTE78", K_MIDINOTE78},
605 {"MIDINOTE79", K_MIDINOTE79},
606 {"MIDINOTE80", K_MIDINOTE80},
607 {"MIDINOTE81", K_MIDINOTE81},
608 {"MIDINOTE82", K_MIDINOTE82},
609 {"MIDINOTE83", K_MIDINOTE83},
610 {"MIDINOTE84", K_MIDINOTE84},
611 {"MIDINOTE85", K_MIDINOTE85},
612 {"MIDINOTE86", K_MIDINOTE86},
613 {"MIDINOTE87", K_MIDINOTE87},
614 {"MIDINOTE88", K_MIDINOTE88},
615 {"MIDINOTE89", K_MIDINOTE89},
616 {"MIDINOTE90", K_MIDINOTE90},
617 {"MIDINOTE91", K_MIDINOTE91},
618 {"MIDINOTE92", K_MIDINOTE92},
619 {"MIDINOTE93", K_MIDINOTE93},
620 {"MIDINOTE94", K_MIDINOTE94},
621 {"MIDINOTE95", K_MIDINOTE95},
622 {"MIDINOTE96", K_MIDINOTE96},
623 {"MIDINOTE97", K_MIDINOTE97},
624 {"MIDINOTE98", K_MIDINOTE98},
625 {"MIDINOTE99", K_MIDINOTE99},
626 {"MIDINOTE100", K_MIDINOTE100},
627 {"MIDINOTE101", K_MIDINOTE101},
628 {"MIDINOTE102", K_MIDINOTE102},
629 {"MIDINOTE103", K_MIDINOTE103},
630 {"MIDINOTE104", K_MIDINOTE104},
631 {"MIDINOTE105", K_MIDINOTE105},
632 {"MIDINOTE106", K_MIDINOTE106},
633 {"MIDINOTE107", K_MIDINOTE107},
634 {"MIDINOTE108", K_MIDINOTE108},
635 {"MIDINOTE109", K_MIDINOTE109},
636 {"MIDINOTE110", K_MIDINOTE110},
637 {"MIDINOTE111", K_MIDINOTE111},
638 {"MIDINOTE112", K_MIDINOTE112},
639 {"MIDINOTE113", K_MIDINOTE113},
640 {"MIDINOTE114", K_MIDINOTE114},
641 {"MIDINOTE115", K_MIDINOTE115},
642 {"MIDINOTE116", K_MIDINOTE116},
643 {"MIDINOTE117", K_MIDINOTE117},
644 {"MIDINOTE118", K_MIDINOTE118},
645 {"MIDINOTE119", K_MIDINOTE119},
646 {"MIDINOTE120", K_MIDINOTE120},
647 {"MIDINOTE121", K_MIDINOTE121},
648 {"MIDINOTE122", K_MIDINOTE122},
649 {"MIDINOTE123", K_MIDINOTE123},
650 {"MIDINOTE124", K_MIDINOTE124},
651 {"MIDINOTE125", K_MIDINOTE125},
652 {"MIDINOTE126", K_MIDINOTE126},
653 {"MIDINOTE127", K_MIDINOTE127},
659 ==============================================================================
661 LINE TYPING INTO THE CONSOLE
663 ==============================================================================
667 Key_ClearEditLine (int edit_line)
669 memset (key_line, '\0', sizeof(key_line));
676 Interactive line editing and console scrollback
680 Key_Console (int key, int unicode)
682 // LordHavoc: copied most of this from Q2 to improve keyboard handling
709 case K_KP_RIGHTARROW:
729 if ((key == 'v' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
732 if ((cbd = Sys_GetClipboardData()) != 0)
739 if (*p == '\r' && *(p+1) == '\n')
744 else if (*p == '\n' || *p == '\r' || *p == '\b')
749 strtok(cbd, "\n\r\b");
751 i = (int)strlen(cbd);
752 if (i + key_linepos >= MAX_INPUTLINE)
753 i= MAX_INPUTLINE - key_linepos - 1;
757 memmove(key_line + key_linepos + i, key_line + key_linepos, sizeof(key_line) - key_linepos - i);
758 memcpy(key_line + key_linepos, cbd, i);
766 if (key == 'l' && keydown[K_CTRL])
768 Cbuf_AddText ("clear\n");
772 if (key == 'u' && keydown[K_CTRL]) // like vi/readline ^u: delete currently edited line
781 if (key == 'q' && keydown[K_CTRL]) // like zsh ^q: push line to history, don't execute, and clear
791 if (key == K_ENTER || key == K_KP_ENTER)
793 Cbuf_AddText (key_line+1); // skip the ]
797 key_line[1] = 0; // EvilTypeGuy: null terminate
799 // force an update, because the command may take some time
800 if (cls.state == ca_disconnected)
807 if(keydown[K_CTRL]) // append to the cvar its value
809 int cvar_len, cvar_str_len, chars_to_move;
811 char cvar[MAX_INPUTLINE];
812 const char *cvar_str;
814 // go to the start of the variable
817 k = key_line[key_linepos];
818 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
823 // save the variable name in cvar
824 for(cvar_len=0; (k = key_line[key_linepos + cvar_len]) != 0; cvar_len++)
826 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
834 // go to the end of the cvar
835 key_linepos += cvar_len;
837 // save the content of the variable in cvar_str
838 cvar_str = Cvar_VariableString(cvar);
839 cvar_str_len = (int)strlen(cvar_str);
843 // insert space and cvar_str in key_line
844 chars_to_move = (int)strlen(&key_line[key_linepos]);
845 if (key_linepos + 1 + cvar_str_len + chars_to_move < MAX_INPUTLINE)
848 memmove(&key_line[key_linepos + 1 + cvar_str_len], &key_line[key_linepos], chars_to_move);
849 key_line[key_linepos++] = ' ';
850 memcpy(&key_line[key_linepos], cvar_str, cvar_str_len);
851 key_linepos += cvar_str_len;
852 key_line[key_linepos + chars_to_move] = 0;
855 Con_Printf("Couldn't append cvar value, edit line too long.\n");
858 // Enhanced command completion
859 // by EvilTypeGuy eviltypeguy@qeradiant.com
860 // Thanks to Fett, Taniwha
861 Con_CompleteCommandLine();
865 // Advanced Console Editing by Radix radix@planetquake.com
866 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
868 // Enhanced by terencehill
870 // move cursor to the previous character
871 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
875 if(keydown[K_CTRL]) // move cursor to the previous word
881 if(pos) // skip all "; ' after the word
885 if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
893 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
896 key_linepos = pos + 1;
898 else if(keydown[K_SHIFT]) // move cursor to the previous character ignoring colors
902 pos = (int)u8_prevbyte(key_line+1, key_linepos-1) + 1; // do NOT give the ']' to u8_prevbyte
904 if(pos-1 > 0 && key_line[pos-1] == STRING_COLOR_TAG && isdigit(key_line[pos]))
906 else if(pos-4 > 0 && key_line[pos-4] == STRING_COLOR_TAG && key_line[pos-3] == STRING_COLOR_RGB_TAG_CHAR
907 && isxdigit(key_line[pos-2]) && isxdigit(key_line[pos-1]) && isxdigit(key_line[pos]))
911 if(pos-1 > 0 && key_line[pos-1] == STRING_COLOR_TAG && key_line[pos] == STRING_COLOR_TAG) // consider ^^ as a character
916 // we need to move to the beginning of the character when in a wide character:
917 u8_charidx(key_line, pos + 1, &inchar);
918 key_linepos = (int)(pos + 1 - inchar);
922 key_linepos = (int)u8_prevbyte(key_line+1, key_linepos-1) + 1; // do NOT give the ']' to u8_prevbyte
927 // delete char before cursor
928 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
932 int newpos = (int)u8_prevbyte(key_line+1, key_linepos-1) + 1; // do NOT give the ']' to u8_prevbyte
933 strlcpy(key_line + newpos, key_line + key_linepos, sizeof(key_line) + 1 - key_linepos);
934 key_linepos = newpos;
939 // delete char on cursor
940 if (key == K_DEL || key == K_KP_DEL)
943 linelen = strlen(key_line);
944 if (key_linepos < (int)linelen)
945 memmove(key_line + key_linepos, key_line + key_linepos + u8_bytelen(key_line + key_linepos, 1), linelen - key_linepos);
950 // move cursor to the next character
951 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
953 if (key_linepos >= (int)strlen(key_line))
955 if(keydown[K_CTRL]) // move cursor to the next word
959 len = (int)strlen(key_line);
965 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
969 if (pos < len) // skip all "; ' after the word
973 if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
978 else if(keydown[K_SHIFT]) // move cursor to the next character ignoring colors
981 len = (int)strlen(key_line);
984 // go beyond all initial consecutive color tags, if any
986 while (key_line[pos] == STRING_COLOR_TAG)
988 if(isdigit(key_line[pos+1]))
990 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]))
997 if (key_line[pos] == STRING_COLOR_TAG && key_line[pos+1] == STRING_COLOR_TAG) // consider ^^ as a character
999 pos += (int)u8_bytelen(key_line + pos, 1);
1001 // now go beyond all next consecutive color tags, if any
1003 while (key_line[pos] == STRING_COLOR_TAG)
1005 if(isdigit(key_line[pos+1]))
1007 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]))
1015 key_linepos += (int)u8_bytelen(key_line + key_linepos, 1);
1019 if (key == K_INS || key == K_KP_INS) // toggle insert mode
1025 // End Advanced Console Editing
1027 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
1033 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
1039 if (keydown[K_CTRL])
1041 // prints all the matching commands
1044 Key_History_Find_All();
1047 // Search forwards/backwards, pointing the history's index to the
1048 // matching command but without fetching it to let one continue the search.
1049 // To fetch it, it suffices to just press UP or DOWN.
1052 if (keydown[K_SHIFT])
1053 Key_History_Find_Forwards();
1055 Key_History_Find_Backwards();
1058 // go to the last/first command of the history
1061 Key_History_First();
1071 if (key == K_PGUP || key == K_KP_PGUP)
1075 con_backscroll += ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
1078 con_backscroll += ((vid_conheight.integer >> 1) / con_textsize.integer)-3;
1082 if (key == K_PGDN || key == K_KP_PGDN)
1086 con_backscroll -= ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
1089 con_backscroll -= ((vid_conheight.integer >> 1) / con_textsize.integer)-3;
1093 if (key == K_MWHEELUP)
1096 con_backscroll += 1;
1097 else if(keydown[K_SHIFT])
1098 con_backscroll += ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
1100 con_backscroll += 5;
1104 if (key == K_MWHEELDOWN)
1107 con_backscroll -= 1;
1108 else if(keydown[K_SHIFT])
1109 con_backscroll -= ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
1111 con_backscroll -= 5;
1115 if (keydown[K_CTRL])
1118 if (key == '+' || key == K_KP_PLUS)
1120 if (con_textsize.integer < 128)
1121 Cvar_SetValueQuick(&con_textsize, con_textsize.integer + 1);
1125 if (key == '-' || key == K_KP_MINUS)
1127 if (con_textsize.integer > 1)
1128 Cvar_SetValueQuick(&con_textsize, con_textsize.integer - 1);
1132 if (key == '0' || key == K_KP_INS)
1134 Cvar_SetValueQuick(&con_textsize, atoi(Cvar_VariableDefString("con_textsize")));
1139 if (key == K_HOME || key == K_KP_HOME)
1141 if (keydown[K_CTRL])
1142 con_backscroll = CON_TEXTSIZE;
1148 if (key == K_END || key == K_KP_END)
1150 if (keydown[K_CTRL])
1153 key_linepos = (int)strlen(key_line);
1161 if (key_linepos < MAX_INPUTLINE-1)
1166 blen = u8_fromchar(unicode, buf, sizeof(buf));
1169 len = (int)strlen(&key_line[key_linepos]);
1170 // check insert mode, or always insert if at end of line
1171 if (key_insert || len == 0)
1173 if (key_linepos + len + blen >= MAX_INPUTLINE)
1175 // can't use strcpy to move string to right
1177 if (key_linepos + blen + len >= MAX_INPUTLINE)
1179 memmove(&key_line[key_linepos + blen], &key_line[key_linepos], len);
1181 else if (key_linepos + len + blen - u8_bytelen(key_line + key_linepos, 1) >= MAX_INPUTLINE)
1183 memcpy(key_line + key_linepos, buf, blen);
1185 key_line[key_linepos + blen] = 0;
1187 key_linepos += blen;
1191 //============================================================================
1194 char chat_buffer[MAX_INPUTLINE];
1195 unsigned int chat_bufferlen = 0;
1198 Key_Message (int key, int ascii)
1201 if (key == K_ENTER || key == K_KP_ENTER || ascii == 10 || ascii == 13)
1204 Cmd_ExecuteString(chat_buffer, src_command, true); // not Cbuf_AddText to allow semiclons in args; however, this allows no variables then. Use aliases!
1206 Cmd_ForwardStringToServer(va(vabuf, sizeof(vabuf), "%s %s", chat_mode ? "say_team" : "say ", chat_buffer));
1208 key_dest = key_game;
1214 // TODO add support for arrow keys and simple editing
1216 if (key == K_ESCAPE) {
1217 key_dest = key_game;
1223 if (key == K_BACKSPACE) {
1224 if (chat_bufferlen) {
1225 chat_bufferlen = (unsigned int)u8_prevbyte(chat_buffer, chat_bufferlen);
1226 chat_buffer[chat_bufferlen] = 0;
1232 chat_bufferlen = Nicks_CompleteChatLine(chat_buffer, sizeof(chat_buffer), chat_bufferlen);
1236 // ctrl+key generates an ascii value < 32 and shows a char from the charmap
1237 if (ascii > 0 && ascii < 32 && utf8_enable.integer)
1238 ascii = 0xE000 + ascii;
1240 if (chat_bufferlen == sizeof (chat_buffer) - 1)
1244 return; // non printable
1246 chat_bufferlen += u8_fromchar(ascii, chat_buffer+chat_bufferlen, sizeof(chat_buffer) - chat_bufferlen - 1);
1248 //chat_buffer[chat_bufferlen++] = ascii;
1249 //chat_buffer[chat_bufferlen] = 0;
1252 //============================================================================
1257 Returns a key number to be used to index keybindings[] by looking at
1258 the given string. Single ascii characters return themselves, while
1259 the K_* names are matched up.
1263 Key_StringToKeynum (const char *str)
1265 const keyname_t *kn;
1267 if (!str || !str[0])
1270 return tolower(str[0]);
1272 for (kn = keynames; kn->name; kn++) {
1273 if (!strcasecmp (str, kn->name))
1281 Returns a string (either a single ascii char, or a K_* name) for the
1283 FIXME: handle quote special (general escape sequence?)
1287 Key_KeynumToString (int keynum, char *tinystr, size_t tinystrlength)
1289 const keyname_t *kn;
1291 // -1 is an invalid code
1293 return "<KEY NOT FOUND>";
1295 // search overrides first, because some characters are special
1296 for (kn = keynames; kn->name; kn++)
1297 if (keynum == kn->keynum)
1300 // if it is printable, output it as a single character
1301 if (keynum > 32 && keynum < 256)
1303 if (tinystrlength >= 2)
1305 tinystr[0] = keynum;
1311 // if it is not overridden and not printable, we don't know what to do with it
1312 return "<UNKNOWN KEYNUM>";
1317 Key_SetBinding (int keynum, int bindmap, const char *binding)
1322 if (keynum == -1 || keynum >= MAX_KEYS)
1324 if ((bindmap < 0) || (bindmap >= MAX_BINDMAPS))
1327 // free old bindings
1328 if (keybindings[bindmap][keynum]) {
1329 Z_Free (keybindings[bindmap][keynum]);
1330 keybindings[bindmap][keynum] = NULL;
1332 if(!binding[0]) // make "" binds be removed --blub
1334 // allocate memory for new binding
1335 l = strlen (binding);
1336 newbinding = (char *)Z_Malloc (l + 1);
1337 memcpy (newbinding, binding, l + 1);
1339 keybindings[bindmap][keynum] = newbinding;
1343 void Key_GetBindMap(int *fg, int *bg)
1351 qboolean Key_SetBindMap(int fg, int bg)
1353 if(fg >= MAX_BINDMAPS)
1355 if(bg >= MAX_BINDMAPS)
1365 Key_In_Unbind_f (void)
1368 char *errchar = NULL;
1370 if (Cmd_Argc () != 3) {
1371 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
1375 m = strtol(Cmd_Argv (1), &errchar, 0);
1376 if ((m < 0) || (m >= MAX_BINDMAPS) || (errchar && *errchar)) {
1377 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1381 b = Key_StringToKeynum (Cmd_Argv (2));
1383 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
1387 if(!Key_SetBinding (b, m, ""))
1388 Con_Printf("Key_SetBinding failed for unknown reason\n");
1392 Key_In_Bind_f (void)
1395 char cmd[MAX_INPUTLINE];
1396 char *errchar = NULL;
1400 if (c != 3 && c != 4) {
1401 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
1405 m = strtol(Cmd_Argv (1), &errchar, 0);
1406 if ((m < 0) || (m >= MAX_BINDMAPS) || (errchar && *errchar)) {
1407 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1411 b = Key_StringToKeynum (Cmd_Argv (2));
1412 if (b == -1 || b >= MAX_KEYS) {
1413 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
1418 if (keybindings[m][b])
1419 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
1421 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
1424 // copy the rest of the command line
1425 cmd[0] = 0; // start out with a null string
1426 for (i = 3; i < c; i++) {
1427 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
1429 strlcat (cmd, " ", sizeof (cmd));
1432 if(!Key_SetBinding (b, m, cmd))
1433 Con_Printf("Key_SetBinding failed for unknown reason\n");
1437 Key_In_Bindmap_f (void)
1440 char *errchar = NULL;
1445 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
1449 m1 = strtol(Cmd_Argv (1), &errchar, 0);
1450 if ((m1 < 0) || (m1 >= MAX_BINDMAPS) || (errchar && *errchar)) {
1451 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1455 m2 = strtol(Cmd_Argv (2), &errchar, 0);
1456 if ((m2 < 0) || (m2 >= MAX_BINDMAPS) || (errchar && *errchar)) {
1457 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(2));
1470 if (Cmd_Argc () != 2) {
1471 Con_Print("unbind <key> : remove commands from a key\n");
1475 b = Key_StringToKeynum (Cmd_Argv (1));
1477 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
1481 if(!Key_SetBinding (b, 0, ""))
1482 Con_Printf("Key_SetBinding failed for unknown reason\n");
1486 Key_Unbindall_f (void)
1490 for (j = 0; j < MAX_BINDMAPS; j++)
1491 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1492 if (keybindings[j][i])
1493 Key_SetBinding (i, j, "");
1497 Key_PrintBindList(int j)
1499 char bindbuf[MAX_INPUTLINE];
1504 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1506 p = keybindings[j][i];
1509 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\", false);
1511 Con_Printf("^2%s ^7= \"%s\"\n", Key_KeynumToString (i, tinystr, sizeof(tinystr)), bindbuf);
1513 Con_Printf("^3bindmap %d: ^2%s ^7= \"%s\"\n", j, Key_KeynumToString (i, tinystr, sizeof(tinystr)), bindbuf);
1519 Key_In_BindList_f (void)
1522 char *errchar = NULL;
1526 m = strtol(Cmd_Argv(1), &errchar, 0);
1527 if ((m < 0) || (m >= MAX_BINDMAPS) || (errchar && *errchar)) {
1528 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1531 Key_PrintBindList(m);
1535 for (m = 0; m < MAX_BINDMAPS; m++)
1536 Key_PrintBindList(m);
1541 Key_BindList_f (void)
1543 Key_PrintBindList(0);
1550 char cmd[MAX_INPUTLINE];
1554 if (c != 2 && c != 3) {
1555 Con_Print("bind <key> [command] : attach a command to a key\n");
1558 b = Key_StringToKeynum (Cmd_Argv (1));
1559 if (b == -1 || b >= MAX_KEYS) {
1560 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
1565 if (keybindings[0][b])
1566 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
1568 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
1571 // copy the rest of the command line
1572 cmd[0] = 0; // start out with a null string
1573 for (i = 2; i < c; i++) {
1574 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
1576 strlcat (cmd, " ", sizeof (cmd));
1579 if(!Key_SetBinding (b, 0, cmd))
1580 Con_Printf("Key_SetBinding failed for unknown reason\n");
1585 Writes lines containing "bind key value"
1589 Key_WriteBindings (qfile_t *f)
1592 char bindbuf[MAX_INPUTLINE];
1596 for (j = 0; j < MAX_BINDMAPS; j++)
1598 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1600 p = keybindings[j][i];
1603 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\", false); // don't need to escape $ because cvars are not expanded inside bind
1605 FS_Printf(f, "bind %s \"%s\"\n", Key_KeynumToString (i, tinystr, sizeof(tinystr)), bindbuf);
1607 FS_Printf(f, "in_bind %d %s \"%s\"\n", j, Key_KeynumToString (i, tinystr, sizeof(tinystr)), bindbuf);
1623 // register our functions
1625 Cmd_AddCommand ("in_bind", Key_In_Bind_f, "binds a command to the specified key in the selected bindmap");
1626 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f, "removes command on the specified key in the selected bindmap");
1627 Cmd_AddCommand ("in_bindlist", Key_In_BindList_f, "bindlist: displays bound keys for all bindmaps, or the given bindmap");
1628 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");
1630 Cmd_AddCommand ("bind", Key_Bind_f, "binds a command to the specified key in bindmap 0");
1631 Cmd_AddCommand ("unbind", Key_Unbind_f, "removes a command on the specified key in bindmap 0");
1632 Cmd_AddCommand ("bindlist", Key_BindList_f, "bindlist: displays bound keys for bindmap 0 bindmaps");
1633 Cmd_AddCommand ("unbindall", Key_Unbindall_f, "removes all commands from all keys in all bindmaps (leaving only shift-escape and escape)");
1635 Cmd_AddCommand ("history", Key_History_f, "prints the history of executed commands (history X prints the last X entries, history -c clears the whole history)");
1637 Cvar_RegisterVariable (&con_closeontoggleconsole);
1643 Key_History_Shutdown();
1646 const char *Key_GetBind (int key, int bindmap)
1649 if (key < 0 || key >= MAX_KEYS)
1651 if(bindmap >= MAX_BINDMAPS)
1655 bind = keybindings[bindmap][key];
1659 bind = keybindings[key_bmap][key];
1661 bind = keybindings[key_bmap2][key];
1666 void Key_FindKeysForCommand (const char *command, int *keys, int numkeys, int bindmap)
1672 for (j = 0;j < numkeys;j++)
1675 if(bindmap >= MAX_BINDMAPS)
1680 for (j = 0; j < MAX_KEYS; ++j)
1682 b = Key_GetBind(j, bindmap);
1685 if (!strcmp (b, command) )
1688 if (count == numkeys)
1696 Called by the system between frames for both key up and key down events
1697 Should NOT be called during an interrupt!
1700 static char tbl_keyascii[MAX_KEYS];
1701 static keydest_t tbl_keydest[MAX_KEYS];
1703 typedef struct eventqueueitem_s
1710 static int events_blocked = 0;
1711 static eventqueueitem_t eventqueue[32];
1712 static unsigned eventqueue_idx = 0;
1714 static void Key_EventQueue_Add(int key, int ascii, qboolean down)
1716 if(eventqueue_idx < sizeof(eventqueue) / sizeof(*eventqueue))
1718 eventqueue[eventqueue_idx].key = key;
1719 eventqueue[eventqueue_idx].ascii = ascii;
1720 eventqueue[eventqueue_idx].down = down;
1725 void Key_EventQueue_Block(void)
1727 // block key events until call to Unblock
1728 events_blocked = true;
1731 void Key_EventQueue_Unblock(void)
1733 // unblocks key events again
1735 events_blocked = false;
1736 for(i = 0; i < eventqueue_idx; ++i)
1737 Key_Event(eventqueue[i].key, eventqueue[i].ascii, eventqueue[i].down);
1742 Key_Event (int key, int ascii, qboolean down)
1746 keydest_t keydest = key_dest;
1749 if (key < 0 || key >= MAX_KEYS)
1754 Key_EventQueue_Add(key, ascii, down);
1759 bind = keybindings[key_bmap][key];
1761 bind = keybindings[key_bmap2][key];
1763 if (developer_insane.integer)
1764 Con_DPrintf("Key_Event(%i, '%c', %s) keydown %i bind \"%s\"\n", key, ascii ? ascii : '?', down ? "down" : "up", keydown[key], bind ? bind : "");
1766 if(key_consoleactive)
1767 keydest = key_console;
1771 // increment key repeat count each time a down is received so that things
1772 // which want to ignore key repeat can ignore it
1773 keydown[key] = min(keydown[key] + 1, 2);
1774 if(keydown[key] == 1) {
1775 tbl_keyascii[key] = ascii;
1776 tbl_keydest[key] = keydest;
1778 ascii = tbl_keyascii[key];
1779 keydest = tbl_keydest[key];
1784 // clear repeat count now that the key is released
1786 keydest = tbl_keydest[key];
1787 ascii = tbl_keyascii[key];
1790 if(keydest == key_void)
1793 // key_consoleactive is a flag not a key_dest because the console is a
1794 // high priority overlay ontop of the normal screen (designed as a safety
1795 // feature so that developers and users can rescue themselves from a bad
1798 // this also means that toggling the console on/off does not lose the old
1801 // specially handle escape (togglemenu) and shift-escape (toggleconsole)
1802 // engine bindings, these are not handled as normal binds so that the user
1803 // can recover from a completely empty bindmap
1804 if (key == K_ESCAPE)
1806 // ignore key repeats on escape
1807 if (keydown[key] > 1)
1810 // escape does these things:
1811 // key_consoleactive - close console
1812 // key_message - abort messagemode
1813 // key_menu - go to parent menu (or key_game)
1814 // key_game - open menu
1816 // in all modes shift-escape toggles console
1817 if (keydown[K_SHIFT])
1821 Con_ToggleConsole_f ();
1822 tbl_keydest[key] = key_void; // esc release should go nowhere (especially not to key_menu or key_game)
1832 if(key_consoleactive & KEY_CONSOLEACTIVE_FORCED)
1834 key_consoleactive &= ~KEY_CONSOLEACTIVE_USER;
1840 Con_ToggleConsole_f();
1846 Key_Message (key, ascii); // that'll close the message input
1850 case key_menu_grabbed:
1852 MR_KeyEvent (key, ascii, down);
1857 // csqc has priority over toggle menu if it wants to (e.g. handling escape for UI stuff in-game.. :sick:)
1858 q = CL_VM_InputEvent(down ? 0 : 1, key, ascii);
1866 Con_Printf ("Key_Event: Bad key_dest\n");
1871 // send function keydowns to interpreter no matter what mode is (unless the menu has specifically grabbed the keyboard, for rebinding keys)
1872 // VorteX: Omnicide does bind F* keys
1873 if (keydest != key_menu_grabbed)
1874 if (key >= K_F1 && key <= K_F12 && gamemode != GAME_BLOODOMNICIDE)
1878 if(keydown[key] == 1 && down)
1880 // button commands add keynum as a parm
1882 Cbuf_AddText (va(vabuf, sizeof(vabuf), "%s %i\n", bind, key));
1885 Cbuf_AddText (bind);
1886 Cbuf_AddText ("\n");
1888 } else if(bind[0] == '+' && !down && keydown[key] == 0)
1889 Cbuf_AddText(va(vabuf, sizeof(vabuf), "-%s %i\n", bind + 1, key));
1894 // send input to console if it wants it
1895 if (keydest == key_console)
1899 // con_closeontoggleconsole enables toggleconsole keys to close the
1900 // console, as long as they are not the color prefix character
1901 // (special exemption for german keyboard layouts)
1902 if (con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && (key_consoleactive & KEY_CONSOLEACTIVE_USER) && (con_closeontoggleconsole.integer >= ((ascii != STRING_COLOR_TAG) ? 2 : 3) || key_linepos == 1))
1904 Con_ToggleConsole_f ();
1908 if (COM_CheckParm ("-noconsole"))
1909 return; // only allow the key bind to turn off console
1911 Key_Console (key, ascii);
1915 // handle toggleconsole in menu too
1916 if (keydest == key_menu)
1918 if (down && con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && ascii != STRING_COLOR_TAG)
1920 Cbuf_AddText("toggleconsole\n"); // Deferred to next frame so we're not sending the text event to the console.
1921 tbl_keydest[key] = key_void; // key release should go nowhere (especially not to key_menu or key_game)
1926 // ignore binds while a video is played, let the video system handle the key event
1927 if (cl_videoplaying)
1929 if (gamemode == GAME_BLOODOMNICIDE) // menu controls key events
1931 MR_KeyEvent(key, ascii, down);
1937 CL_Video_KeyEvent (key, ascii, keydown[key] != 0);
1941 // anything else is a key press into the game, chat line, or menu
1946 Key_Message (key, ascii);
1949 case key_menu_grabbed:
1951 MR_KeyEvent (key, ascii, down);
1955 q = CL_VM_InputEvent(down ? 0 : 1, key, ascii);
1956 // ignore key repeats on binds and only send the bind if the event hasnt been already processed by csqc
1959 if(keydown[key] == 1 && down)
1961 // button commands add keynum as a parm
1963 Cbuf_AddText (va(vabuf, sizeof(vabuf), "%s %i\n", bind, key));
1966 Cbuf_AddText (bind);
1967 Cbuf_AddText ("\n");
1969 } else if(bind[0] == '+' && !down && keydown[key] == 0)
1970 Cbuf_AddText(va(vabuf, sizeof(vabuf), "-%s %i\n", bind + 1, key));
1974 Con_Printf ("Key_Event: Bad key_dest\n");
1978 // a helper to simulate release of ALL keys
1980 Key_ReleaseAll (void)
1983 // clear the event queue first
1985 // then send all down events (possibly into the event queue)
1986 for(key = 0; key < MAX_KEYS; ++key)
1988 Key_Event(key, 0, false);
1989 // now all keys are guaranteed down (once the event queue is unblocked)
1990 // and only future events count
1999 Key_ClearStates (void)
2001 memset(keydown, 0, sizeof(keydown));