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
168 {"APOSTROPHE", '\''},
174 ==============================================================================
176 LINE TYPING INTO THE CONSOLE
178 ==============================================================================
186 Interactive line editing and console scrollback
189 void Key_Console (int key, char ascii)
191 // LordHavoc: copied most of this from Q2 to improve keyboard handling
218 case K_KP_RIGHTARROW:
238 // LordHavoc: FIXME: implement this sometime
240 if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
243 if ((cbd = Sys_GetClipboardData()) != 0)
246 strtok(cbd, "\n\r\b");
248 if (i + key_linepos >= MAXCMDLINE)
249 i= MAXCMDLINE - key_linepos;
253 strcat(key_lines[edit_line], cbd);
266 Cbuf_AddText ("clear\n");
271 if (key == K_ENTER || key == K_KP_ENTER)
273 Cbuf_AddText (key_lines[edit_line]+1); // skip the ]
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
281 // force an update, because the command may take some time
282 if (cls.state == ca_disconnected)
292 // Enhanced command completion
293 // by EvilTypeGuy eviltypeguy@qeradiant.com
294 // Thanks to Fett, Taniwha
295 Con_CompleteCommandLine();
299 // Advanced Console Editing by Radix radix@planetquake.com
300 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
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)
311 // delete char before cursor
312 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
316 strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
322 // delete char on cursor
323 if (key == K_DEL || key == K_KP_DEL)
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);
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)
335 if (strlen(key_lines[edit_line]) == (size_t)key_linepos)
337 if (strlen(key_lines[(edit_line + 31) & 31]) <= (size_t)key_linepos)
338 return; // no character to get
340 key_lines[edit_line][key_linepos] = key_lines[(edit_line + 31) & 31][key_linepos];
342 key_lines[edit_line][key_linepos] = 0;
350 if (key == K_INS || key == K_KP_INS) // toggle insert mode
356 // End Advanced Console Editing
358 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
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]);
372 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
374 if (history_line == edit_line) return;
377 history_line = (history_line + 1) & 31;
379 while (history_line != edit_line
380 && !key_lines[history_line][1]);
381 if (history_line == edit_line)
383 key_lines[edit_line][0] = ']';
388 strcpy(key_lines[edit_line], key_lines[history_line]);
389 key_linepos = strlen(key_lines[edit_line]);
394 if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
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;
402 if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
404 con_backscroll -= ((int) scr_conlines >> 4);
405 if (con_backscroll < 0)
410 if (key == K_HOME || key == K_KP_HOME)
412 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
416 if (key == K_END || key == K_KP_END)
423 if (ascii < 32 || ascii > 126)
426 if (key_linepos < MAXCMDLINE-1)
430 if (key_insert) // check insert mode
432 // can't do strcpy to move string to right
433 i = strlen(key_lines[edit_line]) - 1;
438 for (; i >= key_linepos; i--)
439 key_lines[edit_line][i + 1] = key_lines[edit_line][i];
442 // only null terminate if at the end
443 i = key_lines[edit_line][key_linepos];
444 key_lines[edit_line][key_linepos] = ascii;
448 key_lines[edit_line][key_linepos] = 0;
452 //============================================================================
454 // LordHavoc: increased messagemode length (was 32)
455 qboolean chat_team = false;
456 char chat_buffer[256];
457 int chat_bufferlen = 0;
459 void Key_Message (int key, char ascii)
461 if (key == K_ENTER || key == K_KP_ENTER)
464 Cbuf_AddText ("say_team \"");
466 Cbuf_AddText ("say \"");
467 Cbuf_AddText(chat_buffer);
468 Cbuf_AddText("\"\n");
484 if (key == K_BACKSPACE)
489 chat_buffer[chat_bufferlen] = 0;
495 if (ascii < 32 || ascii > 126)
498 if (chat_bufferlen == sizeof(chat_buffer) - 1)
501 chat_buffer[chat_bufferlen++] = ascii;
502 chat_buffer[chat_bufferlen] = 0;
505 //============================================================================
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.
517 int Key_StringToKeynum (const char *str)
524 return tolower(str[0]);
526 for (kn=keynames ; kn->name ; kn++)
527 if (!strcasecmp(str,kn->name))
536 Returns a string (either a single ascii char, or a K_* name) for the
538 FIXME: handle quote special (general escape sequence?)
541 char *Key_KeynumToString (int keynum)
544 static char tinystr[2];
547 return "<KEY NOT FOUND>";
548 if (keynum > 32 && keynum < 127)
555 for (kn=keynames ; kn->name ; kn++)
556 if (keynum == kn->keynum)
559 return "<UNKNOWN KEYNUM>";
568 void Key_SetBinding (int keynum, char *binding)
573 if (keynum < 0 || keynum >= 256)
577 if (keybindings[keynum])
579 Z_Free (keybindings[keynum]);
580 keybindings[keynum] = NULL;
583 // allocate memory for new binding
584 l = strlen (binding);
585 new = Z_Malloc (l+1);
586 strcpy (new, binding);
588 keybindings[keynum] = new;
596 void Key_Unbind_f (void)
602 Con_Printf ("unbind <key> : remove commands from a key\n");
606 b = Key_StringToKeynum (Cmd_Argv(1));
609 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
613 Key_SetBinding (b, "");
616 void Key_Unbindall_f (void)
620 for (i = 0;i < 256;i++)
622 Key_SetBinding (i, "");
631 void Key_Bind_f (void)
638 if (c != 2 && c != 3)
640 Con_Printf ("bind <key> [command] : attach a command to a key\n");
643 b = Key_StringToKeynum (Cmd_Argv(1));
646 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
653 Con_Printf ("\"%s\" = \"%s\"\n", Cmd_Argv(1), keybindings[b] );
655 Con_Printf ("\"%s\" is not bound\n", Cmd_Argv(1) );
659 // copy the rest of the command line
660 // start out with a null string
662 for (i=2 ; i< c ; i++)
666 strcat (cmd, Cmd_Argv(i));
669 Key_SetBinding (b, cmd);
676 Writes lines containing "bind key value"
679 void Key_WriteBindings (qfile_t *f)
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]);
695 void Key_Bindlist_f (void)
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]);
714 // LordHavoc: clear keybindings array so bounds checking won't freak
715 for (i = 0;i < 256;i++)
716 keybindings[i] = NULL;
718 for (i = 0;i < 32;i++)
720 key_lines[i][0] = ']';
726 // init ascii characters in console mode
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;
751 consolekeys['`'] = false;
752 consolekeys['~'] = false;
754 menubound[K_ESCAPE] = true;
755 for (i=0 ; i<12 ; i++)
756 menubound[K_F1+i] = true;
759 // register our functions
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);
771 Called by the system between frames for both key up and key down events
772 Should NOT be called during an interrupt!
775 void Key_Event (int key, char ascii, qboolean down)
782 // update auto-repeat status
786 if (key != K_BACKSPACE
792 && key_repeats[key] > 1)
793 return; // ignore most autorepeats
795 if (key >= 200 && !keybindings[key])
796 Con_Printf ("%s is unbound, hit F4 to set.\n", Key_KeynumToString (key));
799 key_repeats[key] = 0;
802 // handle escape specialy, so the user can never unbind it
811 Key_Message (key, 0);
820 if(UI_Callback_IsSlotUsed(key_dest - 3))
821 UI_Callback_KeyDown (key, ascii);
823 Sys_Error ("Bad key_dest");
828 // console key is hardcoded, so the user can never unbind it
829 if (key == '`' || key == '~')
832 Con_ToggleConsole_f ();
838 kb = keybindings[key];
839 if (kb && !strncmp(kb, "toggleconsole", strlen("toggleconsole")))
848 // AK Changed so the code does what the comments tell
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);
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
865 kb = keybindings[key];
866 if (kb && kb[0] == '+')
868 sprintf (cmd, "-%s %i\n", kb+1, key);
875 // during demo playback, most keys bring up the main menu
877 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game)
881 // if not a consolekey, send to the interpreter no matter what mode is
883 if ((key_consoleactive && !consolekeys[key])
884 || (key_dest == key_menu && menubound[key])
885 || key_dest == key_game)
887 kb = keybindings[key];
892 // button commands add keynum as a parm
893 sprintf (cmd, "%s %i\n", kb, key);
908 Key_Message (key, ascii);
911 MR_Keydown (key, ascii);
916 Key_Console (key, ascii);
919 if(UI_Callback_IsSlotUsed(key_dest - 3))
920 UI_Callback_KeyDown (key, ascii);
922 Sys_Error ("Bad key_dest");
933 void Key_ClearStates (void)
937 for (i = 0;i < 256;i++)