Skip to content

Commit 88294d2

Browse files
committed
Add IKeyboard.IsScancodePressed and implement it on backends
1 parent 7579993 commit 88294d2

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

src/Input/Silk.NET.Input.Common/Interfaces/IKeyboard.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ public interface IKeyboard : IInputDevice
2828
/// <returns>Whether or not the key is pressed.</returns>
2929
bool IsKeyPressed(Key key);
3030

31+
/// <summary>
32+
/// Checks if a specific scancode is pressed.
33+
/// </summary>
34+
/// <param name="scancode">The scancode to check.</param>
35+
/// <returns>Whether or not the scancode is pressed.</returns>
36+
bool IsScancodePressed(int scancode);
37+
3138
/// <summary>
3239
/// Called when a key is pressed.
3340
/// </summary>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
#nullable enable
2+
Silk.NET.Input.IKeyboard.IsScancodePressed(int scancode) -> bool

src/Input/Silk.NET.Input.Glfw/GlfwKeyboard.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ internal class GlfwKeyboard : IKeyboard, IGlfwSubscriber
1818
private unsafe WindowHandle* _handle;
1919
private GlfwCallbacks.CharCallback? _char;
2020
private GlfwCallbacks.KeyCallback? _key;
21+
private List<int> _scancodesDown = new List<int>();
22+
2123
public string Name { get; } = "Silk.NET Keyboard (via GLFW)";
2224
public int Index { get; } = 0;
2325
public bool IsConnected { get; } = true;
@@ -31,6 +33,8 @@ public unsafe string ClipboardText
3133
public unsafe bool IsKeyPressed
3234
(Key key) => GlfwProvider.GLFW.Value.GetKey(_handle, ConvertKey(key)) == (int) InputAction.Press;
3335

36+
public unsafe bool IsScancodePressed(int scancode) => _scancodesDown.Contains(scancode);
37+
3438
public event Action<IKeyboard, Key, int>? KeyDown;
3539
public event Action<IKeyboard, Key, int>? KeyUp;
3640
public event Action<IKeyboard, char>? KeyChar;
@@ -49,13 +53,22 @@ public unsafe void Subscribe(GlfwEvents events)
4953
_handle = events.Handle;
5054
events.Char += _char = (_, c) => KeyChar?.Invoke(this, (char) c);
5155
events.Key += _key = (_, key, code, action, mods) =>
52-
(action switch
56+
{
57+
Action<IKeyboard, Key, int>? evt = null;
58+
switch(action)
5359
{
54-
InputAction.Press => KeyDown,
55-
InputAction.Release => KeyUp,
56-
InputAction.Repeat => null,
57-
_ => null
58-
})?.Invoke(this, ConvertKey(key), code);
60+
case InputAction.Press:
61+
evt = KeyDown;
62+
_scancodesDown.Add(code);
63+
break;
64+
case InputAction.Release:
65+
evt = KeyUp;
66+
_scancodesDown.Remove(code);
67+
break;
68+
}
69+
70+
evt?.Invoke(this, ConvertKey(key), code);
71+
};
5972
}
6073

6174
public void Unsubscribe(GlfwEvents events)

src/Input/Silk.NET.Input.Sdl/SdlKeyboard.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Silk.NET.Input.Sdl
99
internal partial class SdlKeyboard : IKeyboard, ISdlDevice
1010
{
1111
private readonly SdlInputContext _ctx;
12-
private List<Key> _keysDown = new List<Key>();
12+
private List<Scancode> _scancodesDown = new List<Scancode>();
1313

1414
public SdlKeyboard(SdlInputContext ctx)
1515
{
@@ -27,7 +27,8 @@ public string ClipboardText
2727
get => _ctx.Sdl.GetClipboardTextS();
2828
set => _ctx.Sdl.SetClipboardText(value);
2929
}
30-
public bool IsKeyPressed(Key key) => _keysDown.Contains(key);
30+
public bool IsKeyPressed(Key key) => _scancodesDown.Any(x => _keyMap.TryGetValue(x, out Key skey) && key == skey);
31+
public bool IsScancodePressed(int scancode) => _scancodesDown.Contains((Scancode) scancode);
3132
public event Action<IKeyboard, Key, int>? KeyDown;
3233
public event Action<IKeyboard, Key, int>? KeyUp;
3334
public event Action<IKeyboard, char>? KeyChar;
@@ -46,13 +47,13 @@ public unsafe void DoEvent(Event @event)
4647
if (_keyMap.TryGetValue(@event.Key.Keysym.Scancode, out var key))
4748
{
4849
keyDown = key;
49-
_keysDown.Add(keyDown);
5050
}
5151
else
5252
{
5353
keyDown = Key.Unknown;
5454
}
5555

56+
_scancodesDown.Add(@event.Key.Keysym.Scancode);
5657
KeyDown?.Invoke(this, keyDown, (int) @event.Key.Keysym.Scancode);
5758
}
5859

@@ -66,13 +67,13 @@ public unsafe void DoEvent(Event @event)
6667
if (_keyMap.TryGetValue(@event.Key.Keysym.Scancode, out var key))
6768
{
6869
keyUp = key;
69-
_keysDown.Remove(keyUp);
7070
}
7171
else
7272
{
7373
keyUp = Key.Unknown;
7474
}
7575

76+
_scancodesDown.Remove(@event.Key.Keysym.Scancode);
7677
KeyUp?.Invoke(this, keyUp, (int) @event.Key.Keysym.Scancode);
7778
}
7879

0 commit comments

Comments
 (0)