feat: recognize keyboard keys as "pressed" only after key up occurred

Co-authored-by: Nico <nico.reinartz@rwth-aachen.de>
This commit is contained in:
Isaac Marovitz 2024-02-28 12:58:26 -05:00
parent cfcb9921bc
commit a46e464003
No known key found for this signature in database
GPG key ID: 97250B2B09A132E1

View file

@ -6,7 +6,7 @@ namespace Ryujinx.Input.Assigner
public class KeyboardKeyAssigner : IButtonAssigner public class KeyboardKeyAssigner : IButtonAssigner
{ {
private readonly IKeyboard _keyboard; private readonly IKeyboard _keyboard;
private ButtonValue? _pressedKey;
private KeyboardStateSnapshot _keyboardState; private KeyboardStateSnapshot _keyboardState;
public KeyboardKeyAssigner(IKeyboard keyboard) public KeyboardKeyAssigner(IKeyboard keyboard)
@ -14,16 +14,26 @@ namespace Ryujinx.Input.Assigner
_keyboard = keyboard; _keyboard = keyboard;
} }
public void Initialize() { } public void Initialize()
{
_pressedKey = null;
}
public void ReadInput() public void ReadInput()
{ {
_keyboardState = _keyboard.GetKeyboardStateSnapshot(); var newKeyboardState = _keyboard.GetKeyboardStateSnapshot();
if (_keyboardState != null)
{
DetectPressedKeys(_keyboardState, newKeyboardState);
}
_keyboardState = newKeyboardState;
} }
public bool HasAnyButtonPressed() public bool HasAnyButtonPressed()
{ {
return GetPressedButton() is not null; return _pressedKey != null;
} }
public bool ShouldCancel() public bool ShouldCancel()
@ -33,18 +43,19 @@ namespace Ryujinx.Input.Assigner
public ButtonValue? GetPressedButton() public ButtonValue? GetPressedButton()
{ {
ButtonValue? keyPressed = null; return !ShouldCancel() && HasAnyButtonPressed() ? _pressedKey : null;
}
public void DetectPressedKeys(KeyboardStateSnapshot oldState, KeyboardStateSnapshot newState)
{
for (Key key = Key.Unknown; key < Key.Count; key++) for (Key key = Key.Unknown; key < Key.Count; key++)
{ {
if (_keyboardState.IsPressed(key)) if (oldState.IsPressed(key) && !newState.IsPressed(key))
{ {
keyPressed = new(key); _pressedKey = new(key);
break; break;
} }
} }
return !ShouldCancel() ? keyPressed : null;
} }
} }
} }