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