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)
39 int key_consoleactive;
42 int key_count; // incremented every key event
44 char *keybindings[256];
45 qboolean consolekeys[256]; // if true, can't be rebound while in console
46 qboolean menubound[256]; // if true, can't be rebound while in menu
47 int keyshift[256]; // key to map to if shift held down in console
48 int key_repeats[256]; // if > 1, it is autorepeating
49 qboolean keydown[256];
57 keyname_t keynames[] =
63 {"BACKSPACE", K_BACKSPACE},
64 {"UPARROW", K_UPARROW},
65 {"DOWNARROW", K_DOWNARROW},
66 {"LEFTARROW", K_LEFTARROW},
67 {"RIGHTARROW", K_RIGHTARROW},
137 {"MWHEELUP", K_MWHEELUP},
138 {"MWHEELDOWN", K_MWHEELDOWN},
140 {"SEMICOLON", ';'}, // because a raw semicolon seperates commands
146 ==============================================================================
148 LINE TYPING INTO THE CONSOLE
150 ==============================================================================
158 Interactive line editing and console scrollback
161 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] = ']';
171 key_lines[edit_line][1] = 0; // EvilTypeGuy: null terminate
173 // force an update, because the command may take some time
174 if (cls.state == ca_disconnected)
184 // Enhanced command completion
185 // by EvilTypeGuy eviltypeguy@qeradiant.com
186 // Thanks to Fett, Taniwha
187 Con_CompleteCommandLine();
190 // Advanced Console Editing by Radix radix@planetquake.com
191 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
193 // left arrow will just move left one without erasing, backspace will
194 // actually erase charcter
195 if (key == K_LEFTARROW)
202 if (key == K_BACKSPACE) // delete char before cursor
206 strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
212 if (key == K_DEL) // delete char on cursor
214 if (key_linepos < strlen(key_lines[edit_line]))
215 strcpy(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1);
220 // if we're at the end, get one character from previous line,
221 // otherwise just go right one
222 if (key == K_RIGHTARROW)
224 if (strlen(key_lines[edit_line]) == key_linepos)
226 if (strlen(key_lines[(edit_line + 31) & 31]) <= key_linepos)
227 return; // no character to get
229 key_lines[edit_line][key_linepos] = key_lines[(edit_line + 31) & 31][key_linepos];
231 key_lines[edit_line][key_linepos] = 0;
239 if (key == K_INS) // toggle insert mode
245 // End Advanced Console Editing
247 if (key == K_UPARROW)
251 history_line = (history_line - 1) & 31;
252 } while (history_line != edit_line
253 && !key_lines[history_line][1]);
254 if (history_line == edit_line)
255 history_line = (edit_line+1)&31;
256 strcpy(key_lines[edit_line], key_lines[history_line]);
257 key_linepos = strlen(key_lines[edit_line]);
261 if (key == K_DOWNARROW)
263 if (history_line == edit_line) return;
266 history_line = (history_line + 1) & 31;
268 while (history_line != edit_line
269 && !key_lines[history_line][1]);
270 if (history_line == edit_line)
272 key_lines[edit_line][0] = ']';
277 strcpy(key_lines[edit_line], key_lines[history_line]);
278 key_linepos = strlen(key_lines[edit_line]);
283 if (key == K_PGUP || key==K_MWHEELUP)
285 con_backscroll += ((int) scr_conlines >> 4);
286 if (con_backscroll > con_totallines - (vid.conheight>>3) - 1)
287 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
291 if (key == K_PGDN || key==K_MWHEELDOWN)
293 con_backscroll -= ((int) scr_conlines >> 4);
294 if (con_backscroll < 0)
301 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
311 if (key < 32 || key > 127)
312 return; // non printable
316 if (key_linepos < MAXCMDLINE-1)
320 if (key_insert) // check insert mode
322 // can't do strcpy to move string to right
323 i = strlen(key_lines[edit_line]) - 1;
328 for (; i >= key_linepos; i--)
329 key_lines[edit_line][i + 1] = key_lines[edit_line][i];
332 // only null terminate if at the end
333 i = key_lines[edit_line][key_linepos];
334 key_lines[edit_line][key_linepos] = key;
338 key_lines[edit_line][key_linepos] = 0;
342 //============================================================================
344 // LordHavoc: increased messagemode length (was 32)
345 char chat_buffer[256];
346 qboolean team_message = false;
348 void Key_Message (int key)
350 static int chat_bufferlen = 0;
355 Cbuf_AddText ("say_team \"");
357 Cbuf_AddText ("say \"");
358 Cbuf_AddText(chat_buffer);
359 Cbuf_AddText("\"\n");
375 if (key < 32 || key > 127)
376 return; // non printable
378 if (key == K_BACKSPACE)
383 chat_buffer[chat_bufferlen] = 0;
388 // LordHavoc: increased messagemode length (was 31)
389 if (chat_bufferlen == 240)
392 chat_buffer[chat_bufferlen++] = key;
393 chat_buffer[chat_bufferlen] = 0;
396 //============================================================================
403 Returns a key number to be used to index keybindings[] by looking at
404 the given string. Single ascii characters return themselves, while
405 the K_* names are matched up.
408 int Key_StringToKeynum (const char *str)
417 for (kn=keynames ; kn->name ; kn++)
418 if (!Q_strcasecmp(str,kn->name))
427 Returns a string (either a single ascii char, or a K_* name) for the
429 FIXME: handle quote special (general escape sequence?)
432 char *Key_KeynumToString (int keynum)
435 static char tinystr[2];
438 return "<KEY NOT FOUND>";
439 if (keynum > 32 && keynum < 127)
446 for (kn=keynames ; kn->name ; kn++)
447 if (keynum == kn->keynum)
450 return "<UNKNOWN KEYNUM>";
459 void Key_SetBinding (int keynum, char *binding)
464 if (keynum < 0 || keynum >= 256)
468 if (keybindings[keynum])
470 Z_Free (keybindings[keynum]);
471 keybindings[keynum] = NULL;
474 // allocate memory for new binding
475 l = strlen (binding);
476 new = Z_Malloc (l+1);
477 strcpy (new, binding);
479 keybindings[keynum] = new;
487 void Key_Unbind_f (void)
493 Con_Printf ("unbind <key> : remove commands from a key\n");
497 b = Key_StringToKeynum (Cmd_Argv(1));
500 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
504 Key_SetBinding (b, "");
507 void Key_Unbindall_f (void)
511 for (i=0 ; i<256 ; i++)
513 Key_SetBinding (i, "");
522 void Key_Bind_f (void)
529 if (c != 2 && c != 3)
531 Con_Printf ("bind <key> [command] : attach a command to a key\n");
534 b = Key_StringToKeynum (Cmd_Argv(1));
537 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
544 Con_Printf ("\"%s\" = \"%s\"\n", Cmd_Argv(1), keybindings[b] );
546 Con_Printf ("\"%s\" is not bound\n", Cmd_Argv(1) );
550 // copy the rest of the command line
551 cmd[0] = 0; // start out with a null string
552 for (i=2 ; i< c ; i++)
556 strcat (cmd, Cmd_Argv(i));
559 Key_SetBinding (b, cmd);
566 Writes lines containing "bind key value"
569 void Key_WriteBindings (QFile *f)
573 for (i=0 ; i<256 ; i++)
576 Qprintf (f, "bind \"%s\" \"%s\"\n", Key_KeynumToString(i), keybindings[i]);
589 // LordHavoc: clear keybindings array so bounds checking won't freak
590 for (i = 0;i < 256;i++)
591 keybindings[i] = NULL;
593 for (i=0 ; i<32 ; i++)
595 key_lines[i][0] = ']';
601 // init ascii characters in console mode
603 for (i=32 ; i<128 ; i++)
604 consolekeys[i] = true;
605 consolekeys[K_ENTER] = true;
606 consolekeys[K_TAB] = true;
607 consolekeys[K_LEFTARROW] = true;
608 consolekeys[K_RIGHTARROW] = true;
609 consolekeys[K_UPARROW] = true;
610 consolekeys[K_DOWNARROW] = true;
611 consolekeys[K_BACKSPACE] = true;
612 consolekeys[K_DEL] = true;
613 consolekeys[K_INS] = true;
614 consolekeys[K_PGUP] = true;
615 consolekeys[K_PGDN] = true;
616 consolekeys[K_SHIFT] = true;
617 consolekeys[K_MWHEELUP] = true;
618 consolekeys[K_MWHEELDOWN] = true;
619 consolekeys['`'] = false;
620 consolekeys['~'] = false;
622 for (i=0 ; i<256 ; i++)
624 for (i='a' ; i<='z' ; i++)
625 keyshift[i] = i - 'a' + 'A';
642 keyshift['\''] = '"';
646 keyshift['\\'] = '|';
648 menubound[K_ESCAPE] = true;
649 for (i=0 ; i<12 ; i++)
650 menubound[K_F1+i] = true;
653 // register our functions
655 Cmd_AddCommand ("bind",Key_Bind_f);
656 Cmd_AddCommand ("unbind",Key_Unbind_f);
657 Cmd_AddCommand ("unbindall",Key_Unbindall_f);
664 Called by the system between frames for both key up and key down events
665 Should NOT be called during an interrupt!
668 void Key_Event (int key, qboolean down)
676 key_repeats[key] = 0;
681 return; // just catching keys for Con_NotifyBox
683 // update auto-repeat status
687 if (key != K_BACKSPACE && key != K_PAUSE && key_repeats[key] > 1)
688 return; // ignore most autorepeats
690 if (key >= 200 && !keybindings[key])
691 Con_Printf ("%s is unbound, hit F4 to set.\n", Key_KeynumToString (key) );
698 // handle escape specialy, so the user can never unbind it
716 Sys_Error ("Bad key_dest");
721 // LordHavoc: hack to make toggleconsole always work
724 kb = keybindings[key];
725 if (kb && !strncmp(kb, "toggleconsole", strlen("toggleconsole")))
733 if (key_consoleactive && consolekeys[key])
735 // console only wants key down events
739 // FIXME: this does not support non-QWERTY keyboards
748 // key up events only generate commands if the game key binding is
749 // a button command (leading + sign). These will occur even in console mode,
750 // to keep the character from continuing an action started before a console
751 // switch. Button commands include the keynum as a parameter, so multiple
752 // downs can be matched with ups
756 kb = keybindings[key];
757 if (kb && kb[0] == '+')
759 sprintf (cmd, "-%s %i\n", kb+1, key);
762 if (keyshift[key] != key)
764 kb = keybindings[keyshift[key]];
765 if (kb && kb[0] == '+')
767 sprintf (cmd, "-%s %i\n", kb+1, key);
775 // during demo playback, most keys bring up the main menu
777 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game)
784 // if not a consolekey, send to the interpreter no matter what mode is
786 //if ((key_dest == key_console && !consolekeys[key])
787 if ((key_consoleactive && !consolekeys[key])
788 || (key_dest == key_menu && menubound[key])
789 || key_dest == key_game)
791 kb = keybindings[key];
795 { // button commands add keynum as a parm
796 sprintf (cmd, "%s %i\n", kb, key);
809 return; // other systems only care about key down events
811 // FIXME: this does not support non-QWERTY keyboards
829 Sys_Error ("Bad key_dest");
840 void Key_ClearStates (void)
844 for (i = 0;i < 256;i++)