Add arrow key support for win32 console

This commit is contained in:
Xycaleth 2015-01-17 22:11:35 +00:00 committed by Zack Middleton
parent b9e97b3857
commit 2742dfad26

View file

@ -44,6 +44,7 @@ static int qconsole_history_oldest = 0;
static char qconsole_line[ MAX_EDIT_LINE ]; static char qconsole_line[ MAX_EDIT_LINE ];
static int qconsole_linelen = 0; static int qconsole_linelen = 0;
static qboolean qconsole_drawinput = qtrue; static qboolean qconsole_drawinput = qtrue;
static int qconsole_cursor;
static HANDLE qconsole_hout; static HANDLE qconsole_hout;
static HANDLE qconsole_hin; static HANDLE qconsole_hin;
@ -139,6 +140,7 @@ static void CON_HistPrev( void )
Q_strncpyz( qconsole_line, qconsole_history[ qconsole_history_pos ], Q_strncpyz( qconsole_line, qconsole_history[ qconsole_history_pos ],
sizeof( qconsole_line ) ); sizeof( qconsole_line ) );
qconsole_linelen = strlen( qconsole_line ); qconsole_linelen = strlen( qconsole_line );
qconsole_cursor = qconsole_linelen;
} }
/* /*
@ -163,6 +165,7 @@ static void CON_HistNext( void )
qconsole_history_pos = pos; qconsole_history_pos = pos;
qconsole_line[ 0 ] = '\0'; qconsole_line[ 0 ] = '\0';
qconsole_linelen = 0; qconsole_linelen = 0;
qconsole_cursor = qconsole_linelen;
return; return;
} }
@ -170,6 +173,7 @@ static void CON_HistNext( void )
Q_strncpyz( qconsole_line, qconsole_history[ qconsole_history_pos ], Q_strncpyz( qconsole_line, qconsole_history[ qconsole_history_pos ],
sizeof( qconsole_line ) ); sizeof( qconsole_line ) );
qconsole_linelen = strlen( qconsole_line ); qconsole_linelen = strlen( qconsole_line );
qconsole_cursor = qconsole_linelen;
} }
@ -233,7 +237,11 @@ static void CON_Show( void )
// set curor position // set curor position
cursorPos.Y = binfo.dwCursorPosition.Y; cursorPos.Y = binfo.dwCursorPosition.Y;
cursorPos.X = qconsole_linelen > binfo.srWindow.Right ? binfo.srWindow.Right : qconsole_linelen; cursorPos.X = qconsole_cursor < qconsole_linelen
? qconsole_cursor
: qconsole_linelen > binfo.srWindow.Right
? binfo.srWindow.Right
: qconsole_linelen;
SetConsoleCursorPosition( qconsole_hout, cursorPos ); SetConsoleCursorPosition( qconsole_hout, cursorPos );
} }
@ -358,6 +366,7 @@ char *CON_Input( void )
if( key == VK_RETURN ) if( key == VK_RETURN )
{ {
newlinepos = i; newlinepos = i;
qconsole_cursor = 0;
break; break;
} }
else if( key == VK_UP ) else if( key == VK_UP )
@ -370,6 +379,34 @@ char *CON_Input( void )
CON_HistNext(); CON_HistNext();
break; break;
} }
else if( key == VK_LEFT )
{
qconsole_cursor--;
if ( qconsole_cursor < 0 )
{
qconsole_cursor = 0;
}
break;
}
else if( key == VK_RIGHT )
{
qconsole_cursor++;
if ( qconsole_cursor > qconsole_linelen )
{
qconsole_cursor = qconsole_linelen;
}
break;
}
else if( key == VK_HOME )
{
qconsole_cursor = 0;
break;
}
else if( key == VK_END )
{
qconsole_cursor = qconsole_linelen;
break;
}
else if( key == VK_TAB ) else if( key == VK_TAB )
{ {
field_t f; field_t f;
@ -380,6 +417,7 @@ char *CON_Input( void )
Q_strncpyz( qconsole_line, f.buffer, Q_strncpyz( qconsole_line, f.buffer,
sizeof( qconsole_line ) ); sizeof( qconsole_line ) );
qconsole_linelen = strlen( qconsole_line ); qconsole_linelen = strlen( qconsole_line );
qconsole_cursor = qconsole_linelen;
break; break;
} }
@ -389,15 +427,33 @@ char *CON_Input( void )
if( key == VK_BACK ) if( key == VK_BACK )
{ {
int pos = ( qconsole_linelen > 0 ) ? if ( qconsole_cursor > 0 )
qconsole_linelen - 1 : 0; {
int newlen = ( qconsole_linelen > 0 ) ? qconsole_linelen - 1 : 0;
if ( qconsole_cursor < qconsole_linelen )
{
memmove( qconsole_line + qconsole_cursor - 1,
qconsole_line + qconsole_cursor,
qconsole_linelen - qconsole_cursor );
}
qconsole_line[ pos ] = '\0'; qconsole_line[ newlen ] = '\0';
qconsole_linelen = pos; qconsole_linelen = newlen;
qconsole_cursor--;
}
} }
else if( c ) else if( c )
{ {
qconsole_line[ qconsole_linelen++ ] = c; if ( qconsole_linelen > qconsole_cursor )
{
memmove( qconsole_line + qconsole_cursor + 1,
qconsole_line + qconsole_cursor,
qconsole_linelen - qconsole_cursor );
}
qconsole_line[ qconsole_cursor++ ] = c;
qconsole_linelen++;
qconsole_line[ qconsole_linelen ] = '\0'; qconsole_line[ qconsole_linelen ] = '\0';
} }
} }