2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 key up events are sent even if in console mode
29 #define MAXCMDLINE 256
30 char key_lines[32][MAXCMDLINE];
32 int key_insert; // insert key toggle (for editing)
37 int key_consoleactive;
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];
52 keyname_t keynames[] =
58 {"BACKSPACE", K_BACKSPACE},
59 {"UPARROW", K_UPARROW},
60 {"DOWNARROW", K_DOWNARROW},
61 {"LEFTARROW", K_LEFTARROW},
62 {"RIGHTARROW", K_RIGHTARROW},
91 // LordHavoc: thanks to backslash for this MOUSE4 and MOUSE5 code
92 /* backslash :: imouse explorer buttons */
96 // LordHavoc: added more for completeness
100 {"MOUSE9", K_MOUSE9},
101 {"MOUSE10", K_MOUSE10},
141 {"KP_HOME", K_KP_HOME },
142 {"KP_UPARROW", K_KP_UPARROW },
143 {"KP_PGUP", K_KP_PGUP },
144 {"KP_LEFTARROW", K_KP_LEFTARROW },
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 },
157 {"MWHEELUP", K_MWHEELUP },
158 {"MWHEELDOWN", K_MWHEELDOWN },
162 {"SEMICOLON", ';'}, // because a raw semicolon seperates commands
168 ==============================================================================
170 LINE TYPING INTO THE CONSOLE
172 ==============================================================================
180 Interactive line editing and console scrollback
183 void Key_Console (int key, char ascii)
185 // LordHavoc: copied most of this from Q2 to improve keyboard handling
212 case K_KP_RIGHTARROW:
232 // LordHavoc: FIXME: implement this sometime
234 if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
237 if ((cbd = Sys_GetClipboardData()) != 0)
240 strtok(cbd, "\n\r\b");
242 if (i + key_linepos >= MAXCMDLINE)
243 i= MAXCMDLINE - key_linepos;
247 strcat(key_lines[edit_line], cbd);
260 Cbuf_AddText ("clear\n");
265 if (key == K_ENTER || key == K_KP_ENTER)
267 Cbuf_AddText (key_lines[edit_line]+1); // skip the ]
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
275 // force an update, because the command may take some time
276 if (cls.state == ca_disconnected)
286 // Enhanced command completion
287 // by EvilTypeGuy eviltypeguy@qeradiant.com
288 // Thanks to Fett, Taniwha
289 Con_CompleteCommandLine();
293 // Advanced Console Editing by Radix radix@planetquake.com
294 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
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)
305 // delete char before cursor
306 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
310 strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
316 // delete char on cursor
317 if (key == K_DEL || key == K_KP_DEL)
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);
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)
329 if (strlen(key_lines[edit_line]) == (size_t)key_linepos)
331 if (strlen(key_lines[(edit_line + 31) & 31]) <= (size_t)key_linepos)
332 return; // no character to get
334 key_lines[edit_line][key_linepos] = key_lines[(edit_line + 31) & 31][key_linepos];
336 key_lines[edit_line][key_linepos] = 0;
344 if (key == K_INS || key == K_KP_INS) // toggle insert mode
350 // End Advanced Console Editing
352 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
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]);
366 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
368 if (history_line == edit_line) return;
371 history_line = (history_line + 1) & 31;
373 while (history_line != edit_line
374 && !key_lines[history_line][1]);
375 if (history_line == edit_line)
377 key_lines[edit_line][0] = ']';
382 strcpy(key_lines[edit_line], key_lines[history_line]);
383 key_linepos = strlen(key_lines[edit_line]);
388 if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
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;
396 if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
398 con_backscroll -= ((int) scr_conlines >> 4);
399 if (con_backscroll < 0)
404 if (key == K_HOME || key == K_KP_HOME)
406 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
410 if (key == K_END || key == K_KP_END)
417 if (ascii < 32 || ascii > 126)
420 if (key_linepos < MAXCMDLINE-1)
424 if (key_insert) // check insert mode
426 // can't do strcpy to move string to right
427 i = strlen(key_lines[edit_line]) - 1;
432 for (; i >= key_linepos; i--)
433 key_lines[edit_line][i + 1] = key_lines[edit_line][i];
436 // only null terminate if at the end
437 i = key_lines[edit_line][key_linepos];
438 key_lines[edit_line][key_linepos] = ascii;
442 key_lines[edit_line][key_linepos] = 0;
446 //============================================================================
448 // LordHavoc: increased messagemode length (was 32)
449 qboolean chat_team = false;
450 char chat_buffer[256];
451 int chat_bufferlen = 0;
453 void Key_Message (int key, char ascii)
455 if (key == K_ENTER || key == K_KP_ENTER)
458 Cbuf_AddText ("say_team \"");
460 Cbuf_AddText ("say \"");
461 Cbuf_AddText(chat_buffer);
462 Cbuf_AddText("\"\n");
478 if (key == K_BACKSPACE)
483 chat_buffer[chat_bufferlen] = 0;
489 if (ascii < 32 || ascii > 126)
492 if (chat_bufferlen == sizeof(chat_buffer) - 1)
495 chat_buffer[chat_bufferlen++] = ascii;
496 chat_buffer[chat_bufferlen] = 0;
499 //============================================================================
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.
511 int Key_StringToKeynum (const char *str)
520 for (kn=keynames ; kn->name ; kn++)
521 if (!strcasecmp(str,kn->name))
530 Returns a string (either a single ascii char, or a K_* name) for the
532 FIXME: handle quote special (general escape sequence?)
535 char *Key_KeynumToString (int keynum)
538 static char tinystr[2];
541 return "<KEY NOT FOUND>";
542 if (keynum > 32 && keynum < 127)
549 for (kn=keynames ; kn->name ; kn++)
550 if (keynum == kn->keynum)
553 return "<UNKNOWN KEYNUM>";
562 void Key_SetBinding (int keynum, char *binding)
567 if (keynum < 0 || keynum >= 256)
571 if (keybindings[keynum])
573 Z_Free (keybindings[keynum]);
574 keybindings[keynum] = NULL;
577 // allocate memory for new binding
578 l = strlen (binding);
579 new = Z_Malloc (l+1);
580 strcpy (new, binding);
582 keybindings[keynum] = new;
590 void Key_Unbind_f (void)
596 Con_Printf ("unbind <key> : remove commands from a key\n");
600 b = Key_StringToKeynum (Cmd_Argv(1));
603 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
607 Key_SetBinding (b, "");
610 void Key_Unbindall_f (void)
614 for (i = 0;i < 256;i++)
616 Key_SetBinding (i, "");
625 void Key_Bind_f (void)
632 if (c != 2 && c != 3)
634 Con_Printf ("bind <key> [command] : attach a command to a key\n");
637 b = Key_StringToKeynum (Cmd_Argv(1));
640 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
647 Con_Printf ("\"%s\" = \"%s\"\n", Cmd_Argv(1), keybindings[b] );
649 Con_Printf ("\"%s\" is not bound\n", Cmd_Argv(1) );
653 // copy the rest of the command line
654 // start out with a null string
656 for (i=2 ; i< c ; i++)
660 strcat (cmd, Cmd_Argv(i));
663 Key_SetBinding (b, cmd);
670 Writes lines containing "bind key value"
673 void Key_WriteBindings (qfile_t *f)
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]);
689 void Key_Bindlist_f (void)
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]);
708 // LordHavoc: clear keybindings array so bounds checking won't freak
709 for (i = 0;i < 256;i++)
710 keybindings[i] = NULL;
712 for (i = 0;i < 32;i++)
714 key_lines[i][0] = ']';
720 // init ascii characters in console mode
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;
745 consolekeys['`'] = false;
746 consolekeys['~'] = false;
748 menubound[K_ESCAPE] = true;
749 for (i=0 ; i<12 ; i++)
750 menubound[K_F1+i] = true;
753 // register our functions
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);
765 Called by the system between frames for both key up and key down events
766 Should NOT be called during an interrupt!
769 void Key_Event (int key, char ascii, qboolean down)
776 // update auto-repeat status
780 if (key != K_BACKSPACE
786 && key_repeats[key] > 1)
787 return; // ignore most autorepeats
789 if (key >= 200 && !keybindings[key])
790 Con_Printf ("%s is unbound, hit F4 to set.\n", Key_KeynumToString (key));
793 key_repeats[key] = 0;
796 // handle escape specialy, so the user can never unbind it
805 Key_Message (key, 0);
814 Sys_Error ("Bad key_dest");
819 // console key is hardcoded, so the user can never unbind it
820 if (key == '`' || key == '~')
823 Con_ToggleConsole_f ();
829 kb = keybindings[key];
830 if (kb && !strncmp(kb, "toggleconsole", strlen("toggleconsole")))
838 // AK What the fuck ?!?
839 // AK Changed so the code does what the comments tell
841 // 1. if console is active or not, always send the up events
842 if (key_consoleactive && consolekeys[key] && down)
844 // console only wants key down events
848 Key_Console (key, ascii);
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
861 kb = keybindings[key];
862 if (kb && kb[0] == '+')
864 sprintf (cmd, "-%s %i\n", kb+1, key);
871 // during demo playback, most keys bring up the main menu
873 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game)
877 // if not a consolekey, send to the interpreter no matter what mode is
879 if ((key_consoleactive && !consolekeys[key])
880 || (key_dest == key_menu && menubound[key])
881 || key_dest == key_game)
883 kb = keybindings[key];
888 // button commands add keynum as a parm
889 sprintf (cmd, "%s %i\n", kb, key);
902 return; // other systems only care about key down events
907 Key_Message (key, ascii);
910 MR_Keydown (key, ascii);
915 Key_Console (key, ascii);
918 Sys_Error ("Bad key_dest");
929 void Key_ClearStates (void)
933 for (i = 0;i < 256;i++)