let OS handle key modifers (fixes number buttons w/ French keyboard)

This commit is contained in:
Chris Danford
2006-06-10 20:33:56 +00:00
parent 3b68311391
commit 8c4cedf63f
13 changed files with 113 additions and 118 deletions
@@ -34,24 +34,67 @@ void InputHandler::ButtonPressed( DeviceInput di, bool Down )
}
}
char InputHandler::DeviceButtonToChar( DeviceButton button )
wchar_t InputHandler::DeviceButtonToChar( DeviceButton button, bool bUseCurrentKeyModifiers )
{
if( button < 127 )
return (char) button;
if( button >= KEY_KP_C0 && button <= KEY_KP_C9 )
return (char) (button - KEY_KP_C0) + '0';
wchar_t c;
switch( button )
{
case KEY_KP_SLASH: return '/';
case KEY_KP_ASTERISK: return '*';
case KEY_KP_HYPHEN: return '-';
case KEY_KP_PLUS: return '+';
case KEY_KP_PERIOD: return '.';
case KEY_KP_EQUAL: return '=';
default:
if( button < 127 )
c = (char) button;
else if( button >= KEY_KP_C0 && button <= KEY_KP_C9 )
c =(char) (button - KEY_KP_C0) + '0';
break;
case KEY_KP_SLASH: c ='/'; break;
case KEY_KP_ASTERISK: c ='*'; break;
case KEY_KP_HYPHEN: c ='-'; break;
case KEY_KP_PLUS: c ='+'; break;
case KEY_KP_PERIOD: c ='.'; break;
case KEY_KP_EQUAL: c ='='; break;
}
if( bUseCurrentKeyModifiers )
{
bool bHoldingShift =
INPUTFILTER->IsBeingPressed(DeviceInput(DEVICE_KEYBOARD, KEY_LSHIFT)) ||
INPUTFILTER->IsBeingPressed(DeviceInput(DEVICE_KEYBOARD, KEY_RSHIFT));
bool bHoldingCtrl =
INPUTFILTER->IsBeingPressed(DeviceInput(DEVICE_KEYBOARD, KEY_LCTRL)) ||
INPUTFILTER->IsBeingPressed(DeviceInput(DEVICE_KEYBOARD, KEY_RCTRL));
if( bHoldingShift && !bHoldingCtrl )
{
c = (char)toupper(c);
switch( c )
{
case '`': c='~'; break;
case '1': c='!'; break;
case '2': c='@'; break;
case '3': c='#'; break;
case '4': c='$'; break;
case '5': c='%'; break;
case '6': c='^'; break;
case '7': c='&'; break;
case '8': c='*'; break;
case '9': c='('; break;
case '0': c=')'; break;
case '-': c='_'; break;
case '=': c='+'; break;
case '[': c='{'; break;
case ']': c='}'; break;
case '\'': c='"'; break;
case '\\': c='|'; break;
case ';': c=':'; break;
case ',': c='<'; break;
case '.': c='>'; break;
case '/': c='?'; break;
}
}
}
return '\0';
}
@@ -59,15 +102,15 @@ RString InputHandler::GetDeviceSpecificInputString( const DeviceInput &di )
{
if( di.device == DEVICE_KEYBOARD )
{
char c = DeviceButtonToChar( di.button );
wchar_t c = DeviceButtonToChar( di.button, false );
if( c )
{
if( c == ' ' )
return "space"; // Don't show "Key " for space.
else
return ssprintf( "Key %c", c );
return "Key " + WStringToRString(wstring()+c);
}
return DeviceButtonToString( di.button );
return DeviceButtonToTranslatedString( di.button );
}
return di.ToString();
@@ -33,7 +33,7 @@ public:
// Override to return a pretty string that's specific to the controller type.
virtual RString GetDeviceSpecificInputString( const DeviceInput &di );
virtual char DeviceButtonToChar( DeviceButton button );
virtual wchar_t DeviceButtonToChar( DeviceButton button, bool bUseCurrentKeyModifiers );
// Override to find out whether the controller is currently plugged in.
// Not all InputHandlers will support this. Not applicable to all InputHandlers.
@@ -272,7 +272,7 @@ void InputHandler_DInput::UpdatePolled( DIDevice &device, const RageTimer &tm )
for( int k = 0; k < 256; ++k )
{
const DeviceButton key = (DeviceButton) device.Inputs[k].num;
ButtonPressed(DeviceInput(device.dev, key), !!(keys[k] & 0x80));
ButtonPressed( DeviceInput(device.dev, key), !!(keys[k] & 0x80) );
}
}
break;
@@ -645,7 +645,25 @@ void InputHandler_DInput::GetDevicesAndDescriptions( vector<InputDeviceInfo>& vD
vDevicesOut.push_back( InputDeviceInfo(Devices[i].dev, Devices[i].m_sName) );
}
char InputHandler_DInput::DeviceButtonToChar( DeviceButton button )
static wchar_t ScancodeAndKeysToChar( DWORD scancode, unsigned char keys[256] )
{
static HKL layout = GetKeyboardLayout(0); // 0 == current thread
UINT vk = MapVirtualKeyEx( scancode, 1, layout );
unsigned short result[2]; // ToAscii writes a max of 2 chars
ZERO( result );
// TODO: Use ToUnicodeEx
int iNum = ToAsciiEx( vk, scancode, keys, result, 0, layout );
// iNum == 2 will happen only for dead keys. See MSDN for ToAsciiEx.
if( iNum == 1 )
{
RString s = RString()+(char)result[0];
return RStringToWstring( ConvertACPToUTF8(s) )[0];
}
return '\0';
}
wchar_t InputHandler_DInput::DeviceButtonToChar( DeviceButton button, bool bUseCurrentKeyModifiers )
{
FOREACH_CONST( DIDevice, Devices, d )
{
@@ -657,28 +675,15 @@ char InputHandler_DInput::DeviceButtonToChar( DeviceButton button )
if( button != i->num )
continue;
DWORD scancode = i->ofs;
HKL layout = GetKeyboardLayout(0); // 0 == current thread
unsigned char state[256];
// Don't fill key state. We'll do the shift/ctrl/alt
// translations ourself in ScreenTextEntry.
ZERO( state );
UINT vk = MapVirtualKeyEx( scancode, 1, layout );
unsigned short result[2]; // ToAscii writes a max of 2 chars
ZERO( result );
// TODO: Use ToUnicodeEx
int iNum = ToAsciiEx( vk, scancode, state, result, 0, layout );
if( iNum == 1 )
return (char)result[0];
// iNum == 2 will happen only for dead keys. See MSDN for ToAsciiEx.
unsigned char keys[256];
ZERO( keys );
if( bUseCurrentKeyModifiers )
GetKeyboardState(keys);
return ScancodeAndKeysToChar( i->ofs, keys );
}
}
return InputHandler::DeviceButtonToChar( button );
return InputHandler::DeviceButtonToChar( button, bUseCurrentKeyModifiers );
}
/*
@@ -11,7 +11,7 @@ public:
InputHandler_DInput();
~InputHandler_DInput();
void GetDevicesAndDescriptions( vector<InputDeviceInfo>& vDevicesOut );
char DeviceButtonToChar( DeviceButton button );
wchar_t DeviceButtonToChar( DeviceButton button, bool bUseCurrentKeyModifiers );
void Update();
bool DevicesChanged();
void WindowReset();