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
+2 -2
View File
@@ -97,7 +97,7 @@ RString RageInput::GetDeviceSpecificInputString( const DeviceInput &di )
return di.ToString(); return di.ToString();
} }
char RageInput::DeviceButtonToChar( DeviceButton button ) wchar_t RageInput::DeviceButtonToChar( DeviceButton button, bool bUseCurrentKeyModifiers )
{ {
FOREACH( InputHandler*, m_pDevices, i ) FOREACH( InputHandler*, m_pDevices, i )
{ {
@@ -107,7 +107,7 @@ char RageInput::DeviceButtonToChar( DeviceButton button )
FOREACH_CONST( InputDeviceInfo, vDevices, idi ) FOREACH_CONST( InputDeviceInfo, vDevices, idi )
{ {
if( idi->id == DEVICE_KEYBOARD ) if( idi->id == DEVICE_KEYBOARD )
return (*i)->DeviceButtonToChar(button); return (*i)->DeviceButtonToChar(button, bUseCurrentKeyModifiers);
} }
} }
+1 -1
View File
@@ -21,7 +21,7 @@ public:
void WindowReset(); void WindowReset();
void AddHandler( InputHandler *pHandler ); void AddHandler( InputHandler *pHandler );
RString GetDeviceSpecificInputString( const DeviceInput &di ); RString GetDeviceSpecificInputString( const DeviceInput &di );
char DeviceButtonToChar( DeviceButton button ); wchar_t DeviceButtonToChar( DeviceButton button, bool bUseCurrentKeyModifiers );
InputDeviceState GetInputDeviceState( InputDevice id ); InputDeviceState GetInputDeviceState( InputDevice id );
RString GetDisplayDevicesString() const; RString GetDisplayDevicesString() const;
+13 -2
View File
@@ -8,6 +8,7 @@
#include "RageUtil.h" #include "RageUtil.h"
#include "EnumHelper.h" #include "EnumHelper.h"
#include "LocalizedString.h" #include "LocalizedString.h"
#include "RageInput.h"
static map<DeviceButton,RString> g_mapNamesToString; static map<DeviceButton,RString> g_mapNamesToString;
static map<RString,DeviceButton> g_mapStringToNames; static map<RString,DeviceButton> g_mapStringToNames;
@@ -147,6 +148,16 @@ RString DeviceButtonToString( DeviceButton key )
return "unknown"; return "unknown";
} }
RString DeviceButtonToTranslatedString( DeviceButton key )
{
/* All printable ASCII except for uppercase alpha characters line up. */
if( key >= 33 && key < 127 &&
!(key >= 'A' && key <= 'Z' ) )
return WStringToRString( wstring()+INPUTMAN->DeviceButtonToChar(key,false) );
return DeviceButtonToString( key );
}
static LocalizedString HOME ( "DeviceButton", "Home" ); static LocalizedString HOME ( "DeviceButton", "Home" );
static LocalizedString END ( "DeviceButton", "End" ); static LocalizedString END ( "DeviceButton", "End" );
static LocalizedString UP ( "DeviceButton", "Up" ); static LocalizedString UP ( "DeviceButton", "Up" );
@@ -161,7 +172,7 @@ static LocalizedString PGUP ( "DeviceButton", "PgUp" );
static LocalizedString PGDN ( "DeviceButton", "PgDn" ); static LocalizedString PGDN ( "DeviceButton", "PgDn" );
static LocalizedString BACKSLASH ( "DeviceButton", "Backslash" ); static LocalizedString BACKSLASH ( "DeviceButton", "Backslash" );
RString DeviceButtonToLocalizedString( DeviceButton key ) RString DeviceButtonToLocalizedAndTranslatedString( DeviceButton key )
{ {
switch( key ) switch( key )
{ {
@@ -178,7 +189,7 @@ RString DeviceButtonToLocalizedString( DeviceButton key )
case KEY_PGUP: return PGUP.GetValue(); case KEY_PGUP: return PGUP.GetValue();
case KEY_PGDN: return PGDN.GetValue(); case KEY_PGDN: return PGDN.GetValue();
case KEY_BACKSLASH: return BACKSLASH.GetValue(); case KEY_BACKSLASH: return BACKSLASH.GetValue();
default: return Capitalize( DeviceButtonToString(key) ); default: return Capitalize( DeviceButtonToTranslatedString(key) );
} }
} }
+2 -1
View File
@@ -292,7 +292,8 @@ enum DeviceButton
}; };
RString DeviceButtonToString( DeviceButton i ); RString DeviceButtonToString( DeviceButton i );
RString DeviceButtonToLocalizedString( DeviceButton i ); RString DeviceButtonToTranslatedString( DeviceButton i );
RString DeviceButtonToLocalizedAndTranslatedString( DeviceButton i );
DeviceButton StringToDeviceButton( const RString& s ); DeviceButton StringToDeviceButton( const RString& s );
struct DeviceInput struct DeviceInput
+2 -2
View File
@@ -3596,9 +3596,9 @@ static RString GetDeviceButtonsLocalized( const vector<EditButton> &veb, const M
DeviceInput diPress = editmap.button[*eb][s]; DeviceInput diPress = editmap.button[*eb][s];
DeviceInput diHold = editmap.hold[*eb][s]; DeviceInput diHold = editmap.hold[*eb][s];
if( diPress.IsValid() ) if( diPress.IsValid() )
vsPress.push_back( DeviceButtonToLocalizedString(diPress.button) ); vsPress.push_back( DeviceButtonToLocalizedAndTranslatedString(diPress.button) );
if( diHold.IsValid() ) if( diHold.IsValid() )
vsHold.push_back( DeviceButtonToLocalizedString(diHold.button) ); vsHold.push_back( DeviceButtonToLocalizedAndTranslatedString(diHold.button) );
} }
} }
+1 -1
View File
@@ -516,7 +516,7 @@ void ScreenNameEntryTraditional::Input( const InputEventPlus &input )
if( input.DeviceI == DeviceInput(DEVICE_KEYBOARD, KEY_BACK) ) if( input.DeviceI == DeviceInput(DEVICE_KEYBOARD, KEY_BACK) )
c = CHAR_BACK; c = CHAR_BACK;
else else
c = toupper( INPUTMAN->DeviceButtonToChar( input.DeviceI.button ) ); c = towupper( INPUTMAN->DeviceButtonToChar(input.DeviceI.button,true) );
if( c ) if( c )
{ {
PlayerNumber pn = GAMESTATE->m_MasterPlayerNumber; PlayerNumber pn = GAMESTATE->m_MasterPlayerNumber;
+2 -35
View File
@@ -95,10 +95,6 @@ void ScreenNetSelectBase::Input( const InputEventPlus &input )
if( (input.type != IET_FIRST_PRESS) && (input.type != IET_SLOW_REPEAT) && (input.type != IET_FAST_REPEAT ) ) if( (input.type != IET_FIRST_PRESS) && (input.type != IET_SLOW_REPEAT) && (input.type != IET_FAST_REPEAT ) )
return; return;
bool bHoldingShift =
INPUTFILTER->IsBeingPressed(DeviceInput(DEVICE_KEYBOARD, KEY_LSHIFT)) ||
INPUTFILTER->IsBeingPressed(DeviceInput(DEVICE_KEYBOARD, KEY_RSHIFT));
bool bHoldingCtrl = bool bHoldingCtrl =
INPUTFILTER->IsBeingPressed(DeviceInput(DEVICE_KEYBOARD, KEY_LCTRL)) || INPUTFILTER->IsBeingPressed(DeviceInput(DEVICE_KEYBOARD, KEY_LCTRL)) ||
INPUTFILTER->IsBeingPressed(DeviceInput(DEVICE_KEYBOARD, KEY_RCTRL)) || INPUTFILTER->IsBeingPressed(DeviceInput(DEVICE_KEYBOARD, KEY_RCTRL)) ||
@@ -123,38 +119,9 @@ void ScreenNetSelectBase::Input( const InputEventPlus &input )
UpdateTextInput(); UpdateTextInput();
break; break;
default: default:
char c; wchar_t c;
c = INPUTMAN->DeviceButtonToChar( input.DeviceI.button ); c = INPUTMAN->DeviceButtonToChar(input.DeviceI.button,true);
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;
}
}
if( (c >= ' ') && (!bHoldingCtrl) ) if( (c >= ' ') && (!bHoldingCtrl) )
{ {
m_sTextInput += c; m_sTextInput += c;
+1 -1
View File
@@ -129,7 +129,7 @@ void ScreenNetSelectMusic::Input( const InputEventPlus &input )
INPUTFILTER->IsBeingPressed(DeviceInput(DEVICE_KEYBOARD, KEY_RCTRL)) || INPUTFILTER->IsBeingPressed(DeviceInput(DEVICE_KEYBOARD, KEY_RCTRL)) ||
(!NSMAN->useSMserver); //If we are disconnected, assume no chatting (!NSMAN->useSMserver); //If we are disconnected, assume no chatting
char c = (char) toupper( INPUTMAN->DeviceButtonToChar(input.DeviceI.button) ); wchar_t c = (wchar_t) towupper( INPUTMAN->DeviceButtonToChar(input.DeviceI.button,true) );
if ( bHoldingCtrl && ( c >= 'A' ) && ( c <= 'Z' ) ) if ( bHoldingCtrl && ( c >= 'A' ) && ( c <= 'Z' ) )
{ {
+2 -34
View File
@@ -183,42 +183,10 @@ void ScreenTextEntry::Input( const InputEventPlus &input )
} }
else if( input.type == IET_FIRST_PRESS ) else if( input.type == IET_FIRST_PRESS )
{ {
char c = INPUTMAN->DeviceButtonToChar( input.DeviceI.button ); wchar_t c = INPUTMAN->DeviceButtonToChar(input.DeviceI.button,true);
if( c >= ' ' ) if( c >= ' ' )
{ {
bool bIsHoldingShift = TryAppendToAnswer( WStringToRString(wstring()+c) );
INPUTFILTER->IsBeingPressed( DeviceInput(input.DeviceI.device, KEY_RSHIFT)) ||
INPUTFILTER->IsBeingPressed( DeviceInput(input.DeviceI.device, KEY_LSHIFT));
if( bIsHoldingShift )
{
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;
}
}
TryAppendToAnswer( ssprintf( "%c", c ) );
TextEnteredDirectly(); TextEnteredDirectly();
} }
@@ -34,22 +34,65 @@ void InputHandler::ButtonPressed( DeviceInput di, bool Down )
} }
} }
char InputHandler::DeviceButtonToChar( DeviceButton button ) wchar_t InputHandler::DeviceButtonToChar( DeviceButton button, bool bUseCurrentKeyModifiers )
{ {
if( button < 127 ) wchar_t c;
return (char) button;
if( button >= KEY_KP_C0 && button <= KEY_KP_C9 )
return (char) (button - KEY_KP_C0) + '0';
switch( button ) switch( button )
{ {
case KEY_KP_SLASH: return '/'; default:
case KEY_KP_ASTERISK: return '*'; if( button < 127 )
case KEY_KP_HYPHEN: return '-'; c = (char) button;
case KEY_KP_PLUS: return '+'; else if( button >= KEY_KP_C0 && button <= KEY_KP_C9 )
case KEY_KP_PERIOD: return '.'; c =(char) (button - KEY_KP_C0) + '0';
case KEY_KP_EQUAL: return '='; 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'; return '\0';
@@ -59,15 +102,15 @@ RString InputHandler::GetDeviceSpecificInputString( const DeviceInput &di )
{ {
if( di.device == DEVICE_KEYBOARD ) if( di.device == DEVICE_KEYBOARD )
{ {
char c = DeviceButtonToChar( di.button ); wchar_t c = DeviceButtonToChar( di.button, false );
if( c ) if( c )
{ {
if( c == ' ' ) if( c == ' ' )
return "space"; // Don't show "Key " for space. return "space"; // Don't show "Key " for space.
else else
return ssprintf( "Key %c", c ); return "Key " + WStringToRString(wstring()+c);
} }
return DeviceButtonToString( di.button ); return DeviceButtonToTranslatedString( di.button );
} }
return di.ToString(); return di.ToString();
@@ -33,7 +33,7 @@ public:
// Override to return a pretty string that's specific to the controller type. // Override to return a pretty string that's specific to the controller type.
virtual RString GetDeviceSpecificInputString( const DeviceInput &di ); 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. // Override to find out whether the controller is currently plugged in.
// Not all InputHandlers will support this. Not applicable to all InputHandlers. // 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 ) for( int k = 0; k < 256; ++k )
{ {
const DeviceButton key = (DeviceButton) device.Inputs[k].num; 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; break;
@@ -645,7 +645,25 @@ void InputHandler_DInput::GetDevicesAndDescriptions( vector<InputDeviceInfo>& vD
vDevicesOut.push_back( InputDeviceInfo(Devices[i].dev, Devices[i].m_sName) ); 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 ) FOREACH_CONST( DIDevice, Devices, d )
{ {
@@ -657,28 +675,15 @@ char InputHandler_DInput::DeviceButtonToChar( DeviceButton button )
if( button != i->num ) if( button != i->num )
continue; continue;
DWORD scancode = i->ofs; unsigned char keys[256];
ZERO( keys );
HKL layout = GetKeyboardLayout(0); // 0 == current thread if( bUseCurrentKeyModifiers )
GetKeyboardState(keys);
unsigned char state[256]; return ScancodeAndKeysToChar( i->ofs, keys );
// 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.
} }
} }
return InputHandler::DeviceButtonToChar( button ); return InputHandler::DeviceButtonToChar( button, bUseCurrentKeyModifiers );
} }
/* /*
@@ -11,7 +11,7 @@ public:
InputHandler_DInput(); InputHandler_DInput();
~InputHandler_DInput(); ~InputHandler_DInput();
void GetDevicesAndDescriptions( vector<InputDeviceInfo>& vDevicesOut ); void GetDevicesAndDescriptions( vector<InputDeviceInfo>& vDevicesOut );
char DeviceButtonToChar( DeviceButton button ); wchar_t DeviceButtonToChar( DeviceButton button, bool bUseCurrentKeyModifiers );
void Update(); void Update();
bool DevicesChanged(); bool DevicesChanged();
void WindowReset(); void WindowReset();