Prevent input when window not in focus
This commit is contained in:
parent
66c669fb24
commit
6a5178b665
4 changed files with 33 additions and 17 deletions
|
@ -4,16 +4,16 @@
|
|||
|
||||
struct ButtonState
|
||||
{
|
||||
bool Up;
|
||||
bool Down;
|
||||
bool Left;
|
||||
bool Right;
|
||||
bool Up = false;
|
||||
bool Down = false;
|
||||
bool Left = false;
|
||||
bool Right = false;
|
||||
|
||||
bool A;
|
||||
bool B;
|
||||
bool A = false;
|
||||
bool B = false;
|
||||
|
||||
bool Select;
|
||||
bool Start;
|
||||
bool Select = false;
|
||||
bool Start = false;
|
||||
};
|
||||
|
||||
class IControlDevice
|
||||
|
|
|
@ -1,22 +1,35 @@
|
|||
#include "stdafx.h"
|
||||
#include "InputManager.h"
|
||||
|
||||
InputManager::InputManager(HWND hWnd)
|
||||
{
|
||||
_hWnd = hWnd;
|
||||
}
|
||||
|
||||
bool InputManager::IsKeyPressed(int key)
|
||||
{
|
||||
return (GetAsyncKeyState(key) & 0x8000) == 0x8000;
|
||||
}
|
||||
|
||||
bool InputManager::WindowHasFocus()
|
||||
{
|
||||
return GetForegroundWindow() == _hWnd;
|
||||
}
|
||||
|
||||
ButtonState InputManager::GetButtonState()
|
||||
{
|
||||
ButtonState state;
|
||||
state.A = IsKeyPressed('A') || _gamePad.IsPressed(XINPUT_GAMEPAD_A);
|
||||
state.B = IsKeyPressed('S') || _gamePad.IsPressed(XINPUT_GAMEPAD_X);
|
||||
state.Select = IsKeyPressed('W') || _gamePad.IsPressed(XINPUT_GAMEPAD_BACK);
|
||||
state.Start = IsKeyPressed('Q') || _gamePad.IsPressed(XINPUT_GAMEPAD_START);
|
||||
state.Up = IsKeyPressed(VK_UP) || _gamePad.IsPressed(XINPUT_GAMEPAD_DPAD_UP);
|
||||
state.Down = IsKeyPressed(VK_DOWN) || _gamePad.IsPressed(XINPUT_GAMEPAD_DPAD_DOWN);
|
||||
state.Left = IsKeyPressed(VK_LEFT) || _gamePad.IsPressed(XINPUT_GAMEPAD_DPAD_LEFT);
|
||||
state.Right = IsKeyPressed(VK_RIGHT) || _gamePad.IsPressed(XINPUT_GAMEPAD_DPAD_RIGHT);
|
||||
|
||||
if(WindowHasFocus()) {
|
||||
state.A = IsKeyPressed('A') || _gamePad.IsPressed(XINPUT_GAMEPAD_A);
|
||||
state.B = IsKeyPressed('S') || _gamePad.IsPressed(XINPUT_GAMEPAD_X);
|
||||
state.Select = IsKeyPressed('W') || _gamePad.IsPressed(XINPUT_GAMEPAD_BACK);
|
||||
state.Start = IsKeyPressed('Q') || _gamePad.IsPressed(XINPUT_GAMEPAD_START);
|
||||
state.Up = IsKeyPressed(VK_UP) || _gamePad.IsPressed(XINPUT_GAMEPAD_DPAD_UP);
|
||||
state.Down = IsKeyPressed(VK_DOWN) || _gamePad.IsPressed(XINPUT_GAMEPAD_DPAD_DOWN);
|
||||
state.Left = IsKeyPressed(VK_LEFT) || _gamePad.IsPressed(XINPUT_GAMEPAD_DPAD_LEFT);
|
||||
state.Right = IsKeyPressed(VK_RIGHT) || _gamePad.IsPressed(XINPUT_GAMEPAD_DPAD_RIGHT);
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
|
|
@ -8,9 +8,12 @@ class InputManager : public IControlDevice
|
|||
{
|
||||
private:
|
||||
GamePad _gamePad;
|
||||
HWND _hWnd;
|
||||
|
||||
bool IsKeyPressed(int key);
|
||||
bool WindowHasFocus();
|
||||
|
||||
public:
|
||||
InputManager(HWND hWnd);
|
||||
ButtonState GetButtonState();
|
||||
};
|
|
@ -65,7 +65,7 @@ namespace NES {
|
|||
Initialize();
|
||||
|
||||
InitializeOptions();
|
||||
InputManager inputManager;
|
||||
InputManager inputManager(_hWnd);
|
||||
ControlManager::RegisterControlDevice(&inputManager, 0);
|
||||
|
||||
HACCEL hAccel = LoadAccelerators(_hInstance, MAKEINTRESOURCE(IDC_Accelerator));
|
||||
|
|
Loading…
Add table
Reference in a new issue