Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

37.0 KB
Raw Blame
/*
**	Command & Conquer Generals Zero Hour(tm)
**	Copyright 2025 Electronic Arts Inc.
**
**	This program is free software: you can redistribute it and/or modify
**	it under the terms of the GNU General Public License as published by
**	the Free Software Foundation, either version 3 of the License, or
**	(at your option) any later version.
**
**	This program is distributed in the hope that it will be useful,
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**	GNU General Public License for more details.
**
**	You should have received a copy of the GNU General Public License
**	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
// Modified 2025-2026 by Olcay Seygan for Zero Hour Reforged; see the git history.
// Modified 2026 by İlyas Akın for the macOS/Linux port; see NOTICE.md and the git history.

////////////////////////////////////////////////////////////////////////////////
//																																						//
//  (c) 2001-2003 Electronic Arts Inc.																				//
//																																						//
////////////////////////////////////////////////////////////////////////////////

// FILE: Keyboard.cpp /////////////////////////////////////////////////////////////////////////////
// Created:    Colin Day, June 2001
// Desc:       Basic keyboard
///////////////////////////////////////////////////////////////////////////////////////////////////

#include "PreRTS.h"	// This must go first in EVERY cpp file int the GameEngine

/* The language of the active keyboard layout, the low word of its HKL: Keyboard::initKeyNames names
	 the numpad keys the French way on a French layout.  Windows asks GetKeyboardLayout, as always (the
	 HKL is a handle, so it goes through uintptr_t on its way to an integer).  Elsewhere the layout is
	 C3's to read; until then this is 0, no layout language, and the game's own language decides. */
static Int keyboardLayoutLanguage( void )
{
#if defined(_WIN32)
	HKL kLayout = GetKeyboardLayout(0);
	return (Int)((uintptr_t)kLayout & 0xFFFF);
#else
	return 0;
#endif
}
#include "Lib/Clock.h"

#include "Common/Language.h"
#include "Common/GameEngine.h"
#include "Common/MessageStream.h"
#include "GameClient/Keyboard.h"
#include "GameClient/KeyDefs.h"


// PUBLIC DATA ////////////////////////////////////////////////////////////////////////////////////
Keyboard *TheKeyboard = NULL;

///////////////////////////////////////////////////////////////////////////////////////////////////
// PRIVATE PROTOTYPES /////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

//-------------------------------------------------------------------------------------------------
/** Given the state of the device, create messages from the input and
	* place them on the message stream */
//-------------------------------------------------------------------------------------------------
void Keyboard::createStreamMessages( void )
{
	
	// santiy
	if( TheMessageStream == NULL )
		return;

	KeyboardIO *key = getFirstKey();
	GameMessage *msg = NULL;
	while( key->key != KEY_NONE )
	{

		// add message to stream
		if( BitTest( key->state, KEY_STATE_DOWN ) )
		{

			msg = TheMessageStream->appendMessage( GameMessage::MSG_RAW_KEY_DOWN );
			DEBUG_ASSERTCRASH( msg, ("Unable to append key down message to stream\n") );

		}  // end if
		else if( BitTest( key->state, KEY_STATE_UP ) )
		{

			msg = TheMessageStream->appendMessage( GameMessage::MSG_RAW_KEY_UP );
			DEBUG_ASSERTCRASH( msg, ("Unable to append key up message to stream\n") );

		}  // end else if
		else
		{

			DEBUG_CRASH(( "Unknown key state when creating msg stream\n" ));
			
		}  // end else

		// fill out message arguments
		if( msg )
		{
			msg->appendIntegerArgument( key->key );
			msg->appendIntegerArgument( key->state );
		}

		// next key please
		key++;

	}  // end while

}  // end createStreamMessages

//-------------------------------------------------------------------------------------------------
/** update all our key state data */
//-------------------------------------------------------------------------------------------------
void Keyboard::updateKeys( void )
{
	Int index = 0;

	// get latest keys
	do
	{
		do
		{

			// get a single key
			getKey( &m_keys[ index ] );

			//
			// if we get back that our device was lost, we haven't been paying
			// attention to the keyboard, lets start over now, we only need to
			// do this reset once in this loop though just to be efficient
			//
			if( m_keys[ index ].key == KEY_LOST)
			{

				resetKeys();
				index = 0;

			}  // end if

		}
		while( m_keys[ index ].key == KEY_LOST );

		//
		// keep the terminator slot free. This loop had no bound at all against NUM_KEYS, so a
		// burst of more than 256 events in one frame walked the write cursor off m_keys and
		// straight into m_keyStatus, the very next member - silent key-state corruption.
		//
		if( index >= NUM_KEYS - 1 )
		{
			m_keys[ NUM_KEYS - 1 ].key = KEY_NONE;
			break;
		}
	} 
	while( m_keys[ index++ ].key != KEY_NONE );

	// update keyboard status array
	index = 0;
	while( m_keys[ index ].key != KEY_NONE )
	{

		/** @todo -- if we don't have focus, we could destroy all the keys retrieved
		here so that we don't process anything */

		// what this key was last pressed with, read before its new state overwrites it
		const UnsignedShort pressState = m_keyStatus[ m_keys[ index ].key ].state;

		m_keyStatus[ m_keys[ index ].key ].state = m_keys[ index ].state;
		m_keyStatus[ m_keys[ index ].key ].status = m_keys[ index ].status;
		m_keyStatus[ m_keys[ index ].key ].sequence = Clock_Milliseconds();

		// prevent ALT-TAB from causing a TAB event
		if( m_keys[ index ].key == KEY_TAB )
		{
			if( BitTest( m_keyStatus[ KEY_LALT ].state, KEY_STATE_DOWN ) ||
					BitTest( m_keyStatus[ KEY_RALT ].state, KEY_STATE_DOWN ) )
			{
				m_keys[index].status = KeyboardIO::STATUS_USED;
			}
		}
		else if( m_keys[ index ].key == KEY_CAPS	 ||
						 m_keys[ index ].key == KEY_LCTRL  ||
						 m_keys[ index ].key == KEY_RCTRL	 ||
						 m_keys[ index ].key == KEY_LSHIFT ||
						 m_keys[ index ].key == KEY_RSHIFT ||
						 m_keys[ index ].key == KEY_LALT	 ||
						 m_keys[ index ].key == KEY_RALT )

		{

			//
			// this keeps our internal key state accurate event though we don't
			// use the returned translation ... kinda weird I think
			//
			translateKey( m_keys[ index ].key );

		}  // end else if

		//
		// the modifiers held at this point of the batch, not at its end: Ctrl down, F down, Ctrl up
		// read in one frame used to hand the F its press without the Ctrl.  A press keeps them in
		// the key's status, and its release says so, because a player who lets go of Ctrl before F
		// has still pressed Ctrl+F, not F (the command bar's F hotkey fired on that release).
		//
		BitSet( m_keys[ index ].state, m_modifiers );
		if( BitTest( m_keys[ index ].state, KEY_STATE_DOWN ) )
			m_keyStatus[ m_keys[ index ].key ].state = m_keys[ index ].state;
		else if( pressState & ( KEY_STATE_CONTROL | KEY_STATE_ALT ) )
			BitSet( m_keys[ index ].state, KEY_STATE_PRESSED_WITH_CTRL_ALT );

		index++;

	}  // end while

	// check for key repeats
	checkKeyRepeat();

}  // end updateKeys

