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 = {CF_CLIENT | CF_ARCHIVE, "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 qbool 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 qbool 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 qbool 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(cmd_state_t *cmd)
300 char *errchar = NULL;
303 size_t digits = strlen(va(vabuf, sizeof(vabuf), "%i", HIST_MAXLINES));
305 if (Cmd_Argc (cmd) > 1)
307 if (!strcmp(Cmd_Argv(cmd, 1), "-c"))
309 ConBuffer_Clear(&history);
312 i = strtol(Cmd_Argv(cmd, 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 ==============================================================================
666 int Key_ClearEditLine(qbool is_console)
681 // key modifier states
682 #define KM_NONE (!keydown[K_CTRL] && !keydown[K_SHIFT] && !keydown[K_ALT])
683 #define KM_CTRL_SHIFT_ALT ( keydown[K_CTRL] && keydown[K_SHIFT] && keydown[K_ALT])
684 #define KM_CTRL_SHIFT ( keydown[K_CTRL] && keydown[K_SHIFT] && !keydown[K_ALT])
685 #define KM_CTRL_ALT ( keydown[K_CTRL] && !keydown[K_SHIFT] && keydown[K_ALT])
686 #define KM_SHIFT_ALT (!keydown[K_CTRL] && keydown[K_SHIFT] && keydown[K_ALT])
687 #define KM_CTRL ( keydown[K_CTRL] && !keydown[K_SHIFT] && !keydown[K_ALT])
688 #define KM_SHIFT (!keydown[K_CTRL] && keydown[K_SHIFT] && !keydown[K_ALT])
689 #define KM_ALT (!keydown[K_CTRL] && !keydown[K_SHIFT] && keydown[K_ALT])
693 Interactive line editing and console scrollback
697 signed char chat_mode; // 0 for say, 1 for say_team, -1 for command
698 char chat_buffer[MAX_INPUTLINE];
699 int chat_bufferpos = 0;
701 int Key_AddChar(int unicode, qbool is_console)
705 int len, blen, linepos;
710 linepos = key_linepos;
715 linepos = chat_bufferpos;
718 if (linepos >= MAX_INPUTLINE-1)
721 blen = u8_fromchar(unicode, buf, sizeof(buf));
724 len = (int)strlen(&line[linepos]);
725 // check insert mode, or always insert if at end of line
726 if (key_insert || len == 0)
728 if (linepos + len + blen >= MAX_INPUTLINE)
730 // can't use strcpy to move string to right
732 if (linepos + blen + len >= MAX_INPUTLINE)
734 memmove(&line[linepos + blen], &line[linepos], len);
736 else if (linepos + len + blen - u8_bytelen(line + linepos, 1) >= MAX_INPUTLINE)
738 memcpy(line + linepos, buf, blen);
740 line[linepos + blen] = 0;
745 // returns -1 if no key has been recognized
746 // returns linepos (>= 0) otherwise
747 // if is_console is true can modify key_line (doesn't change key_linepos)
748 int Key_Parse_CommonKeys(cmd_state_t *cmd, qbool is_console, int key, int unicode)
751 int linepos, linestart;
752 unsigned int linesize;
756 linepos = key_linepos;
757 linesize = sizeof(key_line);
763 linepos = chat_bufferpos;
764 linesize = sizeof(chat_buffer);
768 if ((key == 'v' && KM_CTRL) || ((key == K_INS || key == K_KP_INS) && KM_SHIFT))
771 if ((cbd = Sys_GetClipboardData()) != 0)
778 if (*p == '\r' && *(p+1) == '\n')
783 else if (*p == '\n' || *p == '\r' || *p == '\b')
789 strtok(cbd, "\n\r\b");
791 i = (int)strlen(cbd);
792 if (i + linepos >= MAX_INPUTLINE)
793 i= MAX_INPUTLINE - linepos - 1;
797 memmove(line + linepos + i, line + linepos, linesize - linepos - i);
798 memcpy(line + linepos, cbd, i);
806 if (key == 'u' && KM_CTRL) // like vi/readline ^u: delete currently edited line
808 return Key_ClearEditLine(is_console);
813 if (is_console && KM_CTRL) // append the cvar value to the cvar name
815 int cvar_len, cvar_str_len, chars_to_move;
817 char cvar[MAX_INPUTLINE];
818 const char *cvar_str;
820 // go to the start of the variable
824 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
829 // save the variable name in cvar
830 for(cvar_len=0; (k = line[linepos + cvar_len]) != 0; cvar_len++)
832 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
840 // go to the end of the cvar
843 // save the content of the variable in cvar_str
844 cvar_str = Cvar_VariableString(&cvars_all, cvar, CF_CLIENT | CF_SERVER);
845 cvar_str_len = (int)strlen(cvar_str);
849 // insert space and cvar_str in line
850 chars_to_move = (int)strlen(&line[linepos]);
851 if (linepos + 1 + cvar_str_len + chars_to_move < MAX_INPUTLINE)
854 memmove(&line[linepos + 1 + cvar_str_len], &line[linepos], chars_to_move);
855 line[linepos++] = ' ';
856 memcpy(&line[linepos], cvar_str, cvar_str_len);
857 linepos += cvar_str_len;
858 line[linepos + chars_to_move] = 0;
861 Con_Printf("Couldn't append cvar value, edit line too long.\n");
866 return Con_CompleteCommandLine(cmd, is_console);
869 // Advanced Console Editing by Radix radix@planetquake.com
870 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
872 // Enhanced by terencehill
874 // move cursor to the previous character
875 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
877 if(KM_CTRL) // move cursor to the previous word
881 if (linepos <= linestart + 1)
887 if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
889 } while(pos > linestart); // skip all "; ' after the word
891 if (pos == linestart)
896 if (k == '\"' || k == ';' || k == ' ' || k == '\'')
901 } while(pos > linestart);
907 if(KM_SHIFT) // move cursor to the previous character ignoring colors
911 if (linepos <= linestart + 1)
913 pos = (int)u8_prevbyte(line + linestart, linepos - linestart) + linestart;
914 while (pos > linestart)
915 if(pos-1 >= linestart && line[pos-1] == STRING_COLOR_TAG && isdigit(line[pos]))
917 else if(pos-4 >= linestart && line[pos-4] == STRING_COLOR_TAG && line[pos-3] == STRING_COLOR_RGB_TAG_CHAR
918 && isxdigit(line[pos-2]) && isxdigit(line[pos-1]) && isxdigit(line[pos]))
922 if(pos-1 >= linestart && line[pos-1] == STRING_COLOR_TAG && line[pos] == STRING_COLOR_TAG) // consider ^^ as a character
929 // we need to move to the beginning of the character when in a wide character:
930 u8_charidx(line, pos + 1, &inchar);
931 linepos = (int)(pos + 1 - inchar);
937 if (linepos <= linestart + 1)
939 // hide ']' from u8_prevbyte otherwise it could go out of bounds
940 linepos = (int)u8_prevbyte(line + linestart, linepos - linestart) + linestart;
945 // delete char before cursor
946 if ((key == K_BACKSPACE && KM_NONE) || (key == 'h' && KM_CTRL))
948 if (linepos > linestart)
950 // hide ']' from u8_prevbyte otherwise it could go out of bounds
951 int newpos = (int)u8_prevbyte(line + linestart, linepos - linestart) + linestart;
952 strlcpy(line + newpos, line + linepos, linesize + 1 - linepos);
958 // delete char on cursor
959 if ((key == K_DEL || key == K_KP_DEL) && KM_NONE)
962 linelen = strlen(line);
963 if (linepos < (int)linelen)
964 memmove(line + linepos, line + linepos + u8_bytelen(line + linepos, 1), linelen - linepos);
968 // move cursor to the next character
969 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
971 if (KM_CTRL) // move cursor to the next word
975 len = (int)strlen(line);
983 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
987 if (pos < len) // skip all "; ' after the word
991 if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
998 if (KM_SHIFT) // move cursor to the next character ignoring colors
1001 len = (int)strlen(line);
1006 // go beyond all initial consecutive color tags, if any
1008 while (line[pos] == STRING_COLOR_TAG)
1010 if(isdigit(line[pos+1]))
1012 else if(line[pos+1] == STRING_COLOR_RGB_TAG_CHAR && isxdigit(line[pos+2]) && isxdigit(line[pos+3]) && isxdigit(line[pos+4]))
1019 if (line[pos] == STRING_COLOR_TAG && line[pos+1] == STRING_COLOR_TAG) // consider ^^ as a character
1021 pos += (int)u8_bytelen(line + pos, 1);
1023 // now go beyond all next consecutive color tags, if any
1025 while (line[pos] == STRING_COLOR_TAG)
1027 if(isdigit(line[pos+1]))
1029 else if(line[pos+1] == STRING_COLOR_RGB_TAG_CHAR && isxdigit(line[pos+2]) && isxdigit(line[pos+3]) && isxdigit(line[pos+4]))
1040 if (linepos >= (int)strlen(line))
1042 linepos += (int)u8_bytelen(line + linepos, 1);
1047 if ((key == K_INS || key == K_KP_INS) && KM_NONE) // toggle insert mode
1053 if (key == K_HOME || key == K_KP_HOME)
1055 if (is_console && KM_CTRL)
1057 con_backscroll = CON_TEXTSIZE;
1064 if (key == K_END || key == K_KP_END)
1066 if (is_console && KM_CTRL)
1072 return (int)strlen(line);
1078 static int Key_Convert_NumPadKey(int key)
1080 // LadyHavoc: copied most of this from Q2 to improve keyboard handling
1083 case K_KP_SLASH: return '/';
1084 case K_KP_MINUS: return '-';
1085 case K_KP_PLUS: return '+';
1086 case K_KP_HOME: return '7';
1087 case K_KP_UPARROW: return '8';
1088 case K_KP_PGUP: return '9';
1089 case K_KP_LEFTARROW: return '4';
1090 case K_KP_5: return '5';
1091 case K_KP_RIGHTARROW: return '6';
1092 case K_KP_END: return '1';
1093 case K_KP_DOWNARROW: return '2';
1094 case K_KP_PGDN: return '3';
1095 case K_KP_INS: return '0';
1096 case K_KP_DEL: return '.';
1101 static void Key_Console(cmd_state_t *cmd, int key, int unicode)
1105 key = Key_Convert_NumPadKey(key);
1107 // Forbid Ctrl Alt shortcuts since on Windows they are used to type some characters
1108 // in certain non-English keyboards using the AltGr key (which emulates Ctrl Alt)
1109 // Reference: "Why Ctrl+Alt shouldn't be used as a shortcut modifier"
1110 // https://blogs.msdn.microsoft.com/oldnewthing/20040329-00/?p=40003
1111 if (keydown[K_CTRL] && keydown[K_ALT])
1114 linepos = Key_Parse_CommonKeys(cmd, true, key, unicode);
1117 key_linepos = linepos;
1121 if ((key == K_ENTER || key == K_KP_ENTER) && KM_NONE)
1123 // bones_was_here: prepending allows a loop such as `alias foo "bar; wait; foo"; foo`
1124 // to be broken with an alias or unalias command
1125 Cbuf_InsertText(cmd, key_line+1); // skip the ]
1127 key_linepos = Key_ClearEditLine(true);
1128 // force an update, because the command may take some time
1129 if (cls.state == ca_disconnected)
1134 if (key == 'l' && KM_CTRL)
1136 Cbuf_AddText (cmd, "clear\n");
1140 if (key == 'q' && KM_CTRL) // like zsh ^q: push line to history, don't execute, and clear
1144 key_linepos = Key_ClearEditLine(true);
1148 // End Advanced Console Editing
1150 if (((key == K_UPARROW || key == K_KP_UPARROW) && KM_NONE) || (key == 'p' && KM_CTRL))
1156 if (((key == K_DOWNARROW || key == K_KP_DOWNARROW) && KM_NONE) || (key == 'n' && KM_CTRL))
1162 if (keydown[K_CTRL])
1164 // prints all the matching commands
1165 if (key == 'f' && KM_CTRL)
1167 Key_History_Find_All();
1170 // Search forwards/backwards, pointing the history's index to the
1171 // matching command but without fetching it to let one continue the search.
1172 // To fetch it, it suffices to just press UP or DOWN.
1173 if (key == 'r' && KM_CTRL_SHIFT)
1175 Key_History_Find_Forwards();
1178 if (key == 'r' && KM_CTRL)
1180 Key_History_Find_Backwards();
1184 // go to the last/first command of the history
1185 if (key == ',' && KM_CTRL)
1187 Key_History_First();
1190 if (key == '.' && KM_CTRL)
1197 if (key == K_PGUP || key == K_KP_PGUP)
1201 con_backscroll += ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
1206 con_backscroll += ((vid_conheight.integer >> 1) / con_textsize.integer)-3;
1211 if (key == K_PGDN || key == K_KP_PGDN)
1215 con_backscroll -= ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
1220 con_backscroll -= ((vid_conheight.integer >> 1) / con_textsize.integer)-3;
1225 if (key == K_MWHEELUP)
1229 con_backscroll += 1;
1234 con_backscroll += ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
1239 con_backscroll += 5;
1244 if (key == K_MWHEELDOWN)
1248 con_backscroll -= 1;
1253 con_backscroll -= ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
1258 con_backscroll -= 5;
1263 if (keydown[K_CTRL])
1266 if ((key == '+' || key == K_KP_PLUS) && KM_CTRL)
1268 if (con_textsize.integer < 128)
1269 Cvar_SetValueQuick(&con_textsize, con_textsize.integer + 1);
1273 if ((key == '-' || key == K_KP_MINUS) && KM_CTRL)
1275 if (con_textsize.integer > 1)
1276 Cvar_SetValueQuick(&con_textsize, con_textsize.integer - 1);
1280 if ((key == '0' || key == K_KP_INS) && KM_CTRL)
1282 Cvar_SetValueQuick(&con_textsize, atoi(Cvar_VariableDefString(&cvars_all, "con_textsize", CF_CLIENT | CF_SERVER)));
1293 key_linepos = Key_AddChar(unicode, true);
1296 //============================================================================
1299 Key_Message (cmd_state_t *cmd, int key, int ascii)
1304 key = Key_Convert_NumPadKey(key);
1306 if (key == K_ENTER || key == K_KP_ENTER || ascii == 10 || ascii == 13)
1309 Cmd_ExecuteString(cmd, chat_buffer, src_local, true); // not Cbuf_AddText to allow semiclons in args; however, this allows no variables then. Use aliases!
1311 CL_ForwardToServer(va(vabuf, sizeof(vabuf), "%s %s", chat_mode ? "say_team" : "say ", chat_buffer));
1313 key_dest = key_game;
1314 chat_bufferpos = Key_ClearEditLine(false);
1318 if (key == K_ESCAPE) {
1319 key_dest = key_game;
1320 chat_bufferpos = Key_ClearEditLine(false);
1324 linepos = Key_Parse_CommonKeys(cmd, false, key, ascii);
1327 chat_bufferpos = linepos;
1331 // ctrl+key generates an ascii value < 32 and shows a char from the charmap
1332 if (ascii > 0 && ascii < 32 && utf8_enable.integer)
1333 ascii = 0xE000 + ascii;
1336 return; // non printable
1338 chat_bufferpos = Key_AddChar(ascii, false);
1341 //============================================================================
1346 Returns a key number to be used to index keybindings[] by looking at
1347 the given string. Single ascii characters return themselves, while
1348 the K_* names are matched up.
1352 Key_StringToKeynum (const char *str)
1355 const keyname_t *kn;
1357 if (!str || !str[0])
1360 return tolower(str[0]);
1362 for (kn = keynames; kn->name; kn++) {
1363 if (!strcasecmp (str, kn->name))
1367 // non-ascii keys are Unicode codepoints, so give the character if it's valid;
1368 // error message have more than one character, don't allow it
1369 ch = u8_getnchar(str, &str, 3);
1370 return (ch == 0 || *str != 0) ? -1 : (int)ch;
1375 Returns a string (either a single ascii char, or a K_* name) for the
1377 FIXME: handle quote special (general escape sequence?)
1381 Key_KeynumToString (int keynum, char *tinystr, size_t tinystrlength)
1383 const keyname_t *kn;
1385 // -1 is an invalid code
1387 return "<KEY NOT FOUND>";
1389 // search overrides first, because some characters are special
1390 for (kn = keynames; kn->name; kn++)
1391 if (keynum == kn->keynum)
1394 // if it is printable, output it as a single character
1397 u8_fromchar(keynum, tinystr, tinystrlength);
1401 // if it is not overridden and not printable, we don't know what to do with it
1402 return "<UNKNOWN KEYNUM>";
1407 Key_SetBinding (int keynum, int bindmap, const char *binding)
1412 if (keynum == -1 || keynum >= MAX_KEYS)
1414 if ((bindmap < 0) || (bindmap >= MAX_BINDMAPS))
1417 // free old bindings
1418 if (keybindings[bindmap][keynum]) {
1419 Z_Free (keybindings[bindmap][keynum]);
1420 keybindings[bindmap][keynum] = NULL;
1422 if(!binding[0]) // make "" binds be removed --blub
1424 // allocate memory for new binding
1425 l = strlen (binding);
1426 newbinding = (char *)Z_Malloc (l + 1);
1427 memcpy (newbinding, binding, l + 1);
1429 keybindings[bindmap][keynum] = newbinding;
1433 void Key_GetBindMap(int *fg, int *bg)
1441 qbool Key_SetBindMap(int fg, int bg)
1443 if(fg >= MAX_BINDMAPS)
1445 if(bg >= MAX_BINDMAPS)
1455 Key_In_Unbind_f(cmd_state_t *cmd)
1458 char *errchar = NULL;
1460 if (Cmd_Argc (cmd) != 3) {
1461 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
1465 m = strtol(Cmd_Argv(cmd, 1), &errchar, 0);
1466 if ((m < 0) || (m >= MAX_BINDMAPS) || (errchar && *errchar)) {
1467 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(cmd, 1));
1471 b = Key_StringToKeynum (Cmd_Argv(cmd, 2));
1473 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv(cmd, 2));
1477 if(!Key_SetBinding (b, m, ""))
1478 Con_Printf("Key_SetBinding failed for unknown reason\n");
1482 Key_In_Bind_f(cmd_state_t *cmd)
1485 char line[MAX_INPUTLINE];
1486 char *errchar = NULL;
1490 if (c != 3 && c != 4) {
1491 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
1495 m = strtol(Cmd_Argv(cmd, 1), &errchar, 0);
1496 if ((m < 0) || (m >= MAX_BINDMAPS) || (errchar && *errchar)) {
1497 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(cmd, 1));
1501 b = Key_StringToKeynum (Cmd_Argv(cmd, 2));
1502 if (b == -1 || b >= MAX_KEYS) {
1503 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv(cmd, 2));
1508 if (keybindings[m][b])
1509 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv(cmd, 2), keybindings[m][b]);
1511 Con_Printf("\"%s\" is not bound\n", Cmd_Argv(cmd, 2));
1514 // copy the rest of the command line
1515 line[0] = 0; // start out with a null string
1516 for (i = 3; i < c; i++) {
1517 strlcat (line, Cmd_Argv(cmd, i), sizeof (line));
1519 strlcat (line, " ", sizeof (line));
1522 if(!Key_SetBinding (b, m, line))
1523 Con_Printf("Key_SetBinding failed for unknown reason\n");
1527 Key_In_Bindmap_f(cmd_state_t *cmd)
1530 char *errchar = NULL;
1535 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
1539 m1 = strtol(Cmd_Argv(cmd, 1), &errchar, 0);
1540 if ((m1 < 0) || (m1 >= MAX_BINDMAPS) || (errchar && *errchar)) {
1541 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(cmd, 1));
1545 m2 = strtol(Cmd_Argv(cmd, 2), &errchar, 0);
1546 if ((m2 < 0) || (m2 >= MAX_BINDMAPS) || (errchar && *errchar)) {
1547 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(cmd, 2));
1556 Key_Unbind_f(cmd_state_t *cmd)
1560 if (Cmd_Argc (cmd) != 2) {
1561 Con_Print("unbind <key> : remove commands from a key\n");
1565 b = Key_StringToKeynum (Cmd_Argv(cmd, 1));
1567 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv(cmd, 1));
1571 if(!Key_SetBinding (b, 0, ""))
1572 Con_Printf("Key_SetBinding failed for unknown reason\n");
1576 Key_Unbindall_f(cmd_state_t *cmd)
1580 for (j = 0; j < MAX_BINDMAPS; j++)
1581 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1582 if (keybindings[j][i])
1583 Key_SetBinding (i, j, "");
1587 Key_PrintBindList(int j)
1589 char bindbuf[MAX_INPUTLINE];
1590 char tinystr[TINYSTR_LEN];
1594 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1596 p = keybindings[j][i];
1599 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\", false);
1601 Con_Printf("^2%s ^7= \"%s\"\n", Key_KeynumToString (i, tinystr, TINYSTR_LEN), bindbuf);
1603 Con_Printf("^3bindmap %d: ^2%s ^7= \"%s\"\n", j, Key_KeynumToString (i, tinystr, TINYSTR_LEN), bindbuf);
1609 Key_In_BindList_f(cmd_state_t *cmd)
1612 char *errchar = NULL;
1614 if(Cmd_Argc(cmd) >= 2)
1616 m = strtol(Cmd_Argv(cmd, 1), &errchar, 0);
1617 if ((m < 0) || (m >= MAX_BINDMAPS) || (errchar && *errchar)) {
1618 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(cmd, 1));
1621 Key_PrintBindList(m);
1625 for (m = 0; m < MAX_BINDMAPS; m++)
1626 Key_PrintBindList(m);
1631 Key_BindList_f(cmd_state_t *cmd)
1633 Key_PrintBindList(0);
1637 Key_Bind_f(cmd_state_t *cmd)
1640 char line[MAX_INPUTLINE];
1644 if (c != 2 && c != 3) {
1645 Con_Print("bind <key> [command] : attach a command to a key\n");
1648 b = Key_StringToKeynum (Cmd_Argv(cmd, 1));
1649 if (b == -1 || b >= MAX_KEYS) {
1650 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv(cmd, 1));
1655 if (keybindings[0][b])
1656 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv(cmd, 1), keybindings[0][b]);
1658 Con_Printf("\"%s\" is not bound\n", Cmd_Argv(cmd, 1));
1661 // copy the rest of the command line
1662 line[0] = 0; // start out with a null string
1663 for (i = 2; i < c; i++) {
1664 strlcat (line, Cmd_Argv(cmd, i), sizeof (line));
1666 strlcat (line, " ", sizeof (line));
1669 if(!Key_SetBinding (b, 0, line))
1670 Con_Printf("Key_SetBinding failed for unknown reason\n");
1675 Writes lines containing "bind key value"
1679 Key_WriteBindings (qfile_t *f)
1682 char bindbuf[MAX_INPUTLINE];
1683 char tinystr[TINYSTR_LEN];
1686 // Override default binds
1687 FS_Printf(f, "unbindall\n");
1689 for (j = 0; j < MAX_BINDMAPS; j++)
1691 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1693 p = keybindings[j][i];
1696 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\", false); // don't need to escape $ because cvars are not expanded inside bind
1698 FS_Printf(f, "bind %s \"%s\"\n", Key_KeynumToString (i, tinystr, TINYSTR_LEN), bindbuf);
1700 FS_Printf(f, "in_bind %d %s \"%s\"\n", j, Key_KeynumToString (i, tinystr, TINYSTR_LEN), bindbuf);
1711 key_linepos = Key_ClearEditLine(true);
1714 // register our functions
1716 Cmd_AddCommand(CF_CLIENT, "in_bind", Key_In_Bind_f, "binds a command to the specified key in the selected bindmap");
1717 Cmd_AddCommand(CF_CLIENT, "in_unbind", Key_In_Unbind_f, "removes command on the specified key in the selected bindmap");
1718 Cmd_AddCommand(CF_CLIENT, "in_bindlist", Key_In_BindList_f, "bindlist: displays bound keys for all bindmaps, or the given bindmap");
1719 Cmd_AddCommand(CF_CLIENT, "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");
1720 Cmd_AddCommand(CF_CLIENT, "in_releaseall", Key_ReleaseAll_f, "releases all currently pressed keys (debug command)");
1722 Cmd_AddCommand(CF_CLIENT, "bind", Key_Bind_f, "binds a command to the specified key in bindmap 0");
1723 Cmd_AddCommand(CF_CLIENT, "unbind", Key_Unbind_f, "removes a command on the specified key in bindmap 0");
1724 Cmd_AddCommand(CF_CLIENT, "bindlist", Key_BindList_f, "bindlist: displays bound keys for bindmap 0 bindmaps");
1725 Cmd_AddCommand(CF_CLIENT, "unbindall", Key_Unbindall_f, "removes all commands from all keys in all bindmaps (leaving only shift-escape and escape)");
1727 Cmd_AddCommand(CF_CLIENT, "history", Key_History_f, "prints the history of executed commands (history X prints the last X entries, history -c clears the whole history)");
1729 Cvar_RegisterVariable (&con_closeontoggleconsole);
1735 Key_History_Shutdown();
1738 const char *Key_GetBind (int key, int bindmap)
1741 if (key < 0 || key >= MAX_KEYS)
1743 if(bindmap >= MAX_BINDMAPS)
1747 bind = keybindings[bindmap][key];
1751 bind = keybindings[key_bmap][key];
1753 bind = keybindings[key_bmap2][key];
1758 void Key_FindKeysForCommand (const char *command, int *keys, int numkeys, int bindmap)
1764 for (j = 0;j < numkeys;j++)
1767 if(bindmap >= MAX_BINDMAPS)
1772 for (j = 0; j < MAX_KEYS; ++j)
1774 b = Key_GetBind(j, bindmap);
1777 if (!strcmp (b, command) )
1780 if (count == numkeys)
1788 Called by the system between frames for both key up and key down events
1789 Should NOT be called during an interrupt!
1792 static char tbl_keyascii[MAX_KEYS];
1793 static keydest_t tbl_keydest[MAX_KEYS];
1795 typedef struct eventqueueitem_s
1802 static int events_blocked = 0;
1803 static eventqueueitem_t eventqueue[32];
1804 static unsigned eventqueue_idx = 0;
1806 static void Key_EventQueue_Add(int key, int ascii, qbool down)
1808 if(eventqueue_idx < sizeof(eventqueue) / sizeof(*eventqueue))
1810 eventqueue[eventqueue_idx].key = key;
1811 eventqueue[eventqueue_idx].ascii = ascii;
1812 eventqueue[eventqueue_idx].down = down;
1817 void Key_EventQueue_Block(void)
1819 // block key events until call to Unblock
1820 events_blocked = true;
1823 void Key_EventQueue_Unblock(void)
1825 // unblocks key events again
1827 events_blocked = false;
1828 for(i = 0; i < eventqueue_idx; ++i)
1829 Key_Event(eventqueue[i].key, eventqueue[i].ascii, eventqueue[i].down);
1834 Key_Event (int key, int ascii, qbool down)
1836 cmd_state_t *cmd = cmd_local;
1839 keydest_t keydest = key_dest;
1842 if (key < 0 || key >= MAX_KEYS)
1847 Key_EventQueue_Add(key, ascii, down);
1852 bind = keybindings[key_bmap][key];
1854 bind = keybindings[key_bmap2][key];
1856 if (developer_insane.integer)
1857 Con_DPrintf("Key_Event(%i, '%c', %s) keydown %i bind \"%s\"\n", key, ascii ? ascii : '?', down ? "down" : "up", keydown[key], bind ? bind : "");
1859 if(key_consoleactive)
1860 keydest = key_console;
1864 // increment key repeat count each time a down is received so that things
1865 // which want to ignore key repeat can ignore it
1866 keydown[key] = min(keydown[key] + 1, 2);
1867 if(keydown[key] == 1) {
1868 tbl_keyascii[key] = ascii;
1869 tbl_keydest[key] = keydest;
1871 ascii = tbl_keyascii[key];
1872 keydest = tbl_keydest[key];
1877 // clear repeat count now that the key is released
1879 keydest = tbl_keydest[key];
1880 ascii = tbl_keyascii[key];
1883 if(keydest == key_void)
1886 // key_consoleactive is a flag not a key_dest because the console is a
1887 // high priority overlay ontop of the normal screen (designed as a safety
1888 // feature so that developers and users can rescue themselves from a bad
1891 // this also means that toggling the console on/off does not lose the old
1894 // specially handle escape (togglemenu) and shift-escape (toggleconsole)
1895 // engine bindings, these are not handled as normal binds so that the user
1896 // can recover from a completely empty bindmap
1897 if (key == K_ESCAPE)
1899 // ignore key repeats on escape
1900 if (keydown[key] > 1)
1903 // escape does these things:
1904 // key_consoleactive - close console
1905 // key_message - abort messagemode
1906 // key_menu - go to parent menu (or key_game)
1907 // key_game - open menu
1909 // in all modes shift-escape toggles console
1910 if (keydown[K_SHIFT])
1914 Con_ToggleConsole_f(cmd_local);
1915 tbl_keydest[key] = key_void; // esc release should go nowhere (especially not to key_menu or key_game)
1925 if(key_consoleactive & KEY_CONSOLEACTIVE_FORCED)
1927 key_consoleactive &= ~KEY_CONSOLEACTIVE_USER;
1933 Con_ToggleConsole_f(cmd_local);
1939 Key_Message (cmd, key, ascii); // that'll close the message input
1943 case key_menu_grabbed:
1945 MR_KeyEvent (key, ascii, down);
1950 // csqc has priority over toggle menu if it wants to (e.g. handling escape for UI stuff in-game.. :sick:)
1951 q = CL_VM_InputEvent(down ? 0 : 1, key, ascii);
1959 Con_Printf ("Key_Event: Bad key_dest\n");
1964 // send function keydowns to interpreter no matter what mode is (unless the menu has specifically grabbed the keyboard, for rebinding keys)
1965 // VorteX: Omnicide does bind F* keys
1966 if (keydest != key_menu_grabbed)
1967 if (key >= K_F1 && key <= K_F12 && gamemode != GAME_BLOODOMNICIDE)
1971 if(keydown[key] == 1 && down)
1973 // button commands add keynum as a parm
1974 // prepend to avoid delays from `wait` commands added by other sources
1976 Cbuf_InsertText(cmd, va(vabuf, sizeof(vabuf), "%s %i\n", bind, key));
1978 Cbuf_InsertText(cmd, bind);
1980 else if(bind[0] == '+' && !down && keydown[key] == 0)
1981 // append -bind to ensure it's after the +bind in case they arrive in the same frame
1982 Cbuf_AddText(cmd, va(vabuf, sizeof(vabuf), "-%s %i\n", bind + 1, key));
1987 // send input to console if it wants it
1988 if (keydest == key_console)
1992 // con_closeontoggleconsole enables toggleconsole keys to close the
1993 // console, as long as they are not the color prefix character
1994 // (special exemption for german keyboard layouts)
1995 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))
1997 Con_ToggleConsole_f(cmd_local);
2001 if (Sys_CheckParm ("-noconsole"))
2002 return; // only allow the key bind to turn off console
2004 Key_Console (cmd, key, ascii);
2008 // handle toggleconsole in menu too
2009 if (keydest == key_menu)
2011 if (down && con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && ascii != STRING_COLOR_TAG)
2013 Cbuf_InsertText(cmd, "toggleconsole\n"); // Deferred to next frame so we're not sending the text event to the console.
2014 tbl_keydest[key] = key_void; // key release should go nowhere (especially not to key_menu or key_game)
2019 // ignore binds while a video is played, let the video system handle the key event
2020 if (cl_videoplaying)
2022 if (gamemode == GAME_BLOODOMNICIDE) // menu controls key events
2024 MR_KeyEvent(key, ascii, down);
2030 CL_Video_KeyEvent (key, ascii, keydown[key] != 0);
2034 // anything else is a key press into the game, chat line, or menu
2039 Key_Message (cmd, key, ascii);
2042 case key_menu_grabbed:
2044 MR_KeyEvent (key, ascii, down);
2048 q = CL_VM_InputEvent(down ? 0 : 1, key, ascii);
2049 // ignore key repeats on binds and only send the bind if the event hasnt been already processed by csqc
2052 if(keydown[key] == 1 && down)
2054 // button commands add keynum as a parm
2055 // prepend to avoid delays from `wait` commands added by other sources
2057 Cbuf_InsertText(cmd, va(vabuf, sizeof(vabuf), "%s %i\n", bind, key));
2059 Cbuf_InsertText(cmd, bind);
2061 else if(bind[0] == '+' && !down && keydown[key] == 0)
2062 // append -bind to ensure it's after the +bind in case they arrive in the same frame
2063 Cbuf_AddText(cmd, va(vabuf, sizeof(vabuf), "-%s %i\n", bind + 1, key));
2067 Con_Printf ("Key_Event: Bad key_dest\n");
2071 // a helper to simulate release of ALL keys
2073 Key_ReleaseAll (void)
2076 // clear the event queue first
2078 // then send all down events (possibly into the event queue)
2079 for(key = 0; key < MAX_KEYS; ++key)
2081 Key_Event(key, 0, false);
2082 // now all keys are guaranteed down (once the event queue is unblocked)
2083 // and only future events count
2086 void Key_ReleaseAll_f(cmd_state_t *cmd)