]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - keys.c
lights now have an orientation (this isn't editable yet, and is infact not really...
[xonotic/darkplaces.git] / keys.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 #include "quakedef.h"
21
22 /*
23
24 key up events are sent even if in console mode
25
26 */
27
28
29 #define MAXCMDLINE 256
30 char key_lines[32][MAXCMDLINE];
31 int key_linepos;
32 int shift_down = false;
33 int key_insert; // insert key toggle (for editing)
34
35 int edit_line = 0;
36 int history_line = 0;
37
38 int key_consoleactive;
39 keydest_t key_dest;
40
41 char *keybindings[256];
42 qboolean consolekeys[256];      // if true, can't be rebound while in console
43 qboolean menubound[256];        // if true, can't be rebound while in menu
44 int keyshift[256];              // key to map to if shift held down in console
45 int key_repeats[256];   // if > 1, it is autorepeating
46 qboolean keydown[256];
47
48 typedef struct
49 {
50         char *name;
51         int keynum;
52 } keyname_t;
53
54 keyname_t keynames[] =
55 {
56         {"TAB", K_TAB},
57         {"ENTER", K_ENTER},
58         {"ESCAPE", K_ESCAPE},
59         {"SPACE", K_SPACE},
60         {"BACKSPACE", K_BACKSPACE},
61         {"UPARROW", K_UPARROW},
62         {"DOWNARROW", K_DOWNARROW},
63         {"LEFTARROW", K_LEFTARROW},
64         {"RIGHTARROW", K_RIGHTARROW},
65
66         {"ALT", K_ALT},
67         {"CTRL", K_CTRL},
68         {"SHIFT", K_SHIFT},
69
70         {"F1", K_F1},
71         {"F2", K_F2},
72         {"F3", K_F3},
73         {"F4", K_F4},
74         {"F5", K_F5},
75         {"F6", K_F6},
76         {"F7", K_F7},
77         {"F8", K_F8},
78         {"F9", K_F9},
79         {"F10", K_F10},
80         {"F11", K_F11},
81         {"F12", K_F12},
82
83         {"INS", K_INS},
84         {"DEL", K_DEL},
85         {"PGDN", K_PGDN},
86         {"PGUP", K_PGUP},
87         {"HOME", K_HOME},
88         {"END", K_END},
89
90         {"MOUSE1", K_MOUSE1},
91         {"MOUSE2", K_MOUSE2},
92         {"MOUSE3", K_MOUSE3},
93
94         {"JOY1", K_JOY1},
95         {"JOY2", K_JOY2},
96         {"JOY3", K_JOY3},
97         {"JOY4", K_JOY4},
98
99         {"AUX1", K_AUX1},
100         {"AUX2", K_AUX2},
101         {"AUX3", K_AUX3},
102         {"AUX4", K_AUX4},
103         {"AUX5", K_AUX5},
104         {"AUX6", K_AUX6},
105         {"AUX7", K_AUX7},
106         {"AUX8", K_AUX8},
107         {"AUX9", K_AUX9},
108         {"AUX10", K_AUX10},
109         {"AUX11", K_AUX11},
110         {"AUX12", K_AUX12},
111         {"AUX13", K_AUX13},
112         {"AUX14", K_AUX14},
113         {"AUX15", K_AUX15},
114         {"AUX16", K_AUX16},
115         {"AUX17", K_AUX17},
116         {"AUX18", K_AUX18},
117         {"AUX19", K_AUX19},
118         {"AUX20", K_AUX20},
119         {"AUX21", K_AUX21},
120         {"AUX22", K_AUX22},
121         {"AUX23", K_AUX23},
122         {"AUX24", K_AUX24},
123         {"AUX25", K_AUX25},
124         {"AUX26", K_AUX26},
125         {"AUX27", K_AUX27},
126         {"AUX28", K_AUX28},
127         {"AUX29", K_AUX29},
128         {"AUX30", K_AUX30},
129         {"AUX31", K_AUX31},
130         {"AUX32", K_AUX32},
131
132         {"KP_HOME",                     K_KP_HOME },
133         {"KP_UPARROW",          K_KP_UPARROW },
134         {"KP_PGUP",                     K_KP_PGUP },
135         {"KP_LEFTARROW",        K_KP_LEFTARROW },
136         {"KP_5",                        K_KP_5 },
137         {"KP_RIGHTARROW",       K_KP_RIGHTARROW },
138         {"KP_END",                      K_KP_END },
139         {"KP_DOWNARROW",        K_KP_DOWNARROW },
140         {"KP_PGDN",                     K_KP_PGDN },
141         {"KP_ENTER",            K_KP_ENTER },
142         {"KP_INS",                      K_KP_INS },
143         {"KP_DEL",                      K_KP_DEL },
144         {"KP_SLASH",            K_KP_SLASH },
145         {"KP_MINUS",            K_KP_MINUS },
146         {"KP_PLUS",                     K_KP_PLUS },
147
148         {"MWHEELUP", K_MWHEELUP },
149         {"MWHEELDOWN", K_MWHEELDOWN },
150
151         {"PAUSE", K_PAUSE},
152
153         {"SEMICOLON", ';'},     // because a raw semicolon seperates commands
154
155         {NULL,0}
156 };
157
158 /*
159 ==============================================================================
160
161                         LINE TYPING INTO THE CONSOLE
162
163 ==============================================================================
164 */
165
166
167 /*
168 ====================
169 Key_Console
170
171 Interactive line editing and console scrollback
172 ====================
173 */
174 void Key_Console (int key)
175 {
176         // LordHavoc: copied most of this from Q2 to improve keyboard handling
177         switch (key)
178         {
179         case K_KP_SLASH:
180                 key = '/';
181                 break;
182         case K_KP_MINUS:
183                 key = '-';
184                 break;
185         case K_KP_PLUS:
186                 key = '+';
187                 break;
188         case K_KP_HOME:
189                 key = '7';
190                 break;
191         case K_KP_UPARROW:
192                 key = '8';
193                 break;
194         case K_KP_PGUP:
195                 key = '9';
196                 break;
197         case K_KP_LEFTARROW:
198                 key = '4';
199                 break;
200         case K_KP_5:
201                 key = '5';
202                 break;
203         case K_KP_RIGHTARROW:
204                 key = '6';
205                 break;
206         case K_KP_END:
207                 key = '1';
208                 break;
209         case K_KP_DOWNARROW:
210                 key = '2';
211                 break;
212         case K_KP_PGDN:
213                 key = '3';
214                 break;
215         case K_KP_INS:
216                 key = '0';
217                 break;
218         case K_KP_DEL:
219                 key = '.';
220                 break;
221         }
222
223         // LordHavoc: FIXME: implement this sometime
224         #if 0
225         if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
226         {
227                 char *cbd;
228                 if ((cbd = Sys_GetClipboardData()) != 0)
229                 {
230                         int i;
231                         strtok(cbd, "\n\r\b");
232                         i = strlen(cbd);
233                         if (i + key_linepos >= MAXCMDLINE)
234                                 i= MAXCMDLINE - key_linepos;
235                         if (i > 0)
236                         {
237                                 cbd[i]=0;
238                                 strcat(key_lines[edit_line], cbd);
239                                 key_linepos += i;
240                         }
241                         free(cbd);
242                 }
243                 return;
244         }
245         #endif
246
247         if (key == 'l')
248         {
249                 if (keydown[K_CTRL])
250                 {
251                         Cbuf_AddText ("clear\n");
252                         return;
253                 }
254         }
255
256         if (key == K_ENTER || key == K_KP_ENTER)
257         {
258                 Cbuf_AddText (key_lines[edit_line]+1);  // skip the ]
259                 Cbuf_AddText ("\n");
260                 Con_Printf ("%s\n",key_lines[edit_line]);
261                 edit_line = (edit_line + 1) & 31;
262                 history_line = edit_line;
263                 key_lines[edit_line][0] = ']';
264                 key_lines[edit_line][1] = 0;    // EvilTypeGuy: null terminate
265                 key_linepos = 1;
266                 // force an update, because the command may take some time
267                 if (cls.state == ca_disconnected)
268                 {
269                         CL_UpdateScreen ();
270                         CL_UpdateScreen ();
271                 }
272                 return;
273         }
274
275         if (key == K_TAB)
276         {
277                 // Enhanced command completion
278                 // by EvilTypeGuy eviltypeguy@qeradiant.com
279                 // Thanks to Fett, Taniwha
280                 Con_CompleteCommandLine();
281                 return;
282         }
283
284         // Advanced Console Editing by Radix radix@planetquake.com
285         // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
286
287         // left arrow will just move left one without erasing, backspace will
288         // actually erase charcter
289         if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
290         {
291                 if (key_linepos > 1)
292                         key_linepos--;
293                 return;
294         }
295
296         // delete char before cursor
297         if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
298         {
299                 if (key_linepos > 1)
300                 {
301                         strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
302                         key_linepos--;
303                 }
304                 return;
305         }
306
307         // delete char on cursor
308         if (key == K_DEL || key == K_KP_DEL)
309         {
310                 if ((size_t)key_linepos < strlen(key_lines[edit_line]))
311                         strcpy(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1);
312                 return;
313         }
314
315
316         // if we're at the end, get one character from previous line,
317         // otherwise just go right one
318         if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
319         {
320                 if (strlen(key_lines[edit_line]) == (size_t)key_linepos)
321                 {
322                         if (strlen(key_lines[(edit_line + 31) & 31]) <= (size_t)key_linepos)
323                                 return; // no character to get
324
325                         key_lines[edit_line][key_linepos] = key_lines[(edit_line + 31) & 31][key_linepos];
326                         key_linepos++;
327                         key_lines[edit_line][key_linepos] = 0;
328                 }
329                 else
330                         key_linepos++;
331
332                 return;
333         }
334
335         if (key == K_INS || key == K_KP_INS) // toggle insert mode
336         {
337                 key_insert ^= 1;
338                 return;
339         }
340
341         // End Advanced Console Editing
342
343         if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
344         {
345                 do
346                 {
347                         history_line = (history_line - 1) & 31;
348                 } while (history_line != edit_line
349                                 && !key_lines[history_line][1]);
350                 if (history_line == edit_line)
351                         history_line = (edit_line+1)&31;
352                 strcpy(key_lines[edit_line], key_lines[history_line]);
353                 key_linepos = strlen(key_lines[edit_line]);
354                 return;
355         }
356
357         if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
358         {
359                 if (history_line == edit_line) return;
360                 do
361                 {
362                         history_line = (history_line + 1) & 31;
363                 }
364                 while (history_line != edit_line
365                         && !key_lines[history_line][1]);
366                 if (history_line == edit_line)
367                 {
368                         key_lines[edit_line][0] = ']';
369                         key_linepos = 1;
370                 }
371                 else
372                 {
373                         strcpy(key_lines[edit_line], key_lines[history_line]);
374                         key_linepos = strlen(key_lines[edit_line]);
375                 }
376                 return;
377         }
378
379         if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
380         {
381                 con_backscroll += ((int) scr_conlines >> 4);
382                 if (con_backscroll > con_totallines - (vid.conheight>>3) - 1)
383                         con_backscroll = con_totallines - (vid.conheight>>3) - 1;
384                 return;
385         }
386
387         if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
388         {
389                 con_backscroll -= ((int) scr_conlines >> 4);
390                 if (con_backscroll < 0)
391                         con_backscroll = 0;
392                 return;
393         }
394
395         if (key == K_HOME || key == K_KP_HOME)
396         {
397                 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
398                 return;
399         }
400
401         if (key == K_END || key == K_KP_END)
402         {
403                 con_backscroll = 0;
404                 return;
405         }
406
407         // non printable
408         if (key < 32 || key > 127)
409                 return;
410
411         if (key_linepos < MAXCMDLINE-1)
412         {
413                 int i;
414
415                 if (key_insert) // check insert mode
416                 {
417                         // can't do strcpy to move string to right
418                         i = strlen(key_lines[edit_line]) - 1;
419
420                         if (i == 254)
421                                 i--;
422
423                         for (; i >= key_linepos; i--)
424                                 key_lines[edit_line][i + 1] = key_lines[edit_line][i];
425                 }
426
427                 // only null terminate if at the end
428                 i = key_lines[edit_line][key_linepos];
429                 key_lines[edit_line][key_linepos] = key;
430                 key_linepos++;
431
432                 if (!i)
433                         key_lines[edit_line][key_linepos] = 0;
434         }
435 }
436
437 //============================================================================
438
439 // LordHavoc: increased messagemode length (was 32)
440 qboolean chat_team = false;
441 char chat_buffer[256];
442 int chat_bufferlen = 0;
443
444 void Key_Message (int key)
445 {
446         if (key == K_ENTER || key == K_KP_ENTER)
447         {
448                 if (chat_team)
449                         Cbuf_AddText ("say_team \"");
450                 else
451                         Cbuf_AddText ("say \"");
452                 Cbuf_AddText(chat_buffer);
453                 Cbuf_AddText("\"\n");
454
455                 key_dest = key_game;
456                 chat_bufferlen = 0;
457                 chat_buffer[0] = 0;
458                 return;
459         }
460
461         if (key == K_ESCAPE)
462         {
463                 key_dest = key_game;
464                 chat_bufferlen = 0;
465                 chat_buffer[0] = 0;
466                 return;
467         }
468
469         // non printable
470         if (key < 32 || key > 127)
471                 return;
472
473         if (key == K_BACKSPACE)
474         {
475                 if (chat_bufferlen)
476                 {
477                         chat_bufferlen--;
478                         chat_buffer[chat_bufferlen] = 0;
479                 }
480                 return;
481         }
482
483         if (chat_bufferlen == sizeof(chat_buffer) - 1)
484                 return; // all full
485
486         chat_buffer[chat_bufferlen++] = key;
487         chat_buffer[chat_bufferlen] = 0;
488 }
489
490 //============================================================================
491
492
493 /*
494 ===================
495 Key_StringToKeynum
496
497 Returns a key number to be used to index keybindings[] by looking at
498 the given string.  Single ascii characters return themselves, while
499 the K_* names are matched up.
500 ===================
501 */
502 int Key_StringToKeynum (const char *str)
503 {
504         keyname_t *kn;
505
506         if (!str || !str[0])
507                 return -1;
508         if (!str[1])
509                 return str[0];
510
511         for (kn=keynames ; kn->name ; kn++)
512                 if (!Q_strcasecmp(str,kn->name))
513                         return kn->keynum;
514         return -1;
515 }
516
517 /*
518 ===================
519 Key_KeynumToString
520
521 Returns a string (either a single ascii char, or a K_* name) for the
522 given keynum.
523 FIXME: handle quote special (general escape sequence?)
524 ===================
525 */
526 char *Key_KeynumToString (int keynum)
527 {
528         keyname_t *kn;
529         static char tinystr[2];
530
531         if (keynum == -1)
532                 return "<KEY NOT FOUND>";
533         if (keynum > 32 && keynum < 127)
534         {       // printable ascii
535                 tinystr[0] = keynum;
536                 tinystr[1] = 0;
537                 return tinystr;
538         }
539
540         for (kn=keynames ; kn->name ; kn++)
541                 if (keynum == kn->keynum)
542                         return kn->name;
543
544         return "<UNKNOWN KEYNUM>";
545 }
546
547
548 /*
549 ===================
550 Key_SetBinding
551 ===================
552 */
553 void Key_SetBinding (int keynum, char *binding)
554 {
555         char *new;
556         int l;
557
558         if (keynum < 0 || keynum >= 256)
559                 return;
560
561 // free old bindings
562         if (keybindings[keynum])
563         {
564                 Z_Free (keybindings[keynum]);
565                 keybindings[keynum] = NULL;
566         }
567
568 // allocate memory for new binding
569         l = strlen (binding);
570         new = Z_Malloc (l+1);
571         strcpy (new, binding);
572         new[l] = 0;
573         keybindings[keynum] = new;
574 }
575
576 /*
577 ===================
578 Key_Unbind_f
579 ===================
580 */
581 void Key_Unbind_f (void)
582 {
583         int b;
584
585         if (Cmd_Argc() != 2)
586         {
587                 Con_Printf ("unbind <key> : remove commands from a key\n");
588                 return;
589         }
590
591         b = Key_StringToKeynum (Cmd_Argv(1));
592         if (b == -1)
593         {
594                 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
595                 return;
596         }
597
598         Key_SetBinding (b, "");
599 }
600
601 void Key_Unbindall_f (void)
602 {
603         int i;
604
605         for (i = 0;i < 256;i++)
606                 if (keybindings[i])
607                         Key_SetBinding (i, "");
608 }
609
610
611 /*
612 ===================
613 Key_Bind_f
614 ===================
615 */
616 void Key_Bind_f (void)
617 {
618         int i, c, b;
619         char cmd[1024];
620
621         c = Cmd_Argc();
622
623         if (c != 2 && c != 3)
624         {
625                 Con_Printf ("bind <key> [command] : attach a command to a key\n");
626                 return;
627         }
628         b = Key_StringToKeynum (Cmd_Argv(1));
629         if (b == -1)
630         {
631                 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
632                 return;
633         }
634
635         if (c == 2)
636         {
637                 if (keybindings[b])
638                         Con_Printf ("\"%s\" = \"%s\"\n", Cmd_Argv(1), keybindings[b] );
639                 else
640                         Con_Printf ("\"%s\" is not bound\n", Cmd_Argv(1) );
641                 return;
642         }
643
644 // copy the rest of the command line
645         // start out with a null string
646         cmd[0] = 0;
647         for (i=2 ; i< c ; i++)
648         {
649                 if (i > 2)
650                         strcat (cmd, " ");
651                 strcat (cmd, Cmd_Argv(i));
652         }
653
654         Key_SetBinding (b, cmd);
655 }
656
657 /*
658 ============
659 Key_WriteBindings
660
661 Writes lines containing "bind key value"
662 ============
663 */
664 void Key_WriteBindings (QFile *f)
665 {
666         int i;
667
668         for (i = 0;i < 256;i++)
669                 if (keybindings[i] && *keybindings[i])
670                         Qprintf (f, "bind \"%s\" \"%s\"\n", Key_KeynumToString(i), keybindings[i]);
671 }
672
673
674 /*
675 ============
676 Key_Bindlist_f
677
678 ============
679 */
680 void Key_Bindlist_f (void)
681 {
682         int i;
683
684         for (i = 0;i < 256;i++)
685                 if (keybindings[i] && keybindings[i][0])
686                         Con_Printf ("%s \"%s\"\n", Key_KeynumToString(i), keybindings[i]);
687 }
688
689
690 /*
691 ===================
692 Key_Init
693 ===================
694 */
695 void Key_Init (void)
696 {
697         int i;
698
699         // LordHavoc: clear keybindings array so bounds checking won't freak
700         for (i = 0;i < 256;i++)
701                 keybindings[i] = NULL;
702
703         for (i = 0;i < 32;i++)
704         {
705                 key_lines[i][0] = ']';
706                 key_lines[i][1] = 0;
707         }
708         key_linepos = 1;
709
710 //
711 // init ascii characters in console mode
712 //
713         for (i = 32;i < 128;i++)
714                 consolekeys[i] = true;
715         consolekeys[K_ENTER] = true;consolekeys[K_KP_ENTER] = true;
716         consolekeys[K_TAB] = true;
717         consolekeys[K_LEFTARROW] = true;consolekeys[K_KP_LEFTARROW] = true;
718         consolekeys[K_RIGHTARROW] = true;consolekeys[K_KP_RIGHTARROW] = true;
719         consolekeys[K_UPARROW] = true;consolekeys[K_KP_UPARROW] = true;
720         consolekeys[K_DOWNARROW] = true;consolekeys[K_KP_DOWNARROW] = true;
721         consolekeys[K_BACKSPACE] = true;
722         consolekeys[K_HOME] = true;consolekeys[K_KP_HOME] = true;
723         consolekeys[K_END] = true;consolekeys[K_KP_END] = true;
724         consolekeys[K_PGUP] = true;consolekeys[K_KP_PGUP] = true;
725         consolekeys[K_PGDN] = true;consolekeys[K_KP_PGDN] = true;
726         consolekeys[K_SHIFT] = true;
727         consolekeys[K_INS] = true;consolekeys[K_KP_INS] = true;
728         consolekeys[K_DEL] = true;consolekeys[K_KP_DEL] = true;
729         consolekeys[K_KP_SLASH] = true;
730         consolekeys[K_KP_PLUS] = true;
731         consolekeys[K_KP_MINUS] = true;
732         consolekeys[K_KP_5] = true;
733         consolekeys[K_MWHEELUP] = true;
734         consolekeys[K_MWHEELDOWN] = true;
735
736         consolekeys['`'] = false;
737         consolekeys['~'] = false;
738
739         for (i = 0;i < 256;i++)
740                 keyshift[i] = i;
741         for (i = 'a';i <= 'z';i++)
742                 keyshift[i] = i - 'a' + 'A';
743         keyshift['1'] = '!';
744         keyshift['2'] = '@';
745         keyshift['3'] = '#';
746         keyshift['4'] = '$';
747         keyshift['5'] = '%';
748         keyshift['6'] = '^';
749         keyshift['7'] = '&';
750         keyshift['8'] = '*';
751         keyshift['9'] = '(';
752         keyshift['0'] = ')';
753         keyshift['-'] = '_';
754         keyshift['='] = '+';
755         keyshift[','] = '<';
756         keyshift['.'] = '>';
757         keyshift['/'] = '?';
758         keyshift[';'] = ':';
759         keyshift['\''] = '"';
760         keyshift['['] = '{';
761         keyshift[']'] = '}';
762         keyshift['`'] = '~';
763         keyshift['\\'] = '|';
764
765         menubound[K_ESCAPE] = true;
766         for (i=0 ; i<12 ; i++)
767                 menubound[K_F1+i] = true;
768
769 //
770 // register our functions
771 //
772         Cmd_AddCommand ("bind",Key_Bind_f);
773         Cmd_AddCommand ("unbind",Key_Unbind_f);
774         Cmd_AddCommand ("unbindall",Key_Unbindall_f);
775         Cmd_AddCommand ("bindlist",Key_Bindlist_f);
776 }
777
778 /*
779 ===================
780 Key_Event
781
782 Called by the system between frames for both key up and key down events
783 Should NOT be called during an interrupt!
784 ===================
785 */
786 void Key_Event (int key, qboolean down)
787 {
788         char    *kb;
789         char    cmd[1024];
790
791         keydown[key] = down;
792
793         // update auto-repeat status
794         if (down)
795         {
796                 key_repeats[key]++;
797                 if (key != K_BACKSPACE
798                  && key != K_PAUSE
799                  && key != K_PGUP
800                  && key != K_KP_PGUP
801                  && key != K_PGDN
802                  && key != K_KP_PGDN
803                  && key_repeats[key] > 1)
804                         return; // ignore most autorepeats
805
806                 if (key >= 200 && !keybindings[key])
807                         Con_Printf ("%s is unbound, hit F4 to set.\n", Key_KeynumToString (key));
808         }
809         else
810                 key_repeats[key] = 0;
811
812         if (key == K_SHIFT)
813                 shift_down = down;
814
815 //
816 // handle escape specialy, so the user can never unbind it
817 //
818         if (key == K_ESCAPE)
819         {
820                 if (!down)
821                         return;
822                 switch (key_dest)
823                 {
824                 case key_message:
825                         Key_Message (key);
826                         break;
827                 case key_menu:
828                         M_Keydown (key);
829                         break;
830                 case key_game:
831                         M_ToggleMenu_f ();
832                         break;
833                 default:
834                         Sys_Error ("Bad key_dest");
835                 }
836                 return;
837         }
838
839         // console key is hardcoded, so the user can never unbind it
840         if (key == '`' || key == '~')
841         {
842                 if (down)
843                         Con_ToggleConsole_f ();
844                 return;
845         }
846
847         if (down)
848         {
849                 kb = keybindings[key];
850                 if (kb && !strncmp(kb, "toggleconsole", strlen("toggleconsole")))
851                 {
852                         Cbuf_AddText (kb);
853                         Cbuf_AddText ("\n");
854                         return;
855                 }
856         }
857
858         if (key_consoleactive && consolekeys[key])
859         {
860                 // console only wants key down events
861                 if (!down)
862                         return;
863
864                 // FIXME: this does not support non-QWERTY keyboards
865                 if (shift_down)
866                         key = keyshift[key];
867
868                 Key_Console (key);
869         }
870         else
871         {
872                 //
873                 // key up events only generate commands if the game key binding is
874                 // a button command (leading + sign).  These will occur even in console mode,
875                 // to keep the character from continuing an action started before a console
876                 // switch.  Button commands include the keynum as a parameter, so multiple
877                 // downs can be matched with ups
878                 //
879                 if (!down)
880                 {
881                         kb = keybindings[key];
882                         if (kb && kb[0] == '+')
883                         {
884                                 sprintf (cmd, "-%s %i\n", kb+1, key);
885                                 Cbuf_AddText (cmd);
886                         }
887                         if (keyshift[key] != key)
888                         {
889                                 kb = keybindings[keyshift[key]];
890                                 if (kb && kb[0] == '+')
891                                 {
892                                         sprintf (cmd, "-%s %i\n", kb+1, key);
893                                         Cbuf_AddText (cmd);
894                                 }
895                         }
896                         return;
897                 }
898
899                 //
900                 // during demo playback, most keys bring up the main menu
901                 //
902                 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game)
903                         key = K_ESCAPE;
904
905                 //
906                 // if not a consolekey, send to the interpreter no matter what mode is
907                 //
908                 if ((key_consoleactive && !consolekeys[key])
909                  || (key_dest == key_menu && menubound[key])
910                  || key_dest == key_game)
911                 {
912                         kb = keybindings[key];
913                         if (kb)
914                         {
915                                 if (kb[0] == '+')
916                                 {
917                                         // button commands add keynum as a parm
918                                         sprintf (cmd, "%s %i\n", kb, key);
919                                         Cbuf_AddText (cmd);
920                                 }
921                                 else
922                                 {
923                                         Cbuf_AddText (kb);
924                                         Cbuf_AddText ("\n");
925                                 }
926                         }
927                         return;
928                 }
929
930                 if (!down)
931                         return;         // other systems only care about key down events
932
933                 // FIXME: this does not support non-QWERTY keyboards
934                 if (shift_down)
935                         key = keyshift[key];
936
937                 switch (key_dest)
938                 {
939                 case key_message:
940                         Key_Message (key);
941                         break;
942                 case key_menu:
943                         M_Keydown (key);
944                         break;
945
946                 case key_game:
947                 //case key_console:
948                         Key_Console (key);
949                         break;
950                 default:
951                         Sys_Error ("Bad key_dest");
952                 }
953         }
954 }
955
956
957 /*
958 ===================
959 Key_ClearStates
960 ===================
961 */
962 void Key_ClearStates (void)
963 {
964         int i;
965
966         for (i = 0;i < 256;i++)
967         {
968                 keydown[i] = false;
969                 key_repeats[i] = 0;
970         }
971 }
972