//-------------------------------------------------------------------------------------------------
/** check key repeat sequences, TRUE is returned if repeat is occurring */
//-------------------------------------------------------------------------------------------------
Bool Keyboard::checkKeyRepeat( void )
{
	Bool retVal = FALSE;
	Int index = 0;
	Int key;

	/** @todo we shouldn't think about repeating any keys while we 
	don't have the focus */
//	if( currentFocus == FOCUS_OUT )
//		return FALSE;

	// Find end of real keys for this frame
	while( m_keys[ index ].key != KEY_NONE )
		index++;

	const UnsignedInt nowMs = Clock_Milliseconds();

	// Scan Keyboard status array for first key down
	// long enough to repeat
	for( key = 0; key < 256; key++ )
	{

		if( BitTest( m_keyStatus[ key ].state, KEY_STATE_DOWN ) )
		{

			if( (nowMs - m_keyStatus[ key ].sequence) > Keyboard::KEY_REPEAT_DELAY_MS )
			{
				// Add key to this frame
				m_keys[ index ].key = (UnsignedByte)key;
				m_keys[ index ].state = KEY_STATE_DOWN | KEY_STATE_AUTOREPEAT | m_modifiers;  // note: not a bitset; this is an assignment
				m_keys[ index ].status = KeyboardIO::STATUS_UNUSED;

				// Set End Flag
				if( index + 1 < NUM_KEYS )
					m_keys[ ++index ].key = KEY_NONE;
			
				// Set all keys as new to prevent multiple keys repeating.
				// (own counter: this loop used to reuse `index`, the live write cursor into
				// m_keys, and only got away with it because of the break below.)
				for( Int resetIndex = 0; resetIndex < NUM_KEYS; resetIndex++ )
					m_keyStatus[ resetIndex ].sequence = nowMs;

				// Set repeated key so it will repeat again one interval from now
				m_keyStatus[ key ].sequence = nowMs - (Keyboard::KEY_REPEAT_DELAY_MS - Keyboard::KEY_REPEAT_INTERVAL_MS);

				retVal = TRUE;
				break;  // exit for key

			}  // end if

		}  // end if, key down

	}  // end for key

	return retVal;

}  // end checkKeyRepeat

