Fixed keyboard (win/linux) and input focus (linux) bugs when returning from launching.

This commit is contained in:
Don Honerbrink 2015-03-20 23:38:34 -05:00
parent 65e730bc54
commit aa797abc61
4 changed files with 81 additions and 21 deletions

View File

@ -74,6 +74,29 @@ SDL_Scancode UserInput::GetScancode(KeyCode_E key)
} }
UserInput::KeyCode_E UserInput::GetKeycode(SDL_Scancode scancode)
{
KeyCode_E keycode = KeyCodeNull;
std::map<SDL_Scancode, KeyCode_E>::iterator it = ReverseKeyMap.find(scancode);
if(it != ReverseKeyMap.end())
{
keycode = it->second;
}
return keycode;
}
void UserInput::ResetKeyStates()
{
for(std::map<KeyCode_E, bool>::iterator it = KeyState.begin(); it != KeyState.end(); it++)
{
it->second = false;
}
}
bool UserInput::MapKey(std::string keyDescription, KeyCode_E key) bool UserInput::MapKey(std::string keyDescription, KeyCode_E key)
{ {
SDL_Scancode scanCode; SDL_Scancode scanCode;
@ -96,6 +119,28 @@ bool UserInput::MapKey(std::string keyDescription, KeyCode_E key)
} }
KeyMap[key] = scanCode; KeyMap[key] = scanCode;
ReverseKeyMap[scanCode] = key;
KeyState[key] = false;
return true; return true;
} }
bool UserInput::SetKeyState(SDL_Scancode code, bool state)
{
KeyCode_E key = GetKeycode(code);
if(key == KeyCodeNull)
{
return false;
}
KeyState[key] = state;
return true;
}
bool UserInput::GetKeyState(KeyCode_E key)
{
return KeyState[key];
}

View File

@ -25,6 +25,7 @@ class UserInput
public: public:
enum KeyCode_E enum KeyCode_E
{ {
KeyCodeNull,
KeyCodeUp, KeyCodeUp,
KeyCodeDown, KeyCodeDown,
KeyCodeLeft, KeyCodeLeft,
@ -42,11 +43,19 @@ public:
virtual ~UserInput(); virtual ~UserInput();
bool Initialize(); bool Initialize();
SDL_Scancode GetScancode(KeyCode_E key); SDL_Scancode GetScancode(KeyCode_E key);
KeyCode_E GetKeycode(SDL_Scancode scancode);
bool SetKeyState(SDL_Scancode code, bool state);
bool GetKeyState(KeyCode_E key);
bool KeyStateChanged();
void ResetKeyStates();
private: private:
bool MapKey(std::string keyDescription, KeyCode_E key); bool MapKey(std::string keyDescription, KeyCode_E key);
std::map<KeyCode_E, SDL_Scancode> KeyMap; std::map<KeyCode_E, SDL_Scancode> KeyMap;
std::map<SDL_Scancode, KeyCode_E> ReverseKeyMap;
std::map<KeyCode_E, bool> KeyState;
Configuration &Config; Configuration &Config;
const Uint8 *SDLKeys;
}; };

View File

