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 // force an update, because the command may take some time
173 if (cls.state == ca_disconnected)
183 // Enhanced command completion
184 // by EvilTypeGuy eviltypeguy@qeradiant.com
185 // Thanks to Fett, Taniwha
186 Con_CompleteCommandLine();
189 // Advanced Console Editing by Radix radix@planetquake.com
190 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
192 // left arrow will just move left one without erasing, backspace will
193 // actually erase charcter
194 if (key == K_LEFTARROW)
201 if (key == K_BACKSPACE) // delete char before cursor
205 strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
211 if (key == K_DEL) // delete char on cursor
213 if (key_linepos < strlen(key_lines[edit_line]))
214 strcpy(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1);
219 // if we're at the end, get one character from previous line,
220 // otherwise just go right one
221 if (key == K_RIGHTARROW)
223 if (strlen(key_lines[edit_line]) == key_linepos)
225 if (strlen(key_lines[(edit_line + 31) & 31]) <= key_linepos)
226 return; // no character to get
228 key_lines[edit_line][key_linepos] = key_lines[(edit_line + 31) & 31][key_linepos];
230 key_lines[edit_line][key_linepos] = 0;
238 if (key == K_INS) // toggle insert mode
244 // End Advanced Console Editing
246 if (key == K_UPARROW)
250 history_line = (history_line - 1) & 31;
251 } while (history_line != edit_line
252 && !key_lines[history_line][1]);
253 if (history_line == edit_line)
254 history_line = (edit_line+1)&31;
255 strcpy(key_lines[edit_line], key_lines[history_line]);
256 key_linepos = strlen(key_lines[edit_line]);
260 if (key == K_DOWNARROW)
262 if (history_line == edit_line) return;
265 history_line = (history_line + 1) & 31;
267 while (history_line != edit_line
268 && !key_lines[history_line][1]);
269 if (history_line == edit_line)
271 key_lines[edit_line][0] = ']';
276 strcpy(key_lines[edit_line], key_lines[history_line]);
277 key_linepos = strlen(key_lines[edit_line]);
282 if (key == K_PGUP || key==K_MWHEELUP)
285 if (con_backscroll > con_totallines - (vid.conheight>>3) - 1)
286 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
290 if (key == K_PGDN || key==K_MWHEELDOWN)
293 if (con_backscroll < 0)
300 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
310 if (key < 32 || key > 127)
311 return; // non printable
315 if (key_linepos < MAXCMDLINE-1)
319 if (key_insert) // check insert mode
321 // can't do strcpy to move string to right
322 i = strlen(key_lines[edit_line]) - 1;
327 for (; i >= key_linepos; i--)
328 key_lines[edit_line][i + 1] = key_lines[edit_line][i];
331 // only null terminate if at the end
332 i = key_lines[edit_line][key_linepos];
333 key_lines[edit_line][key_linepos] = key;
337 key_lines[edit_line][key_linepos] = 0;
341 //============================================================================
343 // LordHavoc: increased messagemode length (was 32)
344 char chat_buffer[256];
345 qboolean team_message = false;
347 void Key_Message (int key)
349 static int chat_bufferlen = 0;
354 Cbuf_AddText ("say_team \"");
356 Cbuf_AddText ("say \"");
357 Cbuf_AddText(chat_buffer);
358 Cbuf_AddText("\"\n");
374 if (key < 32 || key > 127)
375 return; // non printable
377 if (key == K_BACKSPACE)
382 chat_buffer[chat_bufferlen] = 0;
387 // LordHavoc: increased messagemode length (was 31)
388 if (chat_bufferlen == 240)
391 chat_buffer[chat_bufferlen++] = key;
392 chat_buffer[chat_bufferlen] = 0;
395 //============================================================================
402 Returns a key number to be used to index keybindings[] by looking at
403 the given string. Single ascii characters return themselves, while
404 the K_* names are matched up.
407 int Key_StringToKeynum (char *str)
416 for (kn=keynames ; kn->name ; kn++)
418 if (!Q_strcasecmp(str,kn->name))
428 Returns a string (either a single ascii char, or a K_* name) for the
430 FIXME: handle quote special (general escape sequence?)
433 char *Key_KeynumToString (int keynum)
436 static char tinystr[2];
439 return "<KEY NOT FOUND>";
440 if (keynum > 32 && keynum < 127)
447 for (kn=keynames ; kn->name ; kn++)
448 if (keynum == kn->keynum)
451 return "<UNKNOWN KEYNUM>";
460 void Key_SetBinding (int keynum, char *binding)
469 if (keybindings[keynum])
471 Z_Free (keybindings[keynum]);
472 keybindings[keynum] = NULL;
475 // allocate memory for new binding
476 l = strlen (binding);
477 new = Z_Malloc (l+1);
478 strcpy (new, binding);
480 keybindings[keynum] = new;
488 void Key_Unbind_f (void)
494 Con_Printf ("unbind <key> : remove commands from a key\n");
498 b = Key_StringToKeynum (Cmd_Argv(1));
501 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
505 Key_SetBinding (b, "");
508 void Key_Unbindall_f (void)
512 for (i=0 ; i<256 ; i++)
514 Key_SetBinding (i, "");
523 void Key_Bind_f (void)
530 if (c != 2 && c != 3)
532 Con_Printf ("bind <key> [command] : attach a command to a key\n");
535 b = Key_StringToKeynum (Cmd_Argv(1));
538 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
545 Con_Printf ("\"%s\" = \"%s\"\n", Cmd_Argv(1), keybindings[b] );
547 Con_Printf ("\"%s\" is not bound\n", Cmd_Argv(1) );
551 // copy the rest of the command line
552 cmd[0] = 0; // start out with a null string
553 for (i=2 ; i< c ; i++)
557 strcat (cmd, Cmd_Argv(i));
560 Key_SetBinding (b, cmd);
567 Writes lines containing "bind key value"
570 void Key_WriteBindings (QFile *f)
574 for (i=0 ; i<256 ; i++)
577 Qprintf (f, "bind \"%s\" \"%s\"\n", Key_KeynumToString(i), keybindings[i]);
590 // LordHavoc: clear keybindings array so bounds checking won't freak
591 for (i = 0;i < 256;i++)
592 keybindings[i] = NULL;
594 for (i=0 ; i<32 ; i++)
596 key_lines[i][0] = ']';
602 // init ascii characters in console mode
604 for (i=32 ; i<128 ; i++)
605 consolekeys[i] = true;
606 consolekeys[K_ENTER] = true;
607 consolekeys[K_TAB] = true;
608 consolekeys[K_LEFTARROW] = true;
609 consolekeys[K_RIGHTARROW] = true;
610 consolekeys[K_UPARROW] = true;
611 consolekeys[K_DOWNARROW] = true;
612 consolekeys[K_BACKSPACE] = true;
613 consolekeys[K_DEL] = true;
614 consolekeys[K_INS] = true;
615 consolekeys[K_PGUP] = true;
616 consolekeys[K_PGDN] = true;
617 consolekeys[K_SHIFT] = true;
618 consolekeys[K_MWHEELUP] = true;
619 consolekeys[K_MWHEELDOWN] = true;
620 consolekeys['`'] = false;
621 consolekeys['~'] = false;
623 for (i=0 ; i<256 ; i++)
625 for (i='a' ; i<='z' ; i++)
626 keyshift[i] = i - 'a' + 'A';
643 keyshift['\''] = '"';
647 keyshift['\\'] = '|';
649 menubound[K_ESCAPE] = true;
650 for (i=0 ; i<12 ; i++)
651 menubound[K_F1+i] = true;
654 // register our functions
656 Cmd_AddCommand ("bind",Key_Bind_f);
657 Cmd_AddCommand ("unbind",Key_Unbind_f);
658 Cmd_AddCommand ("unbindall",Key_Unbindall_f);
665 Called by the system between frames for both key up and key down events
666 Should NOT be called during an interrupt!
669 void Key_Event (int key, qboolean down)
677 key_repeats[key] = 0;
682 return; // just catching keys for Con_NotifyBox
684 // update auto-repeat status
688 if (key != K_BACKSPACE && key != K_PAUSE && key_repeats[key] > 1)
689 return; // ignore most autorepeats
691 if (key >= 200 && !keybindings[key])
692 Con_Printf ("%s is unbound, hit F4 to set.\n", Key_KeynumToString (key) );
699 // handle escape specialy, so the user can never unbind it
718 Sys_Error ("Bad key_dest");
724 // key up events only generate commands if the game key binding is
725 // a button command (leading + sign). These will occur even in console mode,
726 // to keep the character from continuing an action started before a console
727 // switch. Button commands include the keynum as a parameter, so multiple
728 // downs can be matched with ups
732 kb = keybindings[key];
733 if (kb && kb[0] == '+')
735 sprintf (cmd, "-%s %i\n", kb+1, key);
738 if (keyshift[key] != key)
740 kb = keybindings[keyshift[key]];
741 if (kb && kb[0] == '+')
743 sprintf (cmd, "-%s %i\n", kb+1, key);
751 // during demo playback, most keys bring up the main menu
753 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game)
760 // if not a consolekey, send to the interpreter no matter what mode is
762 if ( (key_dest == key_menu && menubound[key])
763 || (key_dest == key_console && !consolekeys[key])
764 || (key_dest == key_game && ( !con_forcedup || !consolekeys[key] ) ) )
766 kb = keybindings[key];
770 { // button commands add keynum as a parm
771 sprintf (cmd, "%s %i\n", kb, key);
784 return; // other systems only care about key down events
805 Sys_Error ("Bad key_dest");
815 void Key_ClearStates (void)
819 for (i=0 ; i<256 ; i++)