fix a thinko in big endian support (forgot to use defined() in a #elif)
[divverent/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         {NULL,0}
166 };
167
168 /*
169 ==============================================================================
170
171                         LINE TYPING INTO THE CONSOLE
172
173 ==============================================================================
174 */
175
176
177 /*
178 ====================
179 Key_Console
180
181 Interactive line editing and console scrollback
182 ====================
183 */
184 void Key_Console (int key, char ascii)
185 {
186         // LordHavoc: copied most of this from Q2 to improve keyboard handling
187         switch (key)
188         {
189         case K_KP_SLASH:
190                 key = '/';
191                 break;
192         case K_KP_MINUS:
193                 key = '-';
194                 break;
195         case K_KP_PLUS:
196                 key = '+';
197                 break;
198         case K_KP_HOME:
199                 key = '7';
200                 break;
201         case K_KP_UPARROW:
202                 key = '8';
203                 break;
204         case K_KP_PGUP:
205                 key = '9';
206                 break;
207         case K_KP_LEFTARROW:
208                 key = '4';
209                 break;
210         case K_KP_5:
211                 key = '5';
212                 break;
213         case K_KP_RIGHTARROW:
214                 key = '6';
215                 break;
216         case K_KP_END:
217                 key = '1';
218                 break;
219         case K_KP_DOWNARROW:
220                 key = '2';
221                 break;
222         case K_KP_PGDN:
223                 key = '3';
224                 break;
225         case K_KP_INS:
226                 key = '0';
227                 break;
228         case K_KP_DEL:
229                 key = '.';
230                 break;
231         }
232
233         // LordHavoc: FIXME: implement this sometime
234         #if 0
235         if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
236         {
237                 char *cbd;
238                 if ((cbd = Sys_GetClipboardData()) != 0)
239                 {
240                         int i;
241                         strtok(cbd, "\n\r\b");
242                         i = strlen(cbd);
243                         if (i + key_linepos >= MAXCMDLINE)
244                                 i= MAXCMDLINE - key_linepos;
245                         if (i > 0)
246                         {
247                                 cbd[i]=0;
248                                 strcat(key_lines[edit_line], cbd);
249                                 key_linepos += i;
250                         }
251                         free(cbd);
252                 }
253                 return;
254         }
255         #endif
256
257         if (key == 'l')
258         {
259                 if (keydown[K_CTRL])
260                 {
261                         Cbuf_AddText ("clear\n");
262                         return;
263                 }
264         }
265
266         if (key == K_ENTER || key == K_KP_ENTER)
267         {
268                 Cbuf_AddText (key_lines[edit_line]+1);  // skip the ]
269                 Cbuf_AddText ("\n");
270                 Con_Printf ("%s\n",key_lines[edit_line]);
271                 edit_line = (edit_line + 1) & 31;
272                 history_line = edit_line;
273                 key_lines[edit_line][0] = ']';
274                 key_lines[edit_line][1] = 0;    // EvilTypeGuy: null terminate
275                 key_linepos = 1;
276                 // force an update, because the command may take some time
277                 if (cls.state == ca_disconnected)
278                 {
279                         CL_UpdateScreen ();
280                         CL_UpdateScreen ();
281                 }
282                 return;
283         }
284
285         if (key == K_TAB)
286         {
287                 // Enhanced command completion
288                 // by EvilTypeGuy eviltypeguy@qeradiant.com
289                 // Thanks to Fett, Taniwha
290                 Con_CompleteCommandLine();
291                 return;
292         }
293
294         // Advanced Console Editing by Radix radix@planetquake.com
295         // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
296
297         // left arrow will just move left one without erasing, backspace will
298         // actually erase charcter
299         if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
300         {
301                 if (key_linepos > 1)
302                         key_linepos--;
303                 return;
304         }
305
306         // delete char before cursor
307         if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
308         {
309                 if (key_linepos > 1)
310                 {
311                         strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
312                         key_linepos--;
313                 }
314                 return;
315         }
316
317         // delete char on cursor
318         if (key == K_DEL || key == K_KP_DEL)
319         {
320                 if ((size_t)key_linepos < strlen(key_lines[edit_line]))
321                         strcpy(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1);
322                 return;
323         }
324
325
326         // if we're at the end, get one character from previous line,
327         // otherwise just go right one
328         if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
329         {
330                 if (strlen(key_lines[edit_line]) == (size_t)key_linepos)
331                 {
332                         if (strlen(key_lines[(edit_line + 31) & 31]) <= (size_t)key_linepos)
333                                 return; // no character to get
334
335                         key_lines[edit_line][key_linepos] = key_lines[(edit_line + 31) & 31][key_linepos];
336                         key_linepos++;
337                         key_lines[edit_line][key_linepos] = 0;
338                 }
339                 else
340                         key_linepos++;
341
342                 return;
343         }
344
345         if (key == K_INS || key == K_KP_INS) // toggle insert mode
346         {
347                 key_insert ^= 1;
348                 return;
349         }
350
351         // End Advanced Console Editing
352
353         if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
354         {
355                 do
356                 {
357                         history_line = (history_line - 1) & 31;
358                 } while (history_line != edit_line
359                                 && !key_lines[history_line][1]);
360                 if (history_line == edit_line)
361                         history_line = (edit_line+1)&31;
362                 strcpy(key_lines[edit_line], key_lines[history_line]);
363                 key_linepos = strlen(key_lines[edit_line]);
364                 return;
365         }
366
367         if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
368         {
369                 if (history_line == edit_line) return;
370                 do
371                 {
372                         history_line = (history_line + 1) & 31;
373                 }
374                 while (history_line != edit_line
375                         && !key_lines[history_line][1]);
376                 if (history_line == edit_line)
377                 {
378                         key_lines[edit_line][0] = ']';
379                         key_linepos = 1;
380                 }
381                 else
382                 {
383                         strcpy(key_lines[edit_line], key_lines[history_line]);
384                         key_linepos = strlen(key_lines[edit_line]);
385                 }
386                 return;
387         }
388
389         if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
390         {
391                 con_backscroll += ((int) scr_conlines >> 4);
392                 if (con_backscroll > con_totallines - (vid.conheight>>3) - 1)
393                         con_backscroll = con_totallines - (vid.conheight>>3) - 1;
394                 return;
395         }
396
397         if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
398         {
399                 con_backscroll -= ((int) scr_conlines >> 4);
400                 if (con_backscroll < 0)
401                         con_backscroll = 0;
402                 return;
403         }
404
405         if (key == K_HOME || key == K_KP_HOME)
406         {
407                 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
408                 return;
409         }
410
411         if (key == K_END || key == K_KP_END)
412         {
413                 con_backscroll = 0;
414                 return;
415         }
416
417         // non printable
418         if (ascii < 32 || ascii > 126)
419                 return;
420
421         if (key_linepos < MAXCMDLINE-1)
422         {
423                 int i;
424
425                 if (key_insert) // check insert mode
426                 {
427                         // can't do strcpy to move string to right
428                         i = strlen(key_lines[edit_line]) - 1;
429
430                         if (i == 254)
431                                 i--;
432
433                         for (; i >= key_linepos; i--)
434                                 key_lines[edit_line][i + 1] = key_lines[edit_line][i];
435                 }
436
437                 // only null terminate if at the end
438                 i = key_lines[edit_line][key_linepos];
439                 key_lines[edit_line][key_linepos] = ascii;
440                 key_linepos++;
441
442                 if (!i)
443                         key_lines[edit_line][key_linepos] = 0;
444         }
445 }
446
447 //============================================================================
448
449 // LordHavoc: increased messagemode length (was 32)
450 qboolean chat_team = false;
451 char chat_buffer[256];
452 int chat_bufferlen = 0;
453
454 void Key_Message (int key, char ascii)
455 {
456         if (key == K_ENTER || key == K_KP_ENTER)
457         {
458                 if (chat_team)
459                         Cbuf_AddText ("say_team \"");
460                 else
461                         Cbuf_AddText ("say \"");
462                 Cbuf_AddText(chat_buffer);
463                 Cbuf_AddText("\"\n");
464
465                 key_dest = key_game;
466                 chat_bufferlen = 0;
467                 chat_buffer[0] = 0;
468                 return;
469         }
470
471         if (key == K_ESCAPE)
472         {
473                 key_dest = key_game;
474                 chat_bufferlen = 0;
475                 chat_buffer[0] = 0;
476                 return;
477         }
478
479         if (key == K_BACKSPACE)
480         {
481                 if (chat_bufferlen)
482                 {
483                         chat_bufferlen--;
484                         chat_buffer[chat_bufferlen] = 0;
485                 }
486                 return;
487         }
488
489         // non printable
490         if (ascii < 32 || ascii > 126)
491                 return;
492
493         if (chat_bufferlen == sizeof(chat_buffer) - 1)
494                 return; // all full
495
496         chat_buffer[chat_bufferlen++] = ascii;
497         chat_buffer[chat_bufferlen] = 0;
498 }
499
500 //============================================================================
501
502
503 /*
504 ===================
505 Key_StringToKeynum
506
507 Returns a key number to be used to index keybindings[] by looking at
508 the given string.  Single ascii characters return themselves, while
509 the K_* names are matched up.
510 ===================
511 */
512 int Key_StringToKeynum (const char *str)
513 {
514         keyname_t *kn;
515
516         if (!str || !str[0])
517                 return -1;
518         if (!str[1])
519                 return tolower(str[0]);
520
521         for (kn=keynames ; kn->name ; kn++)
522                 if (!strcasecmp(str,kn->name))
523                         return kn->keynum;
524         return -1;
525 }
526
527 /*
528 ===================
529 Key_KeynumToString
530
531 Returns a string (either a single ascii char, or a K_* name) for the
532 given keynum.
533 FIXME: handle quote special (general escape sequence?)
534 ===================
535 */
536 char *Key_KeynumToString (int keynum)
537 {
538         keyname_t *kn;
539         static char tinystr[2];
540
541         if (keynum == -1)
542                 return "<KEY NOT FOUND>";
543         if (keynum > 32 && keynum < 127)
544         {       // printable ascii
545                 tinystr[0] = keynum;
546                 tinystr[1] = 0;
547                 return tinystr;
548         }
549
550         for (kn=keynames ; kn->name ; kn++)
551                 if (keynum == kn->keynum)
552                         return kn->name;
553
554         return "<UNKNOWN KEYNUM>";
555 }
556
557
558 /*
559 ===================
560 Key_SetBinding
561 ===================
562 */
563 void Key_SetBinding (int keynum, char *binding)
564 {
565         char *new;
566         int l;
567
568         if (keynum < 0 || keynum >= 256)
569                 return;
570
571 // free old bindings
572         if (keybindings[keynum])
573         {
574                 Z_Free (keybindings[keynum]);
575                 keybindings[keynum] = NULL;
576         }
577
578 // allocate memory for new binding
579         l = strlen (binding);
580         new = Z_Malloc (l+1);
581         strcpy (new, binding);
582         new[l] = 0;
583         keybindings[keynum] = new;
584 }
585
586 /*
587 ===================
588 Key_Unbind_f
589 ===================
590 */
591 void Key_Unbind_f (void)
592 {
593         int b;
594
595         if (Cmd_Argc() != 2)
596         {
597                 Con_Printf ("unbind <key> : remove commands from a key\n");
598                 return;
599         }
600
601         b = Key_StringToKeynum (Cmd_Argv(1));
602         if (b == -1)
603         {
604                 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
605                 return;
606         }
607
608         Key_SetBinding (b, "");
609 }
610
611 void Key_Unbindall_f (void)
612 {
613         int i;
614
615         for (i = 0;i < 256;i++)
616                 if (keybindings[i])
617                         Key_SetBinding (i, "");
618 }
619
620
621 /*
622 ===================
623 Key_Bind_f
624 ===================
625 */
626 void Key_Bind_f (void)
627 {
628         int i, c, b;
629         char cmd[1024];
630
631         c = Cmd_Argc();
632
633         if (c != 2 && c != 3)
634         {
635                 Con_Printf ("bind <key> [command] : attach a command to a key\n");
636                 return;
637         }
638         b = Key_StringToKeynum (Cmd_Argv(1));
639         if (b == -1)
640         {
641                 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
642                 return;
643         }
644
645         if (c == 2)
646         {
647                 if (keybindings[b])
648                         Con_Printf ("\"%s\" = \"%s\"\n", Cmd_Argv(1), keybindings[b] );
649                 else
650                         Con_Printf ("\"%s\" is not bound\n", Cmd_Argv(1) );
651                 return;
652         }
653
654 // copy the rest of the command line
655         // start out with a null string
656         cmd[0] = 0;
657         for (i=2 ; i< c ; i++)
658         {
659                 if (i > 2)
660                         strcat (cmd, " ");
661                 strcat (cmd, Cmd_Argv(i));
662         }
663
664         Key_SetBinding (b, cmd);
665 }
666
667 /*
668 ============
669 Key_WriteBindings
670
671 Writes lines containing "bind key value"
672 ============
673 */
674 void Key_WriteBindings (qfile_t *f)
675 {
676         int i;
677
678         for (i = 0;i < 256;i++)
679                 if (keybindings[i] && *keybindings[i])
680                         FS_Printf (f, "bind \"%s\" \"%s\"\n", Key_KeynumToString(i), keybindings[i]);
681 }
682
683
684 /*
685 ============
686 Key_Bindlist_f
687
688 ============
689 */
690 void Key_Bindlist_f (void)
691 {
692         int i;
693
694         for (i = 0;i < 256;i++)
695                 if (keybindings[i] && keybindings[i][0])
696                         Con_Printf ("%s \"%s\"\n", Key_KeynumToString(i), keybindings[i]);
697 }
698
699
700 /*
701 ===================
702 Key_Init
703 ===================
704 */
705 void Key_Init (void)
706 {
707         int i;
708
709         // LordHavoc: clear keybindings array so bounds checking won't freak
710         for (i = 0;i < 256;i++)
711                 keybindings[i] = NULL;
712
713         for (i = 0;i < 32;i++)
714         {
715                 key_lines[i][0] = ']';
716                 key_lines[i][1] = 0;
717         }
718         key_linepos = 1;
719
720 //
721 // init ascii characters in console mode
722 //
723         for (i = 32;i < 128;i++)
724                 consolekeys[i] = true;
725         consolekeys[K_ENTER] = true;consolekeys[K_KP_ENTER] = true;
726         consolekeys[K_TAB] = true;
727         consolekeys[K_LEFTARROW] = true;consolekeys[K_KP_LEFTARROW] = true;
728         consolekeys[K_RIGHTARROW] = true;consolekeys[K_KP_RIGHTARROW] = true;
729         consolekeys[K_UPARROW] = true;consolekeys[K_KP_UPARROW] = true;
730         consolekeys[K_DOWNARROW] = true;consolekeys[K_KP_DOWNARROW] = true;
731         consolekeys[K_BACKSPACE] = true;
732         consolekeys[K_HOME] = true;consolekeys[K_KP_HOME] = true;
733         consolekeys[K_END] = true;consolekeys[K_KP_END] = true;
734         consolekeys[K_PGUP] = true;consolekeys[K_KP_PGUP] = true;
735         consolekeys[K_PGDN] = true;consolekeys[K_KP_PGDN] = true;
736         consolekeys[K_SHIFT] = true;
737         consolekeys[K_INS] = true;consolekeys[K_KP_INS] = true;
738         consolekeys[K_DEL] = true;consolekeys[K_KP_DEL] = true;
739         consolekeys[K_KP_SLASH] = true;
740         consolekeys[K_KP_PLUS] = true;
741         consolekeys[K_KP_MINUS] = true;
742         consolekeys[K_KP_5] = true;
743         consolekeys[K_MWHEELUP] = true;
744         consolekeys[K_MWHEELDOWN] = true;
745
746         consolekeys['`'] = false;
747         consolekeys['~'] = false;
748
749         menubound[K_ESCAPE] = true;
750         for (i=0 ; i<12 ; i++)
751                 menubound[K_F1+i] = true;
752
753 //
754 // register our functions
755 //
756         Cmd_AddCommand ("bind",Key_Bind_f);
757         Cmd_AddCommand ("unbind",Key_Unbind_f);
758         Cmd_AddCommand ("unbindall",Key_Unbindall_f);
759         Cmd_AddCommand ("bindlist",Key_Bindlist_f);
760 }
761
762 /*
763 ===================
764 Key_Event
765
766 Called by the system between frames for both key up and key down events
767 Should NOT be called during an interrupt!
768 ===================
769 */
770 void Key_Event (int key, char ascii, qboolean down)
771 {
772         char    *kb;
773         char    cmd[1024];
774
775         keydown[key] = down;
776
777         // update auto-repeat status
778         if (down)
779         {
780                 key_repeats[key]++;
781                 if (key != K_BACKSPACE
782                  && key != K_PAUSE
783                  && key != K_PGUP
784                  && key != K_KP_PGUP
785                  && key != K_PGDN
786                  && key != K_KP_PGDN
787                  && key_repeats[key] > 1)
788                         return; // ignore most autorepeats
789
790                 if (key >= 200 && !keybindings[key])
791                         Con_Printf ("%s is unbound, hit F4 to set.\n", Key_KeynumToString (key));
792         }
793         else
794                 key_repeats[key] = 0;
795
796 //
797 // handle escape specialy, so the user can never unbind it
798 //
799         if (key == K_ESCAPE)
800         {
801                 if (!down)
802                         return;
803                 switch (key_dest)
804                 {
805                 case key_message:
806                         Key_Message (key, 0);
807                         break;
808                 case key_menu:
809                         MR_Keydown (key, 0);
810                         break;
811                 case key_game:
812                         MR_ToggleMenu_f ();
813                         break;
814                 default:
815                         if(UI_Callback_IsSlotUsed(key_dest - 3))
816                                 UI_Callback_KeyDown (key, ascii);
817                         else
818                                 Sys_Error ("Bad key_dest");
819                 }
820                 return;
821         }
822
823         // console key is hardcoded, so the user can never unbind it
824         if (key == '`' || key == '~')
825         {
826                 if (down)
827                         Con_ToggleConsole_f ();
828                 return;
829         }
830
831         if (down)
832         {
833                 kb = keybindings[key];
834                 if (kb && !strncmp(kb, "toggleconsole", strlen("toggleconsole")))
835                 {
836                         Cbuf_AddText (kb);
837                         Cbuf_AddText ("\n");
838                         return;
839                 }
840         }
841
842         // AK New WTF ?!?
843         // AK Changed so the code does what the comments tell 
844         // 
845         // 1. if console is active or not, always send the up events
846         // console only wants key down events
847         if (key_consoleactive && consolekeys[key] && down)
848                 Key_Console (key, ascii);
849         else
850         {
851                 //
852                 // key up events only generate commands if the game key binding is
853                 // a button command (leading + sign).  These will occur even in console mode,
854                 // to keep the character from continuing an action started before a console
855                 // switch.  Button commands include the keynum as a parameter, so multiple
856                 // downs can be matched with ups
857                 //
858                 if (!down)
859                 {
860                         kb = keybindings[key];
861                         if (kb && kb[0] == '+')
862                         {
863                                 sprintf (cmd, "-%s %i\n", kb+1, key);
864                                 Cbuf_AddText (cmd);
865                         }
866                         return;
867                 }
868
869                 //
870                 // during demo playback, most keys bring up the main menu
871                 //
872                 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game)
873                         key = K_ESCAPE;
874
875                 //
876                 // if not a consolekey, send to the interpreter no matter what mode is
877                 //
878                 if ((key_consoleactive && !consolekeys[key])
879                  || (key_dest == key_menu && menubound[key])
880                  || key_dest == key_game)
881                 {
882                         kb = keybindings[key];
883                         if (kb)
884                         {
885                                 if (kb[0] == '+')
886                                 {
887                                         // button commands add keynum as a parm
888                                         sprintf (cmd, "%s %i\n", kb, key);
889                                         Cbuf_AddText (cmd);
890                                 }
891                                 else
892                                 {
893                                         Cbuf_AddText (kb);
894                                         Cbuf_AddText ("\n");
895                                 }
896                         }
897                         return;
898                 }
899
900                 switch (key_dest)
901                 {
902                 case key_message:
903                         Key_Message (key, ascii);
904                         break;
905                 case key_menu:
906                         MR_Keydown (key, ascii);
907                         break;
908
909                 case key_game:
910                 //case key_console:
911                         Key_Console (key, ascii);
912                         break;
913                 default:
914                         if(UI_Callback_IsSlotUsed(key_dest - 3))
915                                 UI_Callback_KeyDown (key, ascii);
916                         else
917                                 Sys_Error ("Bad key_dest");
918                 }
919         }
920 }
921
922
923 /*
924 ===================
925 Key_ClearStates
926 ===================
927 */
928 void Key_ClearStates (void)
929 {
930         int i;
931
932         for (i = 0;i < 256;i++)
933         {
934                 keydown[i] = false;
935                 key_repeats[i] = 0;
936         }
937 }
938