iKeyhandler repariert

This commit is contained in:
Yamagi Burmeister 2009-03-03 08:53:33 +00:00
parent 21378405a0
commit e2517990b7
2 changed files with 136 additions and 55 deletions

View file

@ -26,7 +26,7 @@ key up events are sent even if in console mode
*/ */
#define MAXCMDLINE 256 #define MAXCMDLINE 512
char key_lines[32][MAXCMDLINE]; char key_lines[32][MAXCMDLINE];
int key_linepos; int key_linepos;
int shift_down=false; int shift_down=false;
@ -91,12 +91,6 @@ keyname_t keynames[] =
{"MOUSE4", K_MOUSE4}, {"MOUSE4", K_MOUSE4},
{"MOUSE5", K_MOUSE5}, {"MOUSE5", K_MOUSE5},
{"JOY1", K_JOY1},
{"JOY2", K_JOY2},
{"JOY3", K_JOY3},
{"JOY4", K_JOY4},
{"AUX1", K_AUX1}, {"AUX1", K_AUX1},
{"AUX2", K_AUX2}, {"AUX2", K_AUX2},
{"AUX3", K_AUX3}, {"AUX3", K_AUX3},
@ -179,7 +173,7 @@ void CompleteCommand (void)
{ {
key_lines[edit_line][1] = '/'; key_lines[edit_line][1] = '/';
strcpy (key_lines[edit_line]+2, cmd); strcpy (key_lines[edit_line]+2, cmd);
key_linepos = strlen(cmd)+2; key_linepos = (int)strlen(cmd)+2;
key_lines[edit_line][key_linepos] = ' '; key_lines[edit_line][key_linepos] = ' ';
key_linepos++; key_linepos++;
key_lines[edit_line][key_linepos] = 0; key_lines[edit_line][key_linepos] = 0;
@ -256,9 +250,9 @@ void Key_Console (int key)
strtok( cbd, "\n\r\b" ); strtok( cbd, "\n\r\b" );
i = strlen( cbd ); i = (int)strlen( cbd );
if ( i + key_linepos >= MAXCMDLINE) if ( i + key_linepos >= MAXCMDLINE - 1)
i= MAXCMDLINE - key_linepos; i= MAXCMDLINE - key_linepos - 1;
if ( i > 0 ) if ( i > 0 )
{ {
@ -306,13 +300,36 @@ void Key_Console (int key)
return; return;
} }
if ( ( key == K_BACKSPACE ) || ( key == K_LEFTARROW ) || ( key == K_KP_LEFTARROW ) || ( ( key == 'h' ) && ( keydown[K_CTRL] ) ) ) if ( key == K_LEFTARROW )
{ {
if (key_linepos > 1) if (key_linepos > 1)
key_linepos--; key_linepos--;
return; return;
} }
if (key == K_RIGHTARROW)
{
if (key_lines[edit_line][key_linepos])
key_linepos++;
return;
}
if ( ( key == K_BACKSPACE ) || ( ( key == 'h' ) && ( keydown[K_CTRL] ) ) )
{
if (key_linepos > 1)
{
memmove (key_lines[edit_line] + key_linepos-1, key_lines[edit_line] + key_linepos, sizeof(key_lines[edit_line])-key_linepos);
key_linepos--;
}
return;
}
if ( key == K_DEL )
{
memmove (key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1, sizeof(key_lines[edit_line])-key_linepos-1);
return;
}
if ( ( key == K_UPARROW ) || ( key == K_KP_UPARROW ) || if ( ( key == K_UPARROW ) || ( key == K_KP_UPARROW ) ||
( ( key == 'p' ) && keydown[K_CTRL] ) ) ( ( key == 'p' ) && keydown[K_CTRL] ) )
{ {
@ -324,7 +341,7 @@ void Key_Console (int key)
if (history_line == edit_line) if (history_line == edit_line)
history_line = (edit_line+1)&31; history_line = (edit_line+1)&31;
strcpy(key_lines[edit_line], key_lines[history_line]); strcpy(key_lines[edit_line], key_lines[history_line]);
key_linepos = strlen(key_lines[edit_line]); key_linepos = (int)strlen(key_lines[edit_line]);
return; return;
} }
@ -346,7 +363,7 @@ void Key_Console (int key)
else else
{ {
strcpy(key_lines[edit_line], key_lines[history_line]); strcpy(key_lines[edit_line], key_lines[history_line]);
key_linepos = strlen(key_lines[edit_line]); key_linepos = (int)strlen(key_lines[edit_line]);
} }
return; return;
} }
@ -367,13 +384,19 @@ void Key_Console (int key)
if (key == K_HOME || key == K_KP_HOME ) if (key == K_HOME || key == K_KP_HOME )
{ {
con.display = con.current - con.totallines + 10; if (keydown[K_CTRL])
con.display = con.current - con.totallines + 10;
else
key_linepos = 1;
return; return;
} }
if (key == K_END || key == K_KP_END ) if (key == K_END || key == K_KP_END )
{ {
con.display = con.current; if (keydown[K_CTRL])
con.display = con.current;
else
key_linepos = (int)strlen(key_lines[edit_line]);
return; return;
} }
@ -382,11 +405,24 @@ void Key_Console (int key)
if (key_linepos < MAXCMDLINE-1) if (key_linepos < MAXCMDLINE-1)
{ {
int last;
int length;
length = strlen(key_lines[edit_line]);
if (length >= MAXCMDLINE-1)
return;
last = key_lines[edit_line][key_linepos];
memmove (key_lines[edit_line] + key_linepos+1, key_lines[edit_line] + key_linepos, length - key_linepos);
key_lines[edit_line][key_linepos] = key; key_lines[edit_line][key_linepos] = key;
key_linepos++; key_linepos++;
key_lines[edit_line][key_linepos] = 0;
}
if (!last)
key_lines[edit_line][key_linepos] = 0;
}
} }
//============================================================================ //============================================================================
@ -394,9 +430,11 @@ void Key_Console (int key)
qboolean chat_team; qboolean chat_team;
char chat_buffer[MAXCMDLINE]; char chat_buffer[MAXCMDLINE];
int chat_bufferlen = 0; int chat_bufferlen = 0;
int chat_cursorpos = 0;
void Key_Message (int key) void Key_Message (int key)
{ {
char last;
if ( key == K_ENTER || key == K_KP_ENTER ) if ( key == K_ENTER || key == K_KP_ENTER )
{ {
@ -410,35 +448,85 @@ void Key_Message (int key)
cls.key_dest = key_game; cls.key_dest = key_game;
chat_bufferlen = 0; chat_bufferlen = 0;
chat_buffer[0] = 0; chat_buffer[0] = 0;
chat_cursorpos = 0;
return; return;
} }
if (key == K_ESCAPE) if (key == K_ESCAPE)
{ {
cls.key_dest = key_game; cls.key_dest = key_game;
chat_cursorpos = 0;
chat_bufferlen = 0; chat_bufferlen = 0;
chat_buffer[0] = 0; chat_buffer[0] = 0;
return; return;
} }
if (key == K_BACKSPACE)
{
if (chat_cursorpos)
{
memmove (chat_buffer + chat_cursorpos - 1, chat_buffer + chat_cursorpos, chat_bufferlen - chat_cursorpos + 1);
chat_cursorpos--;
chat_bufferlen--;
}
return;
}
if (key == K_DEL)
{
if (chat_bufferlen && chat_cursorpos != chat_bufferlen)
{
memmove (chat_buffer + chat_cursorpos, chat_buffer + chat_cursorpos + 1, chat_bufferlen - chat_cursorpos + 1);
chat_bufferlen--;
}
return;
}
if (key == K_LEFTARROW)
{
if (chat_cursorpos > 0)
chat_cursorpos--;
return;
}
if (key == K_HOME)
{
chat_cursorpos = 0;
return;
}
if (key == K_END)
{
chat_cursorpos = chat_bufferlen;
return;
}
if (key == K_RIGHTARROW)
{
if (chat_buffer[chat_cursorpos])
chat_cursorpos++;
return;
}
if (key < 32 || key > 127) if (key < 32 || key > 127)
return; // non printable return; // non printable
if (key == K_BACKSPACE)
{
if (chat_bufferlen)
{
chat_bufferlen--;
chat_buffer[chat_bufferlen] = 0;
}
return;
}
if (chat_bufferlen == sizeof(chat_buffer)-1) if (chat_bufferlen == sizeof(chat_buffer)-1)
return; // all full return; // all full
chat_buffer[chat_bufferlen++] = key; memmove (chat_buffer + chat_cursorpos + 1, chat_buffer + chat_cursorpos, chat_bufferlen - chat_cursorpos + 1);
chat_buffer[chat_bufferlen] = 0;
last = chat_buffer[chat_cursorpos];
chat_buffer[chat_cursorpos] = key;
chat_bufferlen++;
chat_cursorpos++;
if (!last)
{
chat_buffer[chat_cursorpos] = 0;
}
} }
//============================================================================ //============================================================================
@ -464,7 +552,7 @@ int Key_StringToKeynum (char *str)
for (kn=keynames ; kn->name ; kn++) for (kn=keynames ; kn->name ; kn++)
{ {
if (!Q_strcasecmp(str,kn->name)) if (!Q_stricmp(str,kn->name))
return kn->keynum; return kn->keynum;
} }
return -1; return -1;
@ -482,14 +570,13 @@ FIXME: handle quote special (general escape sequence?)
char *Key_KeynumToString (int keynum) char *Key_KeynumToString (int keynum)
{ {
keyname_t *kn; keyname_t *kn;
static char tinystr[2]; static char tinystr[2] = {0};
if (keynum == -1) if (keynum == -1)
return "<KEY NOT FOUND>"; return "<KEY NOT FOUND>";
if (keynum > 32 && keynum < 127) if (keynum > 32 && keynum < 127)
{ // printable ascii { // printable ascii
tinystr[0] = keynum; tinystr[0] = keynum;
tinystr[1] = 0;
return tinystr; return tinystr;
} }
@ -514,14 +601,14 @@ void Key_SetBinding (int keynum, char *binding)
if (keynum == -1) if (keynum == -1)
return; return;
// free old bindings // free old bindings
if (keybindings[keynum]) if (keybindings[keynum])
{ {
Z_Free (keybindings[keynum]); Z_Free (keybindings[keynum]);
keybindings[keynum] = NULL; keybindings[keynum] = NULL;
} }
// allocate memory for new binding // allocate memory for new binding
l = strlen (binding); l = strlen (binding);
new = Z_Malloc (l+1); new = Z_Malloc (l+1);
strcpy (new, binding); strcpy (new, binding);
@ -597,7 +684,7 @@ void Key_Bind_f (void)
return; return;
} }
// copy the rest of the command line // copy the rest of the command line
cmd[0] = 0; // start out with a null string cmd[0] = 0; // start out with a null string
for (i=2 ; i< c ; i++) for (i=2 ; i< c ; i++)
{ {
@ -658,9 +745,9 @@ void Key_Init (void)
} }
key_linepos = 1; key_linepos = 1;
// //
// init ascii characters in console mode // init ascii characters in console mode
// //
for (i=32 ; i<128 ; i++) for (i=32 ; i<128 ; i++)
consolekeys[i] = true; consolekeys[i] = true;
consolekeys[K_ENTER] = true; consolekeys[K_ENTER] = true;
@ -694,6 +781,7 @@ void Key_Init (void)
consolekeys['`'] = false; consolekeys['`'] = false;
consolekeys['~'] = false; consolekeys['~'] = false;
consolekeys['^'] = false;
for (i=0 ; i<K_LAST ; i++) for (i=0 ; i<K_LAST ; i++)
keyshift[i] = i; keyshift[i] = i;
@ -725,9 +813,9 @@ void Key_Init (void)
for (i=0 ; i<12 ; i++) for (i=0 ; i<12 ; i++)
menubound[K_F1+i] = true; menubound[K_F1+i] = true;
// //
// register our functions // register our functions
// //
Cmd_AddCommand ("bind",Key_Bind_f); Cmd_AddCommand ("bind",Key_Bind_f);
Cmd_AddCommand ("unbind",Key_Unbind_f); Cmd_AddCommand ("unbind",Key_Unbind_f);
Cmd_AddCommand ("unbindall",Key_Unbindall_f); Cmd_AddCommand ("unbindall",Key_Unbindall_f);
@ -759,7 +847,7 @@ void Key_Event (int key, qboolean down, unsigned time)
if (down) if (down)
{ {
key_repeats[key]++; key_repeats[key]++;
if (key != K_BACKSPACE if (cls.key_dest != key_console && key != K_BACKSPACE && key != K_DEL && key != K_LEFTARROW && key != K_RIGHTARROW
&& key != K_PAUSE && key != K_PAUSE
&& key != K_PGUP && key != K_PGUP
&& key != K_KP_PGUP && key != K_KP_PGUP
@ -780,7 +868,7 @@ void Key_Event (int key, qboolean down, unsigned time)
shift_down = down; shift_down = down;
// console key is hardcoded, so the user can never unbind it // console key is hardcoded, so the user can never unbind it
if (key == '^' || key == '~') if (key == '^' || key == '~' || key == '`')
{ {
if (!down) if (!down)
return; return;
@ -863,9 +951,9 @@ void Key_Event (int key, qboolean down, unsigned time)
return; return;
} }
// //
// if not a consolekey, send to the interpreter no matter what mode is // if not a consolekey, send to the interpreter no matter what mode is
// //
if ( (cls.key_dest == key_menu && menubound[key]) if ( (cls.key_dest == key_menu && menubound[key])
|| (cls.key_dest == key_console && !consolekeys[key]) || (cls.key_dest == key_console && !consolekeys[key])
|| (cls.key_dest == key_game && ( cls.state == ca_active || !consolekeys[key] ) ) ) || (cls.key_dest == key_game && ( cls.state == ca_active || !consolekeys[key] ) ) )

View file

@ -82,14 +82,6 @@ enum QKEYS {
K_MOUSE4 = 241, K_MOUSE4 = 241,
K_MOUSE5 = 242, K_MOUSE5 = 242,
//
// joystick buttons
//
K_JOY1 = 203,
K_JOY2 = 204,
K_JOY3 = 205,
K_JOY4 = 206,
// //
// aux keys are for multi-buttoned joysticks to generate so they can use // aux keys are for multi-buttoned joysticks to generate so they can use
// the normal binding process // the normal binding process
@ -141,6 +133,7 @@ extern int key_repeats[K_LAST];
extern int anykeydown; extern int anykeydown;
extern char chat_buffer[]; extern char chat_buffer[];
extern int chat_bufferlen; extern int chat_bufferlen;
extern int chat_cursorpos;
extern qboolean chat_team; extern qboolean chat_team;
void Key_Event (int key, qboolean down, unsigned time); void Key_Event (int key, qboolean down, unsigned time);