Skip to content

Commit 7579993

Browse files
committed
Modify Lab/InputTest to also test keyboard key polling
1 parent 5656b0d commit 7579993

File tree

1 file changed

+71
-35
lines changed

1 file changed

+71
-35
lines changed

src/Lab/Experiments/InputTest/Program.cs

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.Drawing;
45
using System.Linq;
@@ -33,10 +34,14 @@ private static void Run(string[]? args = null)
3334
window.Run();
3435
}
3536

37+
static Key[] _allKeys = Enum.GetValues(typeof(Key)).Cast<Key>().Where(x => x != Key.Unknown).ToArray();
38+
static List<Key> _keysDown = new List<Key>();
39+
3640
public static Action OnLoad(IView window) =>
3741
() =>
3842
{
3943
var input = window.CreateInput();
44+
window.Update += OnUpdate(input);
4045
input.ConnectionChanged += DoConnect;
4146
Console.WriteLine("Now, go press buttons in the window and you'll see the feedback here.");
4247
foreach (var gamepad in input.Gamepads)
@@ -69,44 +74,58 @@ public static Action OnLoad(IView window) =>
6974
//};
7075
};
7176

72-
private static void GamepadOnTriggerMoved(IGamepad g, Trigger t)
77+
public static Action<double> OnUpdate(IInputContext input) =>
78+
(dt) =>
79+
{
80+
foreach (Key k in _allKeys)
81+
{
82+
// This should really be separated into different keyboards, but since all current backends
83+
// just report a single keyboard we'll leave this test as is for now.
84+
if (_keysDown.Contains(k) != input.Keyboards.Any(x => x.IsKeyPressed(k)))
85+
{
86+
Console.WriteLine($"[ERROR] IsKeyPressed() and event mismatch! Key {k}");
87+
}
88+
}
89+
};
90+
91+
private static void GamepadOnTriggerMoved(IGamepad gamepad, Trigger trigger)
7392
{
74-
Console.WriteLine($"G{g.Index}> {t.Index} trigger moved: {t.Position}");
93+
Console.WriteLine($"G{gamepad.Index}> {trigger.Index} trigger moved: {trigger.Position}");
7594
}
7695

77-
private static void GamepadOnThumbstickMoved(IGamepad g, Thumbstick t)
96+
private static void GamepadOnThumbstickMoved(IGamepad gamepad, Thumbstick thumbstick)
7897
{
79-
Console.WriteLine($"G{g.Index}> {t.Index} thumbstick moved: ({t.X}, {t.Y})");
98+
Console.WriteLine($"G{gamepad.Index}> {thumbstick.Index} thumbstick moved: ({thumbstick.X}, {thumbstick.Y})");
8099
}
81100

82-
private static void JoystickOnHatMoved(IJoystick arg1, Hat arg2)
101+
private static void JoystickOnHatMoved(IJoystick joystick, Hat hat)
83102
{
84-
Console.WriteLine($"J{arg1.Index}> {arg2.Index} hat moved: {arg2.Position}");
103+
Console.WriteLine($"J{joystick.Index}> {hat.Index} hat moved: {hat.Position}");
85104
}
86105

87-
private static void JoystickOnAxisMoved(IJoystick arg1, Axis arg2)
106+
private static void JoystickOnAxisMoved(IJoystick joystick, Axis axis)
88107
{
89-
Console.WriteLine($"J{arg1.Index}> {arg2.Index} axis moved: {arg2.Position}");
108+
Console.WriteLine($"J{joystick.Index}> {axis.Index} axis moved: {axis.Position}");
90109
}
91110

92-
private static void JoystickOnButtonUp(IJoystick arg1, Button arg2)
111+
private static void JoystickOnButtonUp(IJoystick joystick, Button button)
93112
{
94-
Console.WriteLine($"J{arg1.Index}> {arg2.Name} down.");
113+
Console.WriteLine($"J{joystick.Index}> {button.Name} down.");
95114
}
96115

97-
private static void JoystickOnButtonDown(IJoystick arg1, Button arg2)
116+
private static void JoystickOnButtonDown(IJoystick joystick, Button button)
98117
{
99-
Console.WriteLine($"J{arg1.Index}> {arg2.Name} down.");
118+
Console.WriteLine($"J{joystick.Index}> {button.Name} down.");
100119
}
101120

102-
private static void InputGamepadOnButtonDown(IGamepad arg1, Button arg2)
121+
private static void InputGamepadOnButtonDown(IGamepad gamepad, Button button)
103122
{
104-
Console.WriteLine($"G{arg1.Index}> {arg2.Name} down. {(int) arg2.Name}");
123+
Console.WriteLine($"G{gamepad.Index}> {button.Name} down. {(int) button.Name}");
105124
}
106125

