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.
25 key up events are sent even if in console mode
30 #define MAXCMDLINE 256
31 char key_lines[32][MAXCMDLINE];
33 int key_insert; // insert key toggle (for editing)
38 int key_consoleactive;
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];
53 keyname_t keynames[] =
59 {"BACKSPACE", K_BACKSPACE},
60 {"UPARROW", K_UPARROW},
61 {"DOWNARROW", K_DOWNARROW},
62 {"LEFTARROW", K_LEFTARROW},
63 {"RIGHTARROW", K_RIGHTARROW},
92 // LordHavoc: thanks to backslash for this MOUSE4 and MOUSE5 code
93 /* backslash :: imouse explorer buttons */
97 // LordHavoc: added more for completeness
100 {"MOUSE8", K_MOUSE8},
101 {"MOUSE9", K_MOUSE9},
102 {"MOUSE10", K_MOUSE10},
142 {"KP_HOME", K_KP_HOME },
143 {"KP_UPARROW", K_KP_UPARROW },
144 {"KP_PGUP", K_KP_PGUP },
145 {"KP_LEFTARROW", K_KP_LEFTARROW },
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 },
158 {"MWHEELUP", K_MWHEELUP },
159 {"MWHEELDOWN", K_MWHEELDOWN },
163 {"SEMICOLON", ';'}, // because a raw semicolon seperates commands
169 ==============================================================================
171 LINE TYPING INTO THE CONSOLE
173 ==============================================================================
181 Interactive line editing and console scrollback
184 void Key_Console (int key, char ascii)
186 // LordHavoc: copied most of this from Q2 to improve keyboard handling
213 case K_KP_RIGHTARROW:
233 // LordHavoc: FIXME: implement this sometime
235 if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
238 if ((cbd = Sys_GetClipboardData()) != 0)
241 strtok(cbd, "\n\r\b");
243 if (i + key_linepos >= MAXCMDLINE)
244 i= MAXCMDLINE - key_linepos;
248 strcat(key_lines[edit_line], cbd);
261 Cbuf_AddText ("clear\n");
266 if (key == K_ENTER || key == K_KP_ENTER)
268 Cbuf_AddText (key_lines[edit_line]+1); // skip the ]
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
276 // force an update, because the command may take some time
277 if (cls.state == ca_disconnected)
287 // Enhanced command completion
288 // by EvilTypeGuy eviltypeguy@qeradiant.com
289 // Thanks to Fett, Taniwha
290 Con_CompleteCommandLine();
294 // Advanced Console Editing by Radix radix@planetquake.com
295 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
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)
306 // delete char before cursor
307 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
311 strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
317 // delete char on cursor
318 if (key == K_DEL || key == K_KP_DEL)
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);
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)
330 if (strlen(key_lines[edit_line]) == (size_t)key_linepos)
332 if (strlen(key_lines[(edit_line + 31) & 31]) <= (size_t)key_linepos)
333 return; // no character to get
335 key_lines[edit_line][key_linepos] = key_lines[(edit_line + 31) & 31][key_linepos];
337 key_lines[edit_line][key_linepos] = 0;
345 if (key == K_INS || key == K_KP_INS) // toggle insert mode
351 // End Advanced Console Editing
353 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
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]);
367 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
369 if (history_line == edit_line) return;
372 history_line = (history_line + 1) & 31;
374 while (history_line != edit_line
375 && !key_lines[history_line][1]);
376 if (history_line == edit_line)
378 key_lines[edit_line][0] = ']';
383 strcpy(key_lines[edit_line], key_lines[history_line]);
384 key_linepos = strlen(key_lines[edit_line]);
389 if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
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;
397 if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
399 con_backscroll -= ((int) scr_conlines >> 4);
400 if (con_backscroll < 0)
405 if (key == K_HOME || key == K_KP_HOME)
407 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
411 if (key == K_END || key == K_KP_END)
418 if (ascii < 32 || ascii > 126)
421 if (key_linepos < MAXCMDLINE-1)
425 if (key_insert) // check insert mode
427 // can't do strcpy to move string to right
428 i = strlen(key_lines[edit_line]) - 1;
433 for (; i >= key_linepos; i--)
434 key_lines[edit_line][i + 1] = key_lines[edit_line][i];
437 // only null terminate if at the end
438 i = key_lines[edit_line][key_linepos];
439 key_lines[edit_line][key_linepos] = ascii;
443 key_lines[edit_line][key_linepos] = 0;
447 //============================================================================
449 // LordHavoc: increased messagemode length (was 32)
450 qboolean chat_team = false;
451 char chat_buffer[256];
452 int chat_bufferlen = 0;
454 void Key_Message (int key, char ascii)
456 if (key == K_ENTER || key == K_KP_ENTER)
459 Cbuf_AddText ("say_team \"");
461 Cbuf_AddText ("say \"");
462 Cbuf_AddText(chat_buffer);
463 Cbuf_AddText("\"\n");
479 if (key == K_BACKSPACE)
484 chat_buffer[chat_bufferlen] = 0;
490 if (ascii < 32 || ascii > 126)
493 if (chat_bufferlen == sizeof(chat_buffer) - 1)
496 chat_buffer[chat_bufferlen++] = ascii;
497 chat_buffer[chat_bufferlen] = 0;
500 //============================================================================
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.
512 int Key_StringToKeynum (const char *str)
519 return tolower(str[0]);
521 for (kn=keynames ; kn->name ; kn++)
522 if (!strcasecmp(str,kn->name))
531 Returns a string (either a single ascii char, or a K_* name) for the
533 FIXME: handle quote special (general escape sequence?)
536 char *Key_KeynumToString (int keynum)
539 static char tinystr[2];
542 return "<KEY NOT FOUND>";
543 if (keynum > 32 && keynum < 127)
550 for (kn=keynames ; kn->name ; kn++)
551 if (keynum == kn->keynum)
554 return "<UNKNOWN KEYNUM>";
563 void Key_SetBinding (int keynum, char *binding)
568 if (keynum < 0 || keynum >= 256)
572 if (keybindings[keynum])
574 Z_Free (keybindings[keynum]);
575 keybindings[keynum] = NULL;
578 // allocate memory for new binding
579 l = strlen (binding);
580 new = Z_Malloc (l+1);
581 strcpy (new, binding);
583 keybindings[keynum] = new;
591 void Key_Unbind_f (void)
597 Con_Printf ("unbind <key> : remove commands from a key\n");
601 b = Key_StringToKeynum (Cmd_Argv(1));
604 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
608 Key_SetBinding (b, "");
611 void Key_Unbindall_f (void)
615 for (i = 0;i < 256;i++)
617 Key_SetBinding (i, "");
626 void Key_Bind_f (void)
633 if (c != 2 && c != 3)
635 Con_Printf ("bind <key> [command] : attach a command to a key\n");
638 b = Key_StringToKeynum (Cmd_Argv(1));
641 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
648 Con_Printf ("\"%s\" = \"%s\"\n", Cmd_Argv(1), keybindings[b] );
650 Con_Printf ("\"%s\" is not bound\n", Cmd_Argv(1) );
654 // copy the rest of the command line
655 // start out with a null string
657 for (i=2 ; i< c ; i++)
661 strcat (cmd, Cmd_Argv(i));
664 Key_SetBinding (b, cmd);
671 Writes lines containing "bind key value"
674 void Key_WriteBindings (qfile_t *f)
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]);
690 void Key_Bindlist_f (void)
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]);
709 // LordHavoc: clear keybindings array so bounds checking won't freak
710 for (i = 0;i < 256;i++)
711 keybindings[i] = NULL;
713 for (i = 0;i < 32;i++)
715 key_lines[i][0] = ']';
721 // init ascii characters in console mode
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;
746 consolekeys['`'] = false;
747 consolekeys['~'] = false;
749 menubound[K_ESCAPE] = true;
750 for (i=0 ; i<12 ; i++)
751 menubound[K_F1+i] = true;
754 // register our functions
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);
766 Called by the system between frames for both key up and key down events
767 Should NOT be called during an interrupt!
770 void Key_Event (int key, char ascii, qboolean down)
777 // update auto-repeat status
781 if (key != K_BACKSPACE
787 && key_repeats[key] > 1)
788 return; // ignore most autorepeats
790 if (key >= 200 && !keybindings[key])
791 Con_Printf ("%s is unbound, hit F4 to set.\n", Key_KeynumToString (key));
794 key_repeats[key] = 0;
797 // handle escape specialy, so the user can never unbind it
806 Key_Message (key, 0);
815 Sys_Error ("Bad key_dest");
820 // console key is hardcoded, so the user can never unbind it
821 if (key == '`' || key == '~')
824 Con_ToggleConsole_f ();
830 kb = keybindings[key];
831 if (kb && !strncmp(kb, "toggleconsole", strlen("toggleconsole")))
839 // AK What the fuck ?!?
840 // AK Changed so the code does what the comments tell
842 // 1. if console is active or not, always send the up events
843 if (key_consoleactive && consolekeys[key] && down)
845 // console only wants key down events
849 Key_Console (key, ascii);
854 // key up events only generate commands if the game key binding is
855 // a button command (leading + sign). These will occur even in console mode,
856 // to keep the character from continuing an action started before a console
857 // switch. Button commands include the keynum as a parameter, so multiple
858 // downs can be matched with ups
862 kb = keybindings[key];
863 if (kb && kb[0] == '+')
865 sprintf (cmd, "-%s %i\n", kb+1, key);
872 // during demo playback, most keys bring up the main menu
874 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game)
878 // if not a consolekey, send to the interpreter no matter what mode is
880 if ((key_consoleactive && !consolekeys[key])
881 || (key_dest == key_menu && menubound[key])
882 || key_dest == key_game)
884 kb = keybindings[key];
889 // button commands add keynum as a parm
890 sprintf (cmd, "%s %i\n", kb, key);
903 return; // other systems only care about key down events
908 Key_Message (key, ascii);
911 MR_Keydown (key, ascii);
916 Key_Console (key, ascii);
919 Sys_Error ("Bad key_dest");
930 void Key_ClearStates (void)
934 for (i = 0;i < 256;i++)