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];
40 int key_count; // incremented every key event
42 char *keybindings[256];
43 qboolean consolekeys[256]; // if true, can't be rebound while in console
44 qboolean menubound[256]; // if true, can't be rebound while in menu
45 int keyshift[256]; // key to map to if shift held down in console
46 int key_repeats[256]; // if > 1, it is autorepeating
47 qboolean keydown[256];
55 keyname_t keynames[] =
61 {"BACKSPACE", K_BACKSPACE},
62 {"UPARROW", K_UPARROW},
63 {"DOWNARROW", K_DOWNARROW},
64 {"LEFTARROW", K_LEFTARROW},
65 {"RIGHTARROW", K_RIGHTARROW},
135 {"MWHEELUP", K_MWHEELUP},
136 {"MWHEELDOWN", K_MWHEELDOWN},
138 {"SEMICOLON", ';'}, // because a raw semicolon seperates commands
144 ==============================================================================
146 LINE TYPING INTO THE CONSOLE
148 ==============================================================================
156 Interactive line editing and console scrollback
159 void Key_Console (int key)
165 Cbuf_AddText (key_lines[edit_line]+1); // skip the >
167 Con_Printf ("%s\n",key_lines[edit_line]);
168 edit_line = (edit_line + 1) & 31;
169 history_line = edit_line;
170 key_lines[edit_line][0] = ']';
172 if (cls.state == ca_disconnected)
173 SCR_UpdateScreen (); // force an update, because the command
174 // may take some time
179 { // command completion
180 cmd = Cmd_CompleteCommand (key_lines[edit_line]+1);
182 cmd = Cvar_CompleteVariable (key_lines[edit_line]+1);
185 strcpy (key_lines[edit_line]+1, cmd);
186 key_linepos = strlen(cmd)+1;
187 key_lines[edit_line][key_linepos] = ' ';
189 key_lines[edit_line][key_linepos] = 0;
194 if (key == K_BACKSPACE || key == K_LEFTARROW)
201 if (key == K_UPARROW)
205 history_line = (history_line - 1) & 31;
206 } while (history_line != edit_line
207 && !key_lines[history_line][1]);
208 if (history_line == edit_line)
209 history_line = (edit_line+1)&31;
210 strcpy(key_lines[edit_line], key_lines[history_line]);
211 key_linepos = strlen(key_lines[edit_line]);
215 if (key == K_DOWNARROW)
217 if (history_line == edit_line) return;
220 history_line = (history_line + 1) & 31;
222 while (history_line != edit_line
223 && !key_lines[history_line][1]);
224 if (history_line == edit_line)
226 key_lines[edit_line][0] = ']';
231 strcpy(key_lines[edit_line], key_lines[history_line]);
232 key_linepos = strlen(key_lines[edit_line]);
237 if (key == K_PGUP || key==K_MWHEELUP)
240 if (con_backscroll > con_totallines - (vid.height>>3) - 1)
241 con_backscroll = con_totallines - (vid.height>>3) - 1;
245 if (key == K_PGDN || key==K_MWHEELDOWN)
248 if (con_backscroll < 0)
255 con_backscroll = con_totallines - (vid.height>>3) - 1;
265 if (key < 32 || key > 127)
266 return; // non printable
268 if (key_linepos < MAXCMDLINE-1)
270 key_lines[edit_line][key_linepos] = key;
272 key_lines[edit_line][key_linepos] = 0;
277 //============================================================================
279 // LordHavoc: increased messagemode length (was 32)
280 char chat_buffer[256];
281 qboolean team_message = false;
283 void Key_Message (int key)
285 static int chat_bufferlen = 0;
290 Cbuf_AddText ("say_team \"");
292 Cbuf_AddText ("say \"");
293 Cbuf_AddText(chat_buffer);
294 Cbuf_AddText("\"\n");
310 if (key < 32 || key > 127)
311 return; // non printable
313 if (key == K_BACKSPACE)
318 chat_buffer[chat_bufferlen] = 0;
323 // LordHavoc: increased messagemode length (was 31)
324 if (chat_bufferlen == 255)
327 chat_buffer[chat_bufferlen++] = key;
328 chat_buffer[chat_bufferlen] = 0;
331 //============================================================================
338 Returns a key number to be used to index keybindings[] by looking at
339 the given string. Single ascii characters return themselves, while
340 the K_* names are matched up.
343 int Key_StringToKeynum (char *str)
352 for (kn=keynames ; kn->name ; kn++)
354 if (!Q_strcasecmp(str,kn->name))
364 Returns a string (either a single ascii char, or a K_* name) for the
366 FIXME: handle quote special (general escape sequence?)
369 char *Key_KeynumToString (int keynum)
372 static char tinystr[2];
375 return "<KEY NOT FOUND>";
376 if (keynum > 32 && keynum < 127)
383 for (kn=keynames ; kn->name ; kn++)
384 if (keynum == kn->keynum)
387 return "<UNKNOWN KEYNUM>";
396 void Key_SetBinding (int keynum, char *binding)
405 if (keybindings[keynum])
407 Z_Free (keybindings[keynum]);
408 keybindings[keynum] = NULL;
411 // allocate memory for new binding
412 l = strlen (binding);
413 new = Z_Malloc (l+1);
414 strcpy (new, binding);
416 keybindings[keynum] = new;
424 void Key_Unbind_f (void)
430 Con_Printf ("unbind <key> : remove commands from a key\n");
434 b = Key_StringToKeynum (Cmd_Argv(1));
437 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
441 Key_SetBinding (b, "");
444 void Key_Unbindall_f (void)
448 for (i=0 ; i<256 ; i++)
450 Key_SetBinding (i, "");
459 void Key_Bind_f (void)
466 if (c != 2 && c != 3)
468 Con_Printf ("bind <key> [command] : attach a command to a key\n");
471 b = Key_StringToKeynum (Cmd_Argv(1));
474 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
481 Con_Printf ("\"%s\" = \"%s\"\n", Cmd_Argv(1), keybindings[b] );
483 Con_Printf ("\"%s\" is not bound\n", Cmd_Argv(1) );
487 // copy the rest of the command line
488 cmd[0] = 0; // start out with a null string
489 for (i=2 ; i< c ; i++)
493 strcat (cmd, Cmd_Argv(i));
496 Key_SetBinding (b, cmd);
503 Writes lines containing "bind key value"
506 void Key_WriteBindings (FILE *f)
510 for (i=0 ; i<256 ; i++)
513 fprintf (f, "bind \"%s\" \"%s\"\n", Key_KeynumToString(i), keybindings[i]);
526 for (i=0 ; i<32 ; i++)
528 key_lines[i][0] = ']';
534 // init ascii characters in console mode
536 for (i=32 ; i<128 ; i++)
537 consolekeys[i] = true;
538 consolekeys[K_ENTER] = true;
539 consolekeys[K_TAB] = true;
540 consolekeys[K_LEFTARROW] = true;
541 consolekeys[K_RIGHTARROW] = true;
542 consolekeys[K_UPARROW] = true;
543 consolekeys[K_DOWNARROW] = true;
544 consolekeys[K_BACKSPACE] = true;
545 consolekeys[K_PGUP] = true;
546 consolekeys[K_PGDN] = true;
547 consolekeys[K_SHIFT] = true;
548 consolekeys[K_MWHEELUP] = true;
549 consolekeys[K_MWHEELDOWN] = true;
550 consolekeys['`'] = false;
551 consolekeys['~'] = false;
553 for (i=0 ; i<256 ; i++)
555 for (i='a' ; i<='z' ; i++)
556 keyshift[i] = i - 'a' + 'A';
573 keyshift['\''] = '"';
577 keyshift['\\'] = '|';
579 menubound[K_ESCAPE] = true;
580 for (i=0 ; i<12 ; i++)
581 menubound[K_F1+i] = true;
584 // register our functions
586 Cmd_AddCommand ("bind",Key_Bind_f);
587 Cmd_AddCommand ("unbind",Key_Unbind_f);
588 Cmd_AddCommand ("unbindall",Key_Unbindall_f);
597 Called by the system between frames for both key up and key down events
598 Should NOT be called during an interrupt!
601 void Key_Event (int key, qboolean down)
609 key_repeats[key] = 0;
615 return; // just catching keys for Con_NotifyBox
618 // update auto-repeat status
622 if (key != K_BACKSPACE && key != K_PAUSE && key_repeats[key] > 1)
624 return; // ignore most autorepeats
627 if (key >= 200 && !keybindings[key])
628 Con_Printf ("%s is unbound, hit F4 to set.\n", Key_KeynumToString (key) );
635 // handle escape specialy, so the user can never unbind it
654 Sys_Error ("Bad key_dest");
660 // key up events only generate commands if the game key binding is
661 // a button command (leading + sign). These will occur even in console mode,
662 // to keep the character from continuing an action started before a console
663 // switch. Button commands include the kenum as a parameter, so multiple
664 // downs can be matched with ups
668 kb = keybindings[key];
669 if (kb && kb[0] == '+')
671 sprintf (cmd, "-%s %i\n", kb+1, key);
674 if (keyshift[key] != key)
676 kb = keybindings[keyshift[key]];
677 if (kb && kb[0] == '+')
679 sprintf (cmd, "-%s %i\n", kb+1, key);
687 // during demo playback, most keys bring up the main menu
689 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game)
696 // if not a consolekey, send to the interpreter no matter what mode is
698 if ( (key_dest == key_menu && menubound[key])
699 || (key_dest == key_console && !consolekeys[key])
700 || (key_dest == key_game && ( !con_forcedup || !consolekeys[key] ) ) )
702 kb = keybindings[key];
706 { // button commands add keynum as a parm
707 sprintf (cmd, "%s %i\n", kb, key);
720 return; // other systems only care about key down events
741 Sys_Error ("Bad key_dest");
751 void Key_ClearStates (void)
755 for (i=0 ; i<256 ; i++)