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