107-
private static void InputGamepadOnButtonUp(IGamepad arg1, Button arg2)
126+
private static void InputGamepadOnButtonUp(IGamepad gamepad, Button button)
108127
{
109-
Console.WriteLine($"G{arg1.Index}> {arg2.Name} up.");
128+
Console.WriteLine($"G{gamepad.Index}> {button.Name} up.");
110129
}
111130

112131
public static unsafe void DoConnect(IInputDevice device, bool isConnected)
@@ -214,49 +233,66 @@ public static unsafe void DoConnect(IInputDevice device, bool isConnected)
214233
}
215234
}
216235

217-
private static void KeyboardOnKeyChar(IKeyboard arg1, char arg2)
236+
private static void KeyboardOnKeyChar(IKeyboard keyboard, char c)
218237
{
219-
Console.WriteLine($"K{arg1.Index}> {arg2} received.");
238+
Console.WriteLine($"K{keyboard.Index}> {c} received.");
220239
}
221240

222-
private static void MouseOnMouseMove(IMouse arg1, Vector2 arg2)
241+
private static void MouseOnMouseMove(IMouse mouse, Vector2 pos)
223242
{
224-
Console.WriteLine($"M{arg1.Index}> Moved: {arg2}");
243+
Console.WriteLine($"M{mouse.Index}> Moved: {pos}");
225244
}
226245

227-
private static void MouseOnScroll(IMouse arg1, ScrollWheel arg2)
246+
private static void MouseOnScroll(IMouse mouse, ScrollWheel sw)
228247
{
229-
Console.WriteLine($"K{arg1.Index}> Scrolled: ({arg2.X}, {arg2.Y})");
248+
Console.WriteLine($"K{mouse.Index}> Scrolled: ({sw.X}, {sw.Y})");
230249
}
231250

232-
private static void MouseOnMouseDown(IMouse arg1, MouseButton arg2)
251+
private static void MouseOnMouseDown(IMouse mouse, MouseButton button)
233252
{
234-
Console.WriteLine($"M{arg1.Index}> {arg2} down.");
253+
Console.WriteLine($"M{mouse.Index}> {button} down.");
235254
}
236255

237-
private static void MouseOnMouseUp(IMouse arg1, MouseButton arg2)
256+
private static void MouseOnMouseUp(IMouse mouse, MouseButton button)
238257
{
239-
Console.WriteLine($"M{arg1.Index}> {arg2} up.");
258+
Console.WriteLine($"M{mouse.Index}> {button} up.");
240259
}
241260

242-
private static void MouseOnClick(IMouse arg1, MouseButton arg2, Vector2 pos)
261+
private static void MouseOnClick(IMouse mouse, MouseButton button, Vector2 pos)
243262
{
244-
Console.WriteLine($"M{arg1.Index}> {arg2} single click.");
263+
Console.WriteLine($"M{mouse.Index}> {button} single click.");
245264
}
246265

247-
private static void MouseOnDoubleClick(IMouse arg1, MouseButton arg2, Vector2 pos)
266+
private static void MouseOnDoubleClick(IMouse mouse, MouseButton button, Vector2 pos)
248267
{
249-
Console.WriteLine($"M{arg1.Index}> {arg2} double click.");
268+
Console.WriteLine($"M{mouse.Index}> {button} double click.");
250269
}
251270

252-
private static void KeyboardOnKeyUp(IKeyboard arg1, Key arg2, int _)
271+
private static void KeyboardOnKeyUp(IKeyboard keyboard, Key key, int scancode)
253272
{
254-
Console.WriteLine($"K{arg1.Index}> {arg2} up.");
273+
Console.WriteLine($"K{keyboard.Index}> key={key} scancode={scancode} up.");
274+
275+
if (key != Key.Unknown)
276+
{
277+
int index = _keysDown.IndexOf(key);
278+
if (index < 0)
279+
Console.WriteLine($"[ERROR] Double key up detected? K{keyboard.Index}> {key} up.");
280+
else
281+
_keysDown.RemoveAt(index);
282+
}
255283
}
256284

257-
private static void KeyboardOnKeyDown(IKeyboard arg1, Key arg2, int _)
285+
private static void KeyboardOnKeyDown(IKeyboard keyboard, Key key, int scancode)
258286
{
259-
Console.WriteLine($"K{arg1.Index}> {arg2} down.");
287+
Console.WriteLine($"K{keyboard.Index}> key={key} scancode={scancode} down.");
288+
289+
if (key != Key.Unknown)
290+
{
291+
if (_keysDown.Contains(key))
292+
Console.WriteLine($"[ERROR] Double key down detected? K{keyboard.Index}> {key} down.");
293+
else
294+
_keysDown.Add(key);
295+
}
260296
}
261297
}
262-
}
298+
}

0 commit comments

Comments
 (0)