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 shift_down = false;
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 keyshift[256]; // key to map to if shift held down in console
45 int key_repeats[256]; // if > 1, it is autorepeating
46 qboolean keydown[256];
54 keyname_t keynames[] =
60 {"BACKSPACE", K_BACKSPACE},
61 {"UPARROW", K_UPARROW},
62 {"DOWNARROW", K_DOWNARROW},
63 {"LEFTARROW", K_LEFTARROW},
64 {"RIGHTARROW", K_RIGHTARROW},
93 // LordHavoc: thanks to backslash for this MOUSE4 and MOUSE5 code
94 /* backslash :: imouse explorer buttons */
98 // LordHavoc: added more for completeness
100 {"MOUSE7", K_MOUSE7},
101 {"MOUSE8", K_MOUSE8},
102 {"MOUSE9", K_MOUSE9},
103 {"MOUSE10", K_MOUSE10},
143 {"KP_HOME", K_KP_HOME },
144 {"KP_UPARROW", K_KP_UPARROW },
145 {"KP_PGUP", K_KP_PGUP },
146 {"KP_LEFTARROW", K_KP_LEFTARROW },
148 {"KP_RIGHTARROW", K_KP_RIGHTARROW },
149 {"KP_END", K_KP_END },
150 {"KP_DOWNARROW", K_KP_DOWNARROW },
151 {"KP_PGDN", K_KP_PGDN },
152 {"KP_ENTER", K_KP_ENTER },
153 {"KP_INS", K_KP_INS },
154 {"KP_DEL", K_KP_DEL },
155 {"KP_SLASH", K_KP_SLASH },
156 {"KP_MINUS", K_KP_MINUS },
157 {"KP_PLUS", K_KP_PLUS },
159 {"MWHEELUP", K_MWHEELUP },
160 {"MWHEELDOWN", K_MWHEELDOWN },
164 {"SEMICOLON", ';'}, // because a raw semicolon seperates commands
170 ==============================================================================
172 LINE TYPING INTO THE CONSOLE
174 ==============================================================================
182 Interactive line editing and console scrollback
185 void Key_Console (int key)
187 // LordHavoc: copied most of this from Q2 to improve keyboard handling
214 case K_KP_RIGHTARROW:
234 // LordHavoc: FIXME: implement this sometime
236 if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
239 if ((cbd = Sys_GetClipboardData()) != 0)
242 strtok(cbd, "\n\r\b");
244 if (i + key_linepos >= MAXCMDLINE)
245 i= MAXCMDLINE - key_linepos;
249 strcat(key_lines[edit_line], cbd);
262 Cbuf_AddText ("clear\n");
267 if (key == K_ENTER || key == K_KP_ENTER)
269 Cbuf_AddText (key_lines[edit_line]+1); // skip the ]
271 Con_Printf ("%s\n",key_lines[edit_line]);
272 edit_line = (edit_line + 1) & 31;
273 history_line = edit_line;
274 key_lines[edit_line][0] = ']';
275 key_lines[edit_line][1] = 0; // EvilTypeGuy: null terminate
277 // force an update, because the command may take some time
278 if (cls.state == ca_disconnected)
288 // Enhanced command completion
289 // by EvilTypeGuy eviltypeguy@qeradiant.com
290 // Thanks to Fett, Taniwha
291 Con_CompleteCommandLine();
295 // Advanced Console Editing by Radix radix@planetquake.com
296 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
298 // left arrow will just move left one without erasing, backspace will
299 // actually erase charcter
300 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
307 // delete char before cursor
308 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
312 strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
318 // delete char on cursor
319 if (key == K_DEL || key == K_KP_DEL)
321 if ((size_t)key_linepos < strlen(key_lines[edit_line]))
322 strcpy(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1);
327 // if we're at the end, get one character from previous line,
328 // otherwise just go right one
329 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
331 if (strlen(key_lines[edit_line]) == (size_t)key_linepos)
333 if (strlen(key_lines[(edit_line + 31) & 31]) <= (size_t)key_linepos)
334 return; // no character to get
336 key_lines[edit_line][key_linepos] = key_lines[(edit_line + 31) & 31][key_linepos];
338 key_lines[edit_line][key_linepos] = 0;
346 if (key == K_INS || key == K_KP_INS) // toggle insert mode
352 // End Advanced Console Editing
354 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
358 history_line = (history_line - 1) & 31;
359 } while (history_line != edit_line
360 && !key_lines[history_line][1]);
361 if (history_line == edit_line)
362 history_line = (edit_line+1)&31;
363 strcpy(key_lines[edit_line], key_lines[history_line]);
364 key_linepos = strlen(key_lines[edit_line]);
368 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
370 if (history_line == edit_line) return;
373 history_line = (history_line + 1) & 31;
375 while (history_line != edit_line
376 && !key_lines[history_line][1]);
377 if (history_line == edit_line)
379 key_lines[edit_line][0] = ']';
384 strcpy(key_lines[edit_line], key_lines[history_line]);
385 key_linepos = strlen(key_lines[edit_line]);
390 if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
392 con_backscroll += ((int) scr_conlines >> 4);
393 if (con_backscroll > con_totallines - (vid.conheight>>3) - 1)
394 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
398 if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
400 con_backscroll -= ((int) scr_conlines >> 4);
401 if (con_backscroll < 0)
406 if (key == K_HOME || key == K_KP_HOME)
408 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
412 if (key == K_END || key == K_KP_END)
419 if (key < 32 || key > 127)
422 if (key_linepos < MAXCMDLINE-1)
426 if (key_insert) // check insert mode
428 // can't do strcpy to move string to right
429 i = strlen(key_lines[edit_line]) - 1;
434 for (; i >= key_linepos; i--)
435 key_lines[edit_line][i + 1] = key_lines[edit_line][i];
438 // only null terminate if at the end
439 i = key_lines[edit_line][key_linepos];
440 key_lines[edit_line][key_linepos] = key;
444 key_lines[edit_line][key_linepos] = 0;
448 //============================================================================
450 // LordHavoc: increased messagemode length (was 32)
451 qboolean chat_team = false;
452 char chat_buffer[256];
453 int chat_bufferlen = 0;
455 void Key_Message (int key)
457 if (key == K_ENTER || key == K_KP_ENTER)
460 Cbuf_AddText ("say_team \"");
462 Cbuf_AddText ("say \"");
463 Cbuf_AddText(chat_buffer);
464 Cbuf_AddText("\"\n");
481 if (key < 32 || key > 127)
484 if (key == K_BACKSPACE)
489 chat_buffer[chat_bufferlen] = 0;
494 if (chat_bufferlen == sizeof(chat_buffer) - 1)
497 chat_buffer[chat_bufferlen++] = key;
498 chat_buffer[chat_bufferlen] = 0;
501 //============================================================================
508 Returns a key number to be used to index keybindings[] by looking at
509 the given string. Single ascii characters return themselves, while
510 the K_* names are matched up.
513 int Key_StringToKeynum (const char *str)
522 for (kn=keynames ; kn->name ; kn++)
523 if (!strcasecmp(str,kn->name))
532 Returns a string (either a single ascii char, or a K_* name) for the
534 FIXME: handle quote special (general escape sequence?)
537 char *Key_KeynumToString (int keynum)
540 static char tinystr[2];
543 return "<KEY NOT FOUND>";
544 if (keynum > 32 && keynum < 127)
551 for (kn=keynames ; kn->name ; kn++)
552 if (keynum == kn->keynum)
555 return "<UNKNOWN KEYNUM>";
564 void Key_SetBinding (int keynum, char *binding)
569 if (keynum < 0 || keynum >= 256)
573 if (keybindings[keynum])
575 Z_Free (keybindings[keynum]);
576 keybindings[keynum] = NULL;
579 // allocate memory for new binding
580 l = strlen (binding);
581 new = Z_Malloc (l+1);
582 strcpy (new, binding);
584 keybindings[keynum] = new;
592 void Key_Unbind_f (void)
598 Con_Printf ("unbind <key> : remove commands from a key\n");
602 b = Key_StringToKeynum (Cmd_Argv(1));
605 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
609 Key_SetBinding (b, "");
612 void Key_Unbindall_f (void)
616 for (i = 0;i < 256;i++)
618 Key_SetBinding (i, "");
627 void Key_Bind_f (void)
634 if (c != 2 && c != 3)
636 Con_Printf ("bind <key> [command] : attach a command to a key\n");
639 b = Key_StringToKeynum (Cmd_Argv(1));
642 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
649 Con_Printf ("\"%s\" = \"%s\"\n", Cmd_Argv(1), keybindings[b] );
651 Con_Printf ("\"%s\" is not bound\n", Cmd_Argv(1) );
655 // copy the rest of the command line
656 // start out with a null string
658 for (i=2 ; i< c ; i++)
662 strcat (cmd, Cmd_Argv(i));
665 Key_SetBinding (b, cmd);
672 Writes lines containing "bind key value"
675 void Key_WriteBindings (qfile_t *f)
679 for (i = 0;i < 256;i++)
680 if (keybindings[i] && *keybindings[i])
681 FS_Printf (f, "bind \"%s\" \"%s\"\n", Key_KeynumToString(i), keybindings[i]);
691 void Key_Bindlist_f (void)
695 for (i = 0;i < 256;i++)
696 if (keybindings[i] && keybindings[i][0])
697 Con_Printf ("%s \"%s\"\n", Key_KeynumToString(i), keybindings[i]);
710 // LordHavoc: clear keybindings array so bounds checking won't freak
711 for (i = 0;i < 256;i++)
712 keybindings[i] = NULL;
714 for (i = 0;i < 32;i++)
716 key_lines[i][0] = ']';
722 // init ascii characters in console mode
724 for (i = 32;i < 128;i++)
725 consolekeys[i] = true;
726 consolekeys[K_ENTER] = true;consolekeys[K_KP_ENTER] = true;
727 consolekeys[K_TAB] = true;
728 consolekeys[K_LEFTARROW] = true;consolekeys[K_KP_LEFTARROW] = true;
729 consolekeys[K_RIGHTARROW] = true;consolekeys[K_KP_RIGHTARROW] = true;
730 consolekeys[K_UPARROW] = true;consolekeys[K_KP_UPARROW] = true;
731 consolekeys[K_DOWNARROW] = true;consolekeys[K_KP_DOWNARROW] = true;
732 consolekeys[K_BACKSPACE] = true;
733 consolekeys[K_HOME] = true;consolekeys[K_KP_HOME] = true;
734 consolekeys[K_END] = true;consolekeys[K_KP_END] = true;
735 consolekeys[K_PGUP] = true;consolekeys[K_KP_PGUP] = true;
736 consolekeys[K_PGDN] = true;consolekeys[K_KP_PGDN] = true;
737 consolekeys[K_SHIFT] = true;
738 consolekeys[K_INS] = true;consolekeys[K_KP_INS] = true;
739 consolekeys[K_DEL] = true;consolekeys[K_KP_DEL] = true;
740 consolekeys[K_KP_SLASH] = true;
741 consolekeys[K_KP_PLUS] = true;
742 consolekeys[K_KP_MINUS] = true;
743 consolekeys[K_KP_5] = true;
744 consolekeys[K_MWHEELUP] = true;
745 consolekeys[K_MWHEELDOWN] = true;
747 consolekeys['`'] = false;
748 consolekeys['~'] = false;
750 for (i = 0;i < 256;i++)
752 for (i = 'a';i <= 'z';i++)
753 keyshift[i] = i - 'a' + 'A';
770 keyshift['\''] = '"';
774 keyshift['\\'] = '|';
776 menubound[K_ESCAPE] = true;
777 for (i=0 ; i<12 ; i++)
778 menubound[K_F1+i] = true;
781 // register our functions
783 Cmd_AddCommand ("bind",Key_Bind_f);
784 Cmd_AddCommand ("unbind",Key_Unbind_f);
785 Cmd_AddCommand ("unbindall",Key_Unbindall_f);
786 Cmd_AddCommand ("bindlist",Key_Bindlist_f);
793 Called by the system between frames for both key up and key down events
794 Should NOT be called during an interrupt!
797 void Key_Event (int key, qboolean down)
804 // update auto-repeat status
808 if (key != K_BACKSPACE
814 && key_repeats[key] > 1)
815 return; // ignore most autorepeats
817 if (key >= 200 && !keybindings[key])
818 Con_Printf ("%s is unbound, hit F4 to set.\n", Key_KeynumToString (key));
821 key_repeats[key] = 0;
827 // handle escape specialy, so the user can never unbind it
845 Sys_Error ("Bad key_dest");
850 // console key is hardcoded, so the user can never unbind it
851 if (key == '`' || key == '~')
854 Con_ToggleConsole_f ();
860 kb = keybindings[key];
861 if (kb && !strncmp(kb, "toggleconsole", strlen("toggleconsole")))
869 if (key_consoleactive && consolekeys[key])
871 // console only wants key down events
875 // FIXME: this does not support non-QWERTY keyboards
884 // key up events only generate commands if the game key binding is
885 // a button command (leading + sign). These will occur even in console mode,
886 // to keep the character from continuing an action started before a console
887 // switch. Button commands include the keynum as a parameter, so multiple
888 // downs can be matched with ups
892 kb = keybindings[key];
893 if (kb && kb[0] == '+')
895 sprintf (cmd, "-%s %i\n", kb+1, key);
898 if (keyshift[key] != key)
900 kb = keybindings[keyshift[key]];
901 if (kb && kb[0] == '+')
903 sprintf (cmd, "-%s %i\n", kb+1, key);
911 // during demo playback, most keys bring up the main menu
913 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game)
917 // if not a consolekey, send to the interpreter no matter what mode is
919 if ((key_consoleactive && !consolekeys[key])
920 || (key_dest == key_menu && menubound[key])
921 || key_dest == key_game)
923 kb = keybindings[key];
928 // button commands add keynum as a parm
929 sprintf (cmd, "%s %i\n", kb, key);
942 return; // other systems only care about key down events
944 // FIXME: this does not support non-QWERTY keyboards
962 Sys_Error ("Bad key_dest");
973 void Key_ClearStates (void)
977 for (i = 0;i < 256;i++)