//-------------------------------------------------------------------------------------------------
/** Initialize the keyboard key-names array */
//-------------------------------------------------------------------------------------------------
void Keyboard::initKeyNames( void )
{
	Int i;

	#define _set_keyname_(k,s,s2,z) (m_keyNames[z].stdKey = k, m_keyNames[z].shifted = s, m_keyNames[z].shifted2 = s2)

	/*
	 * Initialize the keyboard key-names array.
	 */
	for( i = 0; i < KEY_NAMES_COUNT; i++ )
	{

		m_keyNames[ i ].stdKey =		u'\0';
		m_keyNames[ i ].shifted =		u'\0';
		m_keyNames[ i ].shifted2 =	u'\0';

	}  // end for i

	m_shift2Key = KEY_NONE;

	// generic to all languages
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_UP );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_DOWN );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_LEFT );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_RIGHT );

	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_HOME );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_END );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_PGUP );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_PGDN );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_INS );
	_set_keyname_(u'\b',	u'\b',	u'\0',	KEY_DEL );

	_set_keyname_(u'\b',	u'\b',	u'\0',	KEY_BACKSPACE  );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_ESC  );
	_set_keyname_(u'\t',	u'\t',	u'\0',	KEY_TAB  );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_CAPS  );
	_set_keyname_(u'\n',	u'\n',	u'\0',	KEY_ENTER  );

	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_RALT );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_RCTRL );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_RSHIFT  );

	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_LALT  );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_LCTRL  );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_LSHIFT  );

	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_NUM );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_SCROLL );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_SYSREQ );

	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_F1  );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_F2  );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_F3  );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_F4  );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_F5  );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_F6  );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_F7  );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_F8  );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_F9  );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_F10 );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_F11 );
	_set_keyname_(u'\0',	u'\0',	u'\0',	KEY_F12 );

	_set_keyname_(u'1',		u'1',		u'\0',	KEY_KP1 );
	_set_keyname_(u'2',		u'2',		u'\0',	KEY_KP2 );
	_set_keyname_(u'3',		u'3',		u'\0',	KEY_KP3 );
	_set_keyname_(u'4',		u'4',		u'\0',	KEY_KP4 );
	_set_keyname_(u'5',		u'5',		u'\0',	KEY_KP5 );
	_set_keyname_(u'6',		u'6',		u'\0',	KEY_KP6 );
	_set_keyname_(u'7',		u'7',		u'\0',	KEY_KP7 );
	_set_keyname_(u'8',		u'8',		u'\0',	KEY_KP8 );
	_set_keyname_(u'9',		u'9',		u'\0',	KEY_KP9 );
	_set_keyname_(u'0',		u'0',		u'\0',	KEY_KP0 );

	_set_keyname_(u' ',		u' ',		u'\0',	KEY_SPACE  );

	Int low = keyboardLayoutLanguage();
	LanguageID currentLanguage = OurLanguage;
	if(low == 0x040c
		 || low == 0x080c
		 || low == 0x0c0c
		 || low == 0x100c
		 || low == 0x140c)
		currentLanguage = LANGUAGE_ID_FRENCH;

	switch( currentLanguage )
	{
		case LANGUAGE_ID_US:
		case LANGUAGE_ID_JAPANESE:
		case LANGUAGE_ID_KOREAN:
		case LANGUAGE_ID_JABBER:
		case LANGUAGE_ID_UNKNOWN:
		case LANGUAGE_ID_SPANISH:		// not localized
			_set_keyname_(u'-',				u'-',				u'\0',	KEY_KPMINUS );
			_set_keyname_(u'+',				u'+',				u'\0',	KEY_KPPLUS );
			_set_keyname_(u'\n',			u'\n',			u'\0',	KEY_KPENTER );
			_set_keyname_(u'/',				u'/',				u'\0',	KEY_KPSLASH );
			_set_keyname_(u'.',				u'.',				u'\0',	KEY_KPDEL );
			_set_keyname_(u'*',				u'*',				u'\0',	KEY_KPSTAR );

			_set_keyname_(u'a',				u'A',				u'\0',	KEY_A  );
			_set_keyname_(u'b',				u'B',				u'\0',	KEY_B  );
			_set_keyname_(u'c',				u'C',				u'\0',	KEY_C  );
			_set_keyname_(u'd',				u'D',				u'\0',	KEY_D  );
			_set_keyname_(u'e',				u'E',				u'\0',	KEY_E  );
			_set_keyname_(u'f',				u'F',				u'\0',	KEY_F  );
			_set_keyname_(u'g',				u'G',				u'\0',	KEY_G  );
			_set_keyname_(u'h',				u'H',				u'\0',	KEY_H  );
			_set_keyname_(u'i',				u'I',				u'\0',	KEY_I  );
			_set_keyname_(u'j',				u'J',				u'\0',	KEY_J  );
			_set_keyname_(u'k',				u'K',				u'\0',	KEY_K  );
			_set_keyname_(u'l',				u'L',				u'\0',	KEY_L  );
			_set_keyname_(u'm',				u'M',				u'\0',	KEY_M  );
			_set_keyname_(u'n',				u'N',				u'\0',	KEY_N  );
			_set_keyname_(u'o',				u'O',				u'\0',	KEY_O  );
			_set_keyname_(u'p',				u'P',				u'\0',	KEY_P  );
			_set_keyname_(u'q',				u'Q',				u'\0',	KEY_Q  );
			_set_keyname_(u'r',				u'R',				u'\0',	KEY_R  );
			_set_keyname_(u's',				u'S',				u'\0',	KEY_S  );
			_set_keyname_(u't',				u'T',				u'\0',	KEY_T  );
			_set_keyname_(u'u',				u'U',				u'\0',	KEY_U  );
			_set_keyname_(u'v',				u'V',				u'\0',	KEY_V  );
			_set_keyname_(u'w',				u'W',				u'\0',	KEY_W  );
			_set_keyname_(u'x',				u'X',				u'\0',	KEY_X  );
			_set_keyname_(u'y',				u'Y',				u'\0',	KEY_Y  );
			_set_keyname_(u'z',				u'Z',				u'\0',	KEY_Z  );

			_set_keyname_(u'1',				u'!',				u'\0',	KEY_1  );
			_set_keyname_(u'2',				u'@',				u'\0',	KEY_2  );
			_set_keyname_(u'3',				u'#',				u'\0',	KEY_3  );
			_set_keyname_(u'4',				u'$',				u'\0',	KEY_4  );
			_set_keyname_(u'5',				u'%',				u'\0',	KEY_5  );
			_set_keyname_(u'6',				u'^',				u'\0',	KEY_6  );
			_set_keyname_(u'7',				u'&',				u'\0',	KEY_7  );
			_set_keyname_(u'8',				u'*',				u'\0',	KEY_8  );
			_set_keyname_(u'9',				u'(',				u'\0',	KEY_9  );
			_set_keyname_(u'0',				u')',				u'\0',	KEY_0  );

			_set_keyname_(u',',				u'<',				u'\0',	KEY_COMMA  );
			_set_keyname_(u'.',				u'>',				u'\0',	KEY_PERIOD  );
			_set_keyname_(u'/',				u'?',				u'\0',	KEY_SLASH  );

			_set_keyname_(u'[',				u'{',				u'\0',	KEY_LBRACKET  );
			_set_keyname_(u']',				u'}',				u'\0',	KEY_RBRACKET  );

			_set_keyname_(u';',				u':',				u'\0',	KEY_SEMICOLON  );
			_set_keyname_(u'\'',			u'\"',			u'\0',	KEY_APOSTROPHE  );
			_set_keyname_(u'`',				u'~',				u'\0',	KEY_TICK  );
			_set_keyname_(u'\\',			u'|',				u'\0',	KEY_BACKSLASH  );

			_set_keyname_(u'-',				u'_',				u'\0',	KEY_MINUS  );
			_set_keyname_(u'=',				u'+',				u'\0',	KEY_EQUAL  );

			break;

		case LANGUAGE_ID_UK:
			_set_keyname_(u'-',				u'-',				u'\0',	KEY_KPMINUS );
			_set_keyname_(u'+',				u'+',				u'\0',	KEY_KPPLUS );
			_set_keyname_(u'\n',			u'\n',			u'\0',	KEY_KPENTER );
			_set_keyname_(u'/',				u'/',				u'\0',	KEY_KPSLASH );
			_set_keyname_(u'.',				u'.',				u'\0',	KEY_KPDEL );
			_set_keyname_(u'*',				u'*',				u'\0',	KEY_KPSTAR );

			_set_keyname_(u'a',				u'A',				u'\0',	KEY_A  );
			_set_keyname_(u'b',				u'B',				u'\0',	KEY_B  );
			_set_keyname_(u'c',				u'C',				u'\0',	KEY_C  );
			_set_keyname_(u'd',				u'D',				u'\0',	KEY_D  );
			_set_keyname_(u'e',				u'E',				u'\0',	KEY_E  );
			_set_keyname_(u'f',				u'F',				u'\0',	KEY_F  );
			_set_keyname_(u'g',				u'G',				u'\0',	KEY_G  );
			_set_keyname_(u'h',				u'H',				u'\0',	KEY_H  );
			_set_keyname_(u'i',				u'I',				u'\0',	KEY_I  );
			_set_keyname_(u'j',				u'J',				u'\0',	KEY_J  );
			_set_keyname_(u'k',				u'K',				u'\0',	KEY_K  );
			_set_keyname_(u'l',				u'L',				u'\0',	KEY_L  );
			_set_keyname_(u'm',				u'M',				u'\0',	KEY_M  );
			_set_keyname_(u'n',				u'N',				u'\0',	KEY_N  );
			_set_keyname_(u'o',				u'O',				u'\0',	KEY_O  );
			_set_keyname_(u'p',				u'P',				u'\0',	KEY_P  );
			_set_keyname_(u'q',				u'Q',				u'\0',	KEY_Q  );
			_set_keyname_(u'r',				u'R',				u'\0',	KEY_R  );
			_set_keyname_(u's',				u'S',				u'\0',	KEY_S  );
			_set_keyname_(u't',				u'T',				u'\0',	KEY_T  );
			_set_keyname_(u'u',				u'U',				u'\0',	KEY_U  );
			_set_keyname_(u'v',				u'V',				u'\0',	KEY_V  );
			_set_keyname_(u'w',				u'W',				u'\0',	KEY_W  );
			_set_keyname_(u'x',				u'X',				u'\0',	KEY_X  );
			_set_keyname_(u'y',				u'Y',				u'\0',	KEY_Y  );
			_set_keyname_(u'z',				u'Z',				u'\0',	KEY_Z  );

			_set_keyname_(u'1',				u'!',				u'\0',	KEY_1  );
			_set_keyname_(u'2',				u'\"',			u'\0',	KEY_2  );
			_set_keyname_(u'3',				0x00A3,			u'\0',	KEY_3  );	//�
			_set_keyname_(u'4',				u'$',				u'\u20AC',		KEY_4  );
			_set_keyname_(u'5',				u'%',				u'\0',	KEY_5  );
			_set_keyname_(u'6',				u'^',				u'\0',	KEY_6  );
			_set_keyname_(u'7',				u'&',				u'\0',	KEY_7  );
			_set_keyname_(u'8',				u'*',				u'\0',	KEY_8  );
			_set_keyname_(u'9',				u'(',				u'\0',	KEY_9  );
			_set_keyname_(u'0',				u')',				u'\0',	KEY_0  );

			_set_keyname_(u',',				u'<',				u'\0',	KEY_COMMA  );
			_set_keyname_(u'.',				u'>',				u'\0',	KEY_PERIOD  );
			_set_keyname_(u'/',				u'?',				u'\0',	KEY_SLASH  );

			_set_keyname_(u'[',				u'{',				u'\0',	KEY_LBRACKET  );
			_set_keyname_(u']',				u'}',				u'\0',	KEY_RBRACKET  );

			_set_keyname_(u';',				u':',				u'\0',	KEY_SEMICOLON  );
			_set_keyname_(u'\'',			u'@',				u'\0',	KEY_APOSTROPHE  );
			_set_keyname_(u'`',				0x00AC,			0x00A6,	KEY_TICK  );	//��
			_set_keyname_(u'#',				u'~',				u'\0',	KEY_BACKSLASH  );

			_set_keyname_(u'-',				u'_',				u'\0',	KEY_MINUS  );
			_set_keyname_(u'=',				u'+',				u'\0',	KEY_EQUAL  );

			_set_keyname_(u'\\',			u'|',				u'\0',	KEY_102  );

			m_shift2Key = KEY_RALT;
			break;

		case LANGUAGE_ID_GERMAN:
			_set_keyname_(u'-',				u'-',				u'\0',	KEY_KPMINUS );
			_set_keyname_(u'+',				u'+',				u'\0',	KEY_KPPLUS );
			_set_keyname_(u'\n',			u'\n',			u'\0',	KEY_KPENTER );
			_set_keyname_(u'/',				u'/',				u'\0',	KEY_KPSLASH );
			_set_keyname_(u',',				u',',				u'\0',	KEY_KPDEL );
			_set_keyname_(u'*',				u'*',				u'\0',	KEY_KPSTAR );

			_set_keyname_(u'a',				u'A',				u'\0',	KEY_A  );
			_set_keyname_(u'b',				u'B',				u'\0',	KEY_B  );
			_set_keyname_(u'c',				u'C',				u'\0',	KEY_C  );
			_set_keyname_(u'd',				u'D',				u'\0',	KEY_D  );
			_set_keyname_(u'e',				u'E',				u'\0',	KEY_E  );
			_set_keyname_(u'f',				u'F',				u'\0',	KEY_F  );
			_set_keyname_(u'g',				u'G',				u'\0',	KEY_G  );
			_set_keyname_(u'h',				u'H',				u'\0',	KEY_H  );
			_set_keyname_(u'i',				u'I',				u'\0',	KEY_I  );
			_set_keyname_(u'j',				u'J',				u'\0',	KEY_J  );
			_set_keyname_(u'k',				u'K',				u'\0',	KEY_K  );
			_set_keyname_(u'l',				u'L',				u'\0',	KEY_L  );
			_set_keyname_(u'm',				u'M',				0x00B5,	KEY_M  );	//�
			_set_keyname_(u'n',				u'N',				u'\0',	KEY_N  );
			_set_keyname_(u'o',				u'O',				u'\0',	KEY_O  );
			_set_keyname_(u'p',				u'P',				u'\0',	KEY_P  );
			_set_keyname_(u'q',				u'Q',				u'@',		KEY_Q  );
			_set_keyname_(u'r',				u'R',				u'\0',	KEY_R  );
			_set_keyname_(u's',				u'S',				u'\0',	KEY_S  );
			_set_keyname_(u't',				u'T',				u'\0',	KEY_T  );
			_set_keyname_(u'u',				u'U',				u'\0',	KEY_U  );
			_set_keyname_(u'v',				u'V',				u'\0',	KEY_V  );
			_set_keyname_(u'w',				u'W',				u'\0',	KEY_W  );
			_set_keyname_(u'x',				u'X',				u'\0',	KEY_X  );
			_set_keyname_(u'z',				u'Z',				u'\0',	KEY_Y  );
			_set_keyname_(u'y',				u'Y',				u'\0',	KEY_Z  );

			_set_keyname_(u'1',				u'!',				u'\0',	KEY_1  );
			_set_keyname_(u'2',				u'"',				0x00B2,	KEY_2  );	//�
			_set_keyname_(u'3',				0x00A7,			0x00B3,	KEY_3  );	//��
			_set_keyname_(u'4',				u'$',				u'\0',	KEY_4  );
			_set_keyname_(u'5',				u'%',				u'\0',	KEY_5  );
			_set_keyname_(u'6',				u'&',				u'\0',	KEY_6  );
			_set_keyname_(u'7',				u'/',				u'{',		KEY_7  );
			_set_keyname_(u'8',				u'(',				u'[',		KEY_8  );
			_set_keyname_(u'9',				u')',				u']',		KEY_9  );
			_set_keyname_(u'0',				u'=',				u'}',		KEY_0  );

			_set_keyname_(u',',				u';',				u'\0',	KEY_COMMA  );
			_set_keyname_(u'.',				u':',				u'\0',	KEY_PERIOD  );
			_set_keyname_(u'-',				u'_',				u'\0',	KEY_SLASH  );

			_set_keyname_(0x00FC,			0x00DC,			u'\0',	KEY_LBRACKET  );		//��
			_set_keyname_(u'+',				u'*',				u'~',		KEY_RBRACKET  );

			_set_keyname_(0x00F6,			0x00D6,			u'\0',	KEY_SEMICOLON  );		//��
			_set_keyname_(0x00E4,			0x00C4,			u'\0',	KEY_APOSTROPHE  );	//��
			_set_keyname_(u'^',				0x00B0,			u'\0',	KEY_TICK  );				//�
			_set_keyname_(u'#',				u'\'',			u'\0',	KEY_BACKSLASH  );

			_set_keyname_(0x00DF,			u'?',				u'\\',	KEY_MINUS  );				//�
			_set_keyname_(0x00B4,			u'`',				u'\0',	KEY_EQUAL  );				//�

			_set_keyname_(u'<',				u'>',				u'|',		KEY_102  );

			m_shift2Key = KEY_RALT;
			break;

		case LANGUAGE_ID_FRENCH:
			_set_keyname_(u'-',				u'-',				u'\0',	KEY_KPMINUS );
			_set_keyname_(u'+',				u'+',				u'\0',	KEY_KPPLUS );
			_set_keyname_(u'\n',			u'\n',			u'\0',	KEY_KPENTER );
			_set_keyname_(u'/',				u'/',				u'\0',	KEY_KPSLASH );
			_set_keyname_(u'.',				u'.',				u'\0',	KEY_KPDEL );
			_set_keyname_(u'*',				u'*',				u'\0',	KEY_KPSTAR );

			_set_keyname_(u'q',				u'Q',				u'\0',	KEY_A  );
			_set_keyname_(u'b',				u'B',				u'\0',	KEY_B  );
			_set_keyname_(u'c',				u'C',				u'\0',	KEY_C  );
			_set_keyname_(u'd',				u'D',				u'\0',	KEY_D  );
			_set_keyname_(u'e',				u'E',				u'\0',	KEY_E  );
			_set_keyname_(u'f',				u'F',				u'\0',	KEY_F  );
			_set_keyname_(u'g',				u'G',				u'\0',	KEY_G  );
			_set_keyname_(u'h',				u'H',				u'\0',	KEY_H  );
			_set_keyname_(u'i',				u'I',				u'\0',	KEY_I  );
			_set_keyname_(u'j',				u'J',				u'\0',	KEY_J  );
			_set_keyname_(u'k',				u'K',				u'\0',	KEY_K  );
			_set_keyname_(u'l',				u'L',				u'\0',	KEY_L  );
			_set_keyname_(u',',				u'?',				u'\0',	KEY_M  );
			_set_keyname_(u'n',				u'N',				u'\0',	KEY_N  );
			_set_keyname_(u'o',				u'O',				u'\0',	KEY_O  );
			_set_keyname_(u'p',				u'P',				u'\0',	KEY_P  );
			_set_keyname_(u'a',				u'A',				u'\0',	KEY_Q  );
			_set_keyname_(u'r',				u'R',				u'\0',	KEY_R  );
			_set_keyname_(u's',				u'S',				u'\0',	KEY_S  );
			_set_keyname_(u't',				u'T',				u'\0',	KEY_T  );
			_set_keyname_(u'u',				u'U',				u'\0',	KEY_U  );
			_set_keyname_(u'v',				u'V',				u'\0',	KEY_V  );
			_set_keyname_(u'z',				u'Z',				u'\0',	KEY_W  );
			_set_keyname_(u'x',				u'X',				u'\0',	KEY_X  );
			_set_keyname_(u'y',				u'Y',				u'\0',	KEY_Y  );
			_set_keyname_(u'w',				u'W',				u'\0',	KEY_Z  );

			_set_keyname_(u'&',				u'1',				u'\0',	KEY_1  );
			_set_keyname_(0x00E9,			u'2',				u'~',		KEY_2  );	//�
			_set_keyname_(u'"',				u'3',				u'#',		KEY_3  );
			_set_keyname_(u'\'',			u'4',				u'{',		KEY_4  );
			_set_keyname_(u'(',				u'5',				u'[',		KEY_5  );
			_set_keyname_(u'-',				u'6',				u'|',		KEY_6  );
			_set_keyname_(0x00E8,			u'7',				u'`',		KEY_7  );	//�
			_set_keyname_(u'_',				u'8',				u'\\',	KEY_8  );
			_set_keyname_(0x00E7,			u'9',				u'\0',	KEY_9  );	//�
			_set_keyname_(0x00E0,			u'0',				u'@',		KEY_0  );	//�

			_set_keyname_(u';',				u'.',				u'\0',	KEY_COMMA  );
			_set_keyname_(u':',				u'/',				u'\0',	KEY_PERIOD  );
			_set_keyname_(u'!',				0x00A7,			u'\0',	KEY_SLASH  );				//�

			_set_keyname_(u'^',				0x00A8,			u'\0',	KEY_LBRACKET  );		//�
			_set_keyname_(u'$',				0x00A3,			0x00A4,	KEY_RBRACKET  );		//��

			_set_keyname_(u'm',				u'M',				u'\0',	KEY_SEMICOLON  );
			_set_keyname_(0x00F9,			u'%',				u'\0',	KEY_APOSTROPHE  );	//�
			_set_keyname_(0x00B2,			u'\0',			u'\0',	KEY_TICK  );				//�
			_set_keyname_(u'*',				0x00B5,			u'\0',	KEY_BACKSLASH  );		//�

			_set_keyname_(u')',				0x00B0,			u']',		KEY_MINUS  );				//�
			_set_keyname_(u'=',				u'+',				u'}',		KEY_EQUAL  );

			_set_keyname_(u'<',				u'>',				u'\0',	KEY_102  );

			m_shift2Key = KEY_RALT;
			break;

		case LANGUAGE_ID_ITALIAN:
			_set_keyname_(u'-',				u'-',				u'\0',	KEY_KPMINUS );
			_set_keyname_(u'+',				u'+',				u'\0',	KEY_KPPLUS );
			_set_keyname_(u'\n',			u'\n',			u'\0',	KEY_KPENTER );
			_set_keyname_(u'/',				u'/',				u'\0',	KEY_KPSLASH );
			_set_keyname_(u'.',				u'.',				u'\0',	KEY_KPDEL );
			_set_keyname_(u'*',				u'*',				u'\0',	KEY_KPSTAR );

			_set_keyname_(u'a',				u'A',				u'\0',	KEY_A  );
			_set_keyname_(u'b',				u'B',				u'\0',	KEY_B  );
			_set_keyname_(u'c',				u'C',				u'\0',	KEY_C  );
			_set_keyname_(u'd',				u'D',				u'\0',	KEY_D  );
			_set_keyname_(u'e',				u'E',				u'\0',	KEY_E  );
			_set_keyname_(u'f',				u'F',				u'\0',	KEY_F  );
			_set_keyname_(u'g',				u'G',				u'\0',	KEY_G  );
			_set_keyname_(u'h',				u'H',				u'\0',	KEY_H  );
			_set_keyname_(u'i',				u'I',				u'\0',	KEY_I  );
			_set_keyname_(u'j',				u'J',				u'\0',	KEY_J  );
			_set_keyname_(u'k',				u'K',				u'\0',	KEY_K  );
			_set_keyname_(u'l',				u'L',				u'\0',	KEY_L  );
			_set_keyname_(u'm',				u'M',				u'\0',	KEY_M  );
			_set_keyname_(u'n',				u'N',				u'\0',	KEY_N  );
			_set_keyname_(u'o',				u'O',				u'\0',	KEY_O  );
			_set_keyname_(u'p',				u'P',				u'\0',	KEY_P  );
			_set_keyname_(u'q',				u'Q',				u'\0',	KEY_Q  );
			_set_keyname_(u'r',				u'R',				u'\0',	KEY_R  );
			_set_keyname_(u's',				u'S',				u'\0',	KEY_S  );
			_set_keyname_(u't',				u'T',				u'\0',	KEY_T  );
			_set_keyname_(u'u',				u'U',				u'\0',	KEY_U  );
			_set_keyname_(u'v',				u'V',				u'\0',	KEY_V  );
			_set_keyname_(u'w',				u'W',				u'\0',	KEY_W  );
			_set_keyname_(u'x',				u'X',				u'\0',	KEY_X  );
			_set_keyname_(u'y',				u'Y',				u'\0',	KEY_Y  );
			_set_keyname_(u'z',				u'Z',				u'\0',	KEY_Z  );

			_set_keyname_(u'1',				u'!',				u'\0',	KEY_1  );
			_set_keyname_(u'2',				u'"',				u'\0',	KEY_2  );
			_set_keyname_(u'3',				0x00A3,			u'\0',	KEY_3  );		//�
			_set_keyname_(u'4',				u'$',				u'\0',	KEY_4  );
			_set_keyname_(u'5',				u'%',				u'\0',	KEY_5  );
			_set_keyname_(u'6',				u'&',				u'\0',	KEY_6  );
			_set_keyname_(u'7',				u'/',				u'\0',	KEY_7  );
			_set_keyname_(u'8',				u'(',				u'\0',	KEY_8  );
			_set_keyname_(u'9',				u')',				u'\0',	KEY_9  );
			_set_keyname_(u'0',				u'=',				u'\0',	KEY_0  );

			_set_keyname_(u',',				u';',				u'\0',	KEY_COMMA  );
			_set_keyname_(u'.',				u':',				u'\0',	KEY_PERIOD  );
			_set_keyname_(u'-',				u'_',				u'\0',	KEY_SLASH  );

			_set_keyname_(0x00E8,			0x00E9,			u'[',		KEY_LBRACKET  );		//��
			_set_keyname_(u'+',				u'*',				u']',		KEY_RBRACKET  );

			_set_keyname_(0x00F2,			0x00E7,			u'@',		KEY_SEMICOLON  );		//��
			_set_keyname_(0x00E0,			0x00B0,			u'#',		KEY_APOSTROPHE  );	//�
			_set_keyname_(u'\\',			u'|',				u'\0',	KEY_TICK  );
			_set_keyname_(0x00F9,			0x00A7,			u'\0',	KEY_BACKSLASH  );		//��

			_set_keyname_(u'\'',			u'?',				u'\0',	KEY_MINUS  );
			_set_keyname_(0x00EC,			u'^',				u'\0',	KEY_EQUAL  );				//�

			_set_keyname_(u'<',				u'>',				u'\0',	KEY_102  );

			m_shift2Key = KEY_RALT;
			break;

	}  // end switch( Language )

}  // end initKeyNames

