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;
34 int key_insert; // insert key toggle (for editing)
41 int key_count; // incremented every key event
43 char *keybindings[256];
44 qboolean consolekeys[256]; // if true, can't be rebound while in console
45 qboolean menubound[256]; // if true, can't be rebound while in menu
46 int keyshift[256]; // key to map to if shift held down in console
47 int key_repeats[256]; // if > 1, it is autorepeating
48 qboolean keydown[256];
56 keyname_t keynames[] =
62 {"BACKSPACE", K_BACKSPACE},
63 {"UPARROW", K_UPARROW},
64 {"DOWNARROW", K_DOWNARROW},
65 {"LEFTARROW", K_LEFTARROW},
66 {"RIGHTARROW", K_RIGHTARROW},
136 {"MWHEELUP", K_MWHEELUP},
137 {"MWHEELDOWN", K_MWHEELDOWN},
139 {"SEMICOLON", ';'}, // because a raw semicolon seperates commands
145 ==============================================================================
147 LINE TYPING INTO THE CONSOLE
149 ==============================================================================
157 Interactive line editing and console scrollback
160 void Key_Console (int key)
164 Cbuf_AddText (key_lines[edit_line]+1); // skip the >
166 Con_Printf ("%s\n",key_lines[edit_line]);
167 edit_line = (edit_line + 1) & 31;
168 history_line = edit_line;
169 key_lines[edit_line][0] = ']';
170 key_lines[edit_line][1] = 0; // EvilTypeGuy: null terminate
172 if (cls.state == ca_disconnected)
173 SCR_UpdateScreen (); // force an update, because the command
174 // may take some time
180 // Enhanced command completion
181 // by EvilTypeGuy eviltypeguy@qeradiant.com
182 // Thanks to Fett, Taniwha
183 Con_CompleteCommandLine();
186 // Advanced Console Editing by Radix radix@planetquake.com
187 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
189 // left arrow will just move left one without erasing, backspace will
190 // actually erase charcter
191 if (key == K_LEFTARROW)
198 if (key == K_BACKSPACE) // delete char before cursor
202 strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
208 if (key == K_DEL) // delete char on cursor
210 if (key_linepos < strlen(key_lines[edit_line]))
211 strcpy(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1);
216 // if we're at the end, get one character from previous line,
217 // otherwise just go right one
218 if (key == K_RIGHTARROW)
220 if (strlen(key_lines[edit_line]) == key_linepos)
222 if (strlen(key_lines[(edit_line + 31) & 31]) <= key_linepos)
223 return; // no character to get
225 key_lines[edit_line][key_linepos] = key_lines[(edit_line + 31) & 31][key_linepos];
227 key_lines[edit_line][key_linepos] = 0;
235 if (key == K_INS) // toggle insert mode
241 // End Advanced Console Editing
243 if (key == K_UPARROW)
247 history_line = (history_line - 1) & 31;
248 } while (history_line != edit_line
249 && !key_lines[history_line][1]);
250 if (history_line == edit_line)
251 history_line = (edit_line+1)&31;
252 strcpy(key_lines[edit_line], key_lines[history_line]);
253 key_linepos = strlen(key_lines[edit_line]);
257 if (key == K_DOWNARROW)
259 if (history_line == edit_line) return;
262 history_line = (history_line + 1) & 31;
264 while (history_line != edit_line
265 && !key_lines[history_line][1]);
266 if (history_line == edit_line)
268 key_lines[edit_line][0] = ']';
273 strcpy(key_lines[edit_line], key_lines[history_line]);
274 key_linepos = strlen(key_lines[edit_line]);
279 if (key == K_PGUP || key==K_MWHEELUP)
282 if (con_backscroll > con_totallines - (vid.height>>3) - 1)
283 con_backscroll = con_totallines - (vid.height>>3) - 1;
287 if (key == K_PGDN || key==K_MWHEELDOWN)
290 if (con_backscroll < 0)
297 con_backscroll = con_totallines - (vid.height>>3) - 1;
307 if (key < 32 || key > 127)
308 return; // non printable
312 if (key_linepos < MAXCMDLINE-1)
316 if (key_insert) // check insert mode
318 // can't do strcpy to move string to right
319 i = strlen(key_lines[edit_line]) - 1;
324 for (; i >= key_linepos; i--)
325 key_lines[edit_line][i + 1] = key_lines[edit_line][i];
328 // only null terminate if at the end
329 i = key_lines[edit_line][key_linepos];
330 key_lines[edit_line][key_linepos] = key;
334 key_lines[edit_line][key_linepos] = 0;
338 //============================================================================
340 // LordHavoc: increased messagemode length (was 32)
341 char chat_buffer[256];
342 qboolean team_message = false;
344 void Key_Message (int key)
346 static int chat_bufferlen = 0;
351 Cbuf_AddText ("say_team \"");
353 Cbuf_AddText ("say \"");
354 Cbuf_AddText(chat_buffer);
355 Cbuf_AddText("\"\n");
371 if (key < 32 || key > 127)
372 return; // non printable
374 if (key == K_BACKSPACE)
379 chat_buffer[chat_bufferlen] = 0;
384 // LordHavoc: increased messagemode length (was 31)
385 if (chat_bufferlen == 240)
388 chat_buffer[chat_bufferlen++] = key;
389 chat_buffer[chat_bufferlen] = 0;
392 //============================================================================
399 Returns a key number to be used to index keybindings[] by looking at
400 the given string. Single ascii characters return themselves, while
401 the K_* names are matched up.
404 int Key_StringToKeynum (char *str)
413 for (kn=keynames ; kn->name ; kn++)
415 if (!Q_strcasecmp(str,kn->name))
425 Returns a string (either a single ascii char, or a K_* name) for the
427 FIXME: handle quote special (general escape sequence?)
430 char *Key_KeynumToString (int keynum)
433 static char tinystr[2];
436 return "<KEY NOT FOUND>";
437 if (keynum > 32 && keynum < 127)
444 for (kn=keynames ; kn->name ; kn++)
445 if (keynum == kn->keynum)
448 return "<UNKNOWN KEYNUM>";
457 void Key_SetBinding (int keynum, char *binding)
466 if (keybindings[keynum])
468 Z_Free (keybindings[keynum]);
469 keybindings[keynum] = NULL;
472 // allocate memory for new binding
473 l = strlen (binding);
474 new = Z_Malloc (l+1);
475 strcpy (new, binding);
477 keybindings[keynum] = new;
485 void Key_Unbind_f (void)
491 Con_Printf ("unbind <key> : remove commands from a key\n");
495 b = Key_StringToKeynum (Cmd_Argv(1));
498 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
502 Key_SetBinding (b, "");
505 void Key_Unbindall_f (void)
509 for (i=0 ; i<256 ; i++)
511 Key_SetBinding (i, "");
520 void Key_Bind_f (void)
527 if (c != 2 && c != 3)
529 Con_Printf ("bind <key> [command] : attach a command to a key\n");
532 b = Key_StringToKeynum (Cmd_Argv(1));
535 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
542 Con_Printf ("\"%s\" = \"%s\"\n", Cmd_Argv(1), keybindings[b] );
544 Con_Printf ("\"%s\" is not bound\n", Cmd_Argv(1) );
548 // copy the rest of the command line
549 cmd[0] = 0; // start out with a null string
550 for (i=2 ; i< c ; i++)
554 strcat (cmd, Cmd_Argv(i));
557 Key_SetBinding (b, cmd);
564 Writes lines containing "bind key value"
567 void Key_WriteBindings (QFile *f)
571 for (i=0 ; i<256 ; i++)
574 Qprintf (f, "bind \"%s\" \"%s\"\n", Key_KeynumToString(i), keybindings[i]);
587 for (i=0 ; i<32 ; i++)
589 key_lines[i][0] = ']';
595 // init ascii characters in console mode
597 for (i=32 ; i<128 ; i++)
598 consolekeys[i] = true;
599 consolekeys[K_ENTER] = true;
600 consolekeys[K_TAB] = true;
601 consolekeys[K_LEFTARROW] = true;
602 consolekeys[K_RIGHTARROW] = true;
603 consolekeys[K_UPARROW] = true;
604 consolekeys[K_DOWNARROW] = true;
605 consolekeys[K_BACKSPACE] = true;
606 consolekeys[K_DEL] = true;
607 consolekeys[K_INS] = true;
608 consolekeys[K_PGUP] = true;
609 consolekeys[K_PGDN] = true;
610 consolekeys[K_SHIFT] = true;
611 consolekeys[K_MWHEELUP] = true;
612 consolekeys[K_MWHEELDOWN] = true;
613 consolekeys['`'] = false;
614 consolekeys['~'] = false;
616 for (i=0 ; i<256 ; i++)
618 for (i='a' ; i<='z' ; i++)
619 keyshift[i] = i - 'a' + 'A';
636 keyshift['\''] = '"';
640 keyshift['\\'] = '|';
642 menubound[K_ESCAPE] = true;
643 for (i=0 ; i<12 ; i++)
644 menubound[K_F1+i] = true;
647 // register our functions
649 Cmd_AddCommand ("bind",Key_Bind_f);
650 Cmd_AddCommand ("unbind",Key_Unbind_f);
651 Cmd_AddCommand ("unbindall",Key_Unbindall_f);
660 Called by the system between frames for both key up and key down events
661 Should NOT be called during an interrupt!
664 void Key_Event (int key, qboolean down)
672 key_repeats[key] = 0;
677 return; // just catching keys for Con_NotifyBox
679 // update auto-repeat status
683 if (key != K_BACKSPACE && key != K_PAUSE && key_repeats[key] > 1)
684 return; // ignore most autorepeats
686 if (key >= 200 && !keybindings[key])
687 Con_Printf ("%s is unbound, hit F4 to set.\n", Key_KeynumToString (key) );
694 // handle escape specialy, so the user can never unbind it
713 Sys_Error ("Bad key_dest");
719 // key up events only generate commands if the game key binding is
720 // a button command (leading + sign). These will occur even in console mode,
721 // to keep the character from continuing an action started before a console
722 // switch. Button commands include the keynum as a parameter, so multiple
723 // downs can be matched with ups
727 kb = keybindings[key];
728 if (kb && kb[0] == '+')
730 sprintf (cmd, "-%s %i\n", kb+1, key);
733 if (keyshift[key] != key)
735 kb = keybindings[keyshift[key]];
736 if (kb && kb[0] == '+')
738 sprintf (cmd, "-%s %i\n", kb+1, key);
746 // during demo playback, most keys bring up the main menu
748 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game)
755 // if not a consolekey, send to the interpreter no matter what mode is
757 if ( (key_dest == key_menu && menubound[key])
758 || (key_dest == key_console && !consolekeys[key])
759 || (key_dest == key_game && ( !con_forcedup || !consolekeys[key] ) ) )
761 kb = keybindings[key];
765 { // button commands add keynum as a parm
766 sprintf (cmd, "%s %i\n", kb, key);
779 return; // other systems only care about key down events
800 Sys_Error ("Bad key_dest");
810 void Key_ClearStates (void)
814 for (i=0 ; i<256 ; i++)