@ -120,8 +120,10 @@ void RetroFE::LaunchEnter()
void RetroFE::LaunchExit() void RetroFE::LaunchExit()
{ {
SDL_RestoreWindow(SDL::GetWindow()); SDL_RestoreWindow(SDL::GetWindow());
SDL_RaiseWindow(SDL::GetWindow());
SDL_SetWindowGrab(SDL::GetWindow(), SDL_TRUE); SDL_SetWindowGrab(SDL::GetWindow(), SDL_TRUE);
Input.ResetKeyStates();
Attract.Reset();
if(CurrentPage) if(CurrentPage)
{ {
CurrentPage->LaunchExit(); CurrentPage->LaunchExit();
@ -386,50 +388,50 @@ RetroFE::RETROFE_STATE RetroFE::ProcessUserInput(Page *page)
SDL_Event e; SDL_Event e;
bool exit = false; bool exit = false;
RETROFE_STATE state = RETROFE_IDLE; RETROFE_STATE state = RETROFE_IDLE;
if (SDL_PollEvent(&e) == 0) return state; if (SDL_PollEvent(&e) == 0) return state;
if(e.type == SDL_KEYDOWN || e.type == SDL_KEYUP) if(e.type == SDL_KEYDOWN || e.type == SDL_KEYUP)
{ {
const Uint8 *keys = SDL_GetKeyboardState(NULL); SDL_Scancode scancode = SDL_GetScancodeFromKey(e.key.keysym.sym);
Input.SetKeyState(scancode, (e.type == SDL_KEYDOWN) ? true : false);
Attract.Reset(); Attract.Reset();
if(page->IsHorizontalScroll()) if(page->IsHorizontalScroll())
{ {
if (keys[Input.GetScancode(UserInput::KeyCodeLeft)]) if (Input.GetKeyState(UserInput::KeyCodeLeft))
{ {
page->SetScrolling(Page::ScrollDirectionBack); page->SetScrolling(Page::ScrollDirectionBack);
} }
if (keys[Input.GetScancode(UserInput::KeyCodeRight)]) if (Input.GetKeyState(UserInput::KeyCodeRight))
{ {
page->SetScrolling(Page::ScrollDirectionForward); page->SetScrolling(Page::ScrollDirectionForward);
} }
} }
else else
{ {
if (keys[Input.GetScancode(UserInput::KeyCodeUp)]) if (Input.GetKeyState(UserInput::KeyCodeUp))
{ {
page->SetScrolling(Page::ScrollDirectionBack); page->SetScrolling(Page::ScrollDirectionBack);
} }
if (keys[Input.GetScancode(UserInput::KeyCodeDown)]) if (Input.GetKeyState(UserInput::KeyCodeDown))
{ {
page->SetScrolling(Page::ScrollDirectionForward); page->SetScrolling(Page::ScrollDirectionForward);
} }
} }
if (keys[Input.GetScancode(UserInput::KeyCodePageUp)]) if (Input.GetKeyState(UserInput::KeyCodePageUp))
{ {
page->PageScroll(Page::ScrollDirectionBack); page->PageScroll(Page::ScrollDirectionBack);
} }
if (keys[Input.GetScancode(UserInput::KeyCodePageDown)]) if (Input.GetKeyState(UserInput::KeyCodePageDown))
{ {
page->PageScroll(Page::ScrollDirectionForward); page->PageScroll(Page::ScrollDirectionForward);
} }
if (keys[Input.GetScancode(UserInput::KeyCodeAdminMode)]) if (Input.GetKeyState(UserInput::KeyCodeAdminMode))
{ {
//todo: add admin mode support //todo: add admin mode support
} }
if (keys[Input.GetScancode(UserInput::KeyCodeSelect)] && page->IsMenuIdle()) if (Input.GetKeyState(UserInput::KeyCodeSelect) && page->IsMenuIdle())
{ {
NextPageItem = page->GetSelectedItem(); NextPageItem = page->GetSelectedItem();
@ -453,7 +455,7 @@ RetroFE::RETROFE_STATE RetroFE::ProcessUserInput(Page *page)
} }
} }
if (keys[Input.GetScancode(UserInput::KeyCodeBack)] && page->IsMenuIdle()) if (Input.GetKeyState(UserInput::KeyCodeBack) && page->IsMenuIdle())
{ {
if(Back(exit) || exit) if(Back(exit) || exit)
{ {
@ -461,17 +463,17 @@ RetroFE::RETROFE_STATE RetroFE::ProcessUserInput(Page *page)
} }
} }
if (keys[Input.GetScancode(UserInput::KeyCodeQuit)]) if (Input.GetKeyState(UserInput::KeyCodeQuit))
{ {
state = RETROFE_QUIT_REQUEST; state = RETROFE_QUIT_REQUEST;
} }
if(!keys[Input.GetScancode(UserInput::KeyCodeUp)] && if(!Input.GetKeyState(UserInput::KeyCodeUp) &&
!keys[Input.GetScancode(UserInput::KeyCodeLeft)] && !Input.GetKeyState(UserInput::KeyCodeLeft) &&
!keys[Input.GetScancode(UserInput::KeyCodeDown)] && !Input.GetKeyState(UserInput::KeyCodeDown) &&
!keys[Input.GetScancode(UserInput::KeyCodeRight)] && !Input.GetKeyState(UserInput::KeyCodeRight) &&
!keys[Input.GetScancode(UserInput::KeyCodePageUp)] && !Input.GetKeyState(UserInput::KeyCodePageUp) &&
!keys[Input.GetScancode(UserInput::KeyCodePageDown)]) !Input.GetKeyState(UserInput::KeyCodePageDown))
{ {
page->SetScrolling(Page::ScrollDirectionIdle); page->SetScrolling(Page::ScrollDirectionIdle);
} }

View File

@ -135,7 +135,11 @@ bool SDL::Initialize(Configuration &config)
if (retVal && Fullscreen) if (retVal && Fullscreen)
{ {
#ifdef WIN32
windowFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP; windowFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
#else
windowFlags |= SDL_WINDOW_FULLSCREEN;
#endif
} }
if(retVal) if(retVal)