///////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC PROTOTYPES //////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
Keyboard::Keyboard( void )
{

	memset( m_keys, 0, sizeof( m_keys ) );
	memset( m_keyStatus, 0, sizeof( m_keyStatus ) );
	m_modifiers = KEY_STATE_NONE;
	m_shift2Key = KEY_NONE;

	memset( m_keyNames, 0, sizeof( m_keyNames ) );
	m_inputFrame = 0;

}  // end Keyboard

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
Keyboard::~Keyboard( void )
{

}  // end ~Keyboard

//-------------------------------------------------------------------------------------------------
/** Initialzie the keyboard */
//-------------------------------------------------------------------------------------------------
void Keyboard::init( void )
{

	// initialize the key names
	initKeyNames();

	// first input frame
	m_inputFrame = 0;

}  // end init

//-------------------------------------------------------------------------------------------------
/** Reset keyboard system */
//-------------------------------------------------------------------------------------------------
void Keyboard::reset( void )
{

}  // end reset

//-------------------------------------------------------------------------------------------------
/** Called once per frame to gather key data input */
//-------------------------------------------------------------------------------------------------
void Keyboard::update( void )
{

	// increment input frame
	m_inputFrame++;

	// update the key data
	updateKeys();

}  // end update

//-------------------------------------------------------------------------------------------------
/** Reset the state data for the keys, we likely want to do this when
	* we once again gain focus to our app from something like an alt tab */
//-------------------------------------------------------------------------------------------------
void Keyboard::resetKeys( void )
{

	//
	// A modifier held when focus went away comes up in another window, and its release never
	// reaches the game.  Wiping the state here was not enough: the translators downstream still
	// had Alt held, so after an Alt+Tab the first key pressed went to ending an Alt that was long
	// gone, and a fresh Alt could not start waypoints because nothing had changed.  Let them see
	// the release.  There is no stream yet, or any more, while the window starts and closes.
	//
	static const KeyDefType modifierKeys[] = { KEY_LCTRL, KEY_RCTRL, KEY_LSHIFT, KEY_RSHIFT, KEY_LALT, KEY_RALT };
	for( Int i = 0; TheMessageStream && i < (Int)ARRAY_SIZE( modifierKeys ); ++i )
	{
		if( BitTest( m_keyStatus[ modifierKeys[ i ] ].state, KEY_STATE_DOWN ) )
		{
			GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_RAW_KEY_UP );
			msg->appendIntegerArgument( modifierKeys[ i ] );
			msg->appendIntegerArgument( KEY_STATE_UP );
		}
	}

	memset( m_keys, 0, sizeof( m_keys ) );
	memset( m_keyStatus, 0, sizeof( m_keyStatus ) );
	m_modifiers = KEY_STATE_NONE; 
	if( getCapsState() )
	{
		m_modifiers |= KEY_STATE_CAPSLOCK;
	}

}  // end resetKeys

//-------------------------------------------------------------------------------------------------
/** get the first key in our current state of the keyboard */
//-------------------------------------------------------------------------------------------------
KeyboardIO *Keyboard::getFirstKey( void )
{
	return &m_keys[ 0 ];	
}  // end getFirstKey

//-------------------------------------------------------------------------------------------------
/** return the key status for the specified key */
//-------------------------------------------------------------------------------------------------
UnsignedByte Keyboard::getKeyStatusData( UnsignedByte key )
{
	return m_keyStatus[ key ].status;
}  // end getKeyStatusData

//-------------------------------------------------------------------------------------------------
/** Get the key state data as a Bool for the specified key */
//-------------------------------------------------------------------------------------------------
Bool Keyboard::getKeyStateBit( UnsignedByte key, Int bit )
{
	return (m_keyStatus[ key ].state & bit) ? 1 : 0;
}  // end getKeyStateBit

//-------------------------------------------------------------------------------------------------
/** return the sequence data for the given key */
//-------------------------------------------------------------------------------------------------
UnsignedInt Keyboard::getKeySequenceData( UnsignedByte key )
{
	return m_keyStatus[ key ].sequence;
}  // end getKeySequenceData

//-------------------------------------------------------------------------------------------------
/** set the key status data */
//-------------------------------------------------------------------------------------------------
void Keyboard::setKeyStatusData( UnsignedByte key, KeyboardIO::StatusType data )
{
	m_keyStatus[ key ].status = data;
}  // end setKeyStatusData

//-------------------------------------------------------------------------------------------------
/** set the key state data */
//-------------------------------------------------------------------------------------------------
void Keyboard::setKeyStateData( UnsignedByte key, UnsignedByte data )
{
	m_keyStatus[ key ].state = data;
}  // end setKeyStateData

//-------------------------------------------------------------------------------------------------
/** This routine must be called with every character to
	* properly monitor the shift state.  Takes a keycode as
	* input, and returns the UNICODE char representing the
	* character. */
//-------------------------------------------------------------------------------------------------
WideChar Keyboard::translateKey( WideChar keyCode )
{

	if( keyCode > 0x00FF )
		return keyCode;

	UnsignedByte ubKeyCode = (UnsignedByte)keyCode;
	switch( ubKeyCode )
	{

		case KEY_CAPS:
			if( getKeyStatusData( KEY_CAPS ) == KeyboardIO::STATUS_UNUSED )
			{
				if( getKeyStateBit( KEY_CAPS, KEY_STATE_DOWN ) )
				{
					if( m_modifiers & KEY_STATE_CAPSLOCK )
					{
						// Toggle caplocks off
						m_modifiers &= ~KEY_STATE_CAPSLOCK;
					}
					else
					{
						// Toggle caplocks on
						m_modifiers |= KEY_STATE_CAPSLOCK;
					}
				}

				setKeyStatusData( KEY_CAPS, KeyboardIO::STATUS_USED );
			}
			return 0;

		case KEY_LSHIFT:
			if( getKeyStateBit( KEY_LSHIFT, KEY_STATE_DOWN ) )
			{
				m_modifiers |= KEY_STATE_LSHIFT;
			}
			else
			{
				m_modifiers &= ~KEY_STATE_LSHIFT;
			}
			return 0;

		case KEY_RSHIFT:
			if( getKeyStateBit( KEY_RSHIFT, KEY_STATE_DOWN ) )
			{
				m_modifiers |= KEY_STATE_RSHIFT;
			}
			else
			{
				m_modifiers &= ~KEY_STATE_RSHIFT;
			}
			return 0;

		case KEY_LCTRL:
			if( getKeyStateBit( KEY_LCTRL, KEY_STATE_DOWN ) )
			{
				m_modifiers |= KEY_STATE_LCONTROL;
			}
			else
			{
				m_modifiers &= ~KEY_STATE_LCONTROL;
			}
			return 0;

		case KEY_RCTRL:
			if( getKeyStateBit( KEY_RCTRL, KEY_STATE_DOWN ) )
			{
				m_modifiers |= KEY_STATE_RCONTROL;
			}
			else
			{
				m_modifiers &= ~KEY_STATE_RCONTROL;
			}
			return 0;

		case KEY_LALT:
			if( getKeyStateBit( KEY_LALT, KEY_STATE_DOWN ) )
			{
				m_modifiers |= KEY_STATE_LALT;
			}
			else
			{
				m_modifiers &= ~KEY_STATE_LALT;
			}
			return 0;

		case KEY_RALT:
			if( getKeyStateBit( KEY_RALT, KEY_STATE_DOWN ) )
			{
				m_modifiers |= KEY_STATE_RALT;
			}
			else
			{
				m_modifiers &= ~KEY_STATE_RALT;
			}
			return 0;

		default:
			if( ubKeyCode == m_shift2Key )
			{
				if( getKeyStateBit( m_shift2Key, KEY_STATE_DOWN ) )
				{
					m_modifiers |= KEY_STATE_SHIFT2;
				}
				else
				{
					m_modifiers &= ~KEY_STATE_SHIFT2;
				}
				return 0;
			}

			if( m_modifiers & KEY_STATE_SHIFT2 )
			{
				return( m_keyNames[ ubKeyCode ].shifted2 );
			}
			
			if( isShift() || getCapsState() && GameIsAlpha( m_keyNames[ ubKeyCode ].stdKey ) )
			{
				return( m_keyNames[ ubKeyCode ].shifted );
			}
			
			return( m_keyNames[ubKeyCode ].stdKey );
	}  // end switch( ubKeyCode )
}  // end translateKey

//-------------------------------------------------------------------------------------------------
/** returns true if any shift state is pressed */
//-------------------------------------------------------------------------------------------------
Bool Keyboard::isShift()
{
		if( m_modifiers & KEY_STATE_LSHIFT || m_modifiers & KEY_STATE_RSHIFT || m_modifiers & KEY_STATE_SHIFT2 )
		{
			return TRUE;
		}
		return FALSE;
}  // end isShift()

//-------------------------------------------------------------------------------------------------
/** returns true if any control state is pressed */
//-------------------------------------------------------------------------------------------------
Bool Keyboard::isCtrl()
{
		if( m_modifiers & KEY_STATE_LCONTROL || m_modifiers & KEY_STATE_RCONTROL )
		{
			return TRUE;
		}
		return FALSE;
}  // end isCtrl()

//-------------------------------------------------------------------------------------------------
/** returns true if any shift state is pressed */
//-------------------------------------------------------------------------------------------------
Bool Keyboard::isAlt()
{
	if( m_modifiers & KEY_STATE_LALT || m_modifiers & KEY_STATE_RALT )
	{
		return TRUE;
	}
	return FALSE;
}  // end isAlt()


WideChar Keyboard::getPrintableKey( UnsignedByte key,  Int state )
{
	if((key < 0 || key >=KEY_NAMES_COUNT) || ( state < 0 || state >= MAX_KEY_STATES))
		return u'\0';
	if(state == 0)
		return m_keyNames[key].stdKey;
	else if(state == 1)
		return m_keyNames[key].shifted;
	else 
		return m_keyNames[key].shifted2;


}