1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Diagnostics ;
3
4
using System . Drawing ;
4
5
using System . Linq ;
@@ -33,10 +34,15 @@ private static void Run(string[]? args = null)
33
34
window . Run ( ) ;
34
35
}
35
36
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
+ static List < int > _scancodesDown = new List < int > ( ) ;
40
+
36
41
public static Action OnLoad ( IView window ) =>
37
42
( ) =>
38
43
{
39
44
var input = window . CreateInput ( ) ;
45
+ window . Update += OnUpdate ( input ) ;
40
46
input . ConnectionChanged += DoConnect ;
41
47
Console . WriteLine ( "Now, go press buttons in the window and you'll see the feedback here." ) ;
42
48
foreach ( var gamepad in input . Gamepads )
@@ -69,44 +75,66 @@ public static Action OnLoad(IView window) =>
69
75
//};
70
76
} ;
71
77
72
- private static void GamepadOnTriggerMoved ( IGamepad g , Trigger t )
78
+ public static Action < double > OnUpdate ( IInputContext input ) =>
79
+ ( dt ) =>
80
+ {
81
+ foreach ( Key k in _allKeys )
82
+ {
83
+ // This should really be separated into different keyboards, but since all current backends
84
+ // just report a single keyboard we'll leave this test as is for now.
85
+ if ( _keysDown . Contains ( k ) != input . Keyboards . Any ( x => x . IsKeyPressed ( k ) ) )
86
+ {
87
+ Console . WriteLine ( $ "[ERROR] IsKeyPressed() and event mismatch! Key { k } ") ;
88
+ }
89
+ }
90
+
91
+ for ( int s = 0 ; s < 256 ; s ++ )
92
+ {
93
+ if ( _scancodesDown . Contains ( s ) != input . Keyboards . Any ( x => x . IsScancodePressed ( s ) ) )
94
+ {
95
+ Console . WriteLine ( $ "[ERROR] IsScancodePressed() and event mismatch! Scancode { s } ") ;
96
+ }
97
+ }
98
+ } ;
99
+
100
+ private static void GamepadOnTriggerMoved ( IGamepad gamepad , Trigger trigger )
73
101
{
74
- Console . WriteLine ( $ "G{ g . Index } > { t . Index } trigger moved: { t . Position } ") ;
102
+ Console . WriteLine ( $ "G{ gamepad . Index } > { trigger . Index } trigger moved: { trigger . Position } ") ;
75
103
}
76
104
77
- private static void GamepadOnThumbstickMoved ( IGamepad g , Thumbstick t )
105
+ private static void GamepadOnThumbstickMoved ( IGamepad gamepad , Thumbstick thumbstick )
78
106
{
79
- Console . WriteLine ( $ "G{ g . Index } > { t . Index } thumbstick moved: ({ t . X } , { t . Y } )") ;
107
+ Console . WriteLine ( $ "G{ gamepad . Index } > { thumbstick . Index } thumbstick moved: ({ thumbstick . X } , { thumbstick . Y } )") ;
80
108
}
81
109
82
- private static void JoystickOnHatMoved ( IJoystick arg1 , Hat arg2 )
110
+ private static void JoystickOnHatMoved ( IJoystick joystick , Hat hat )
83
111
{
84
- Console . WriteLine ( $ "J{ arg1 . Index } > { arg2 . Index } hat moved: { arg2 . Position } ") ;
112
+ Console . WriteLine ( $ "J{ joystick . Index } > { hat . Index } hat moved: { hat . Position } ") ;
85
113
}
86
114
87
- private static void JoystickOnAxisMoved ( IJoystick arg1 , Axis arg2 )
115
+ private static void JoystickOnAxisMoved ( IJoystick joystick , Axis axis )
88
116
{
89
- Console . WriteLine ( $ "J{ arg1 . Index } > { arg2 . Index } axis moved: { arg2 . Position } ") ;
117
+ Console . WriteLine ( $ "J{ joystick . Index } > { axis . Index } axis moved: { axis . Position } ") ;
90
118
}
91
119
92
- private static void JoystickOnButtonUp ( IJoystick arg1 , Button arg2 )
120
+ private static void JoystickOnButtonUp ( IJoystick joystick , Button button )
93
121
{
94
- Console . WriteLine ( $ "J{ arg1 . Index } > { arg2 . Name } down.") ;
122
+ Console . WriteLine ( $ "J{ joystick . Index } > { button . Name } down.") ;
95
123
}
96
124
97
- private static void JoystickOnButtonDown ( IJoystick arg1 , Button arg2 )
125
+ private static void JoystickOnButtonDown ( IJoystick joystick , Button button )
98
126
{
99
- Console . WriteLine ( $ "J{ arg1 . Index } > { arg2 . Name } down.") ;
127
+ Console . WriteLine ( $ "J{ joystick . Index } > { button . Name } down.") ;
100
128
}
101
129
102
- private static void InputGamepadOnButtonDown ( IGamepad arg1 , Button arg2 )
130
+ private static void InputGamepadOnButtonDown ( IGamepad gamepad , Button button )
103
131
{
104
- Console . WriteLine ( $ "G{ arg1 . Index } > { arg2 . Name } down. { ( int ) arg2 . Name } ") ;
132
+ Console . WriteLine ( $ "G{ gamepad . Index } > { button . Name } down. { ( int ) button . Name } ") ;
105
133
}
106
134
107
- private static void InputGamepadOnButtonUp ( IGamepad arg1 , Button arg2 )
135
+ private static void InputGamepadOnButtonUp ( IGamepad gamepad , Button button )
108
136
{
109
- Console . WriteLine ( $ "G{ arg1 . Index } > { arg2 . Name } up.") ;
137
+ Console . WriteLine ( $ "G{ gamepad . Index } > { button . Name } up.") ;
110
138
}
111
139
112
140
public static unsafe void DoConnect ( IInputDevice device , bool isConnected )
@@ -214,49 +242,77 @@ public static unsafe void DoConnect(IInputDevice device, bool isConnected)
214
242
}
215
243
}
216
244
217
- private static void KeyboardOnKeyChar ( IKeyboard arg1 , char arg2 )
245
+ private static void KeyboardOnKeyChar ( IKeyboard keyboard , char c )
218
246
{
219
- Console . WriteLine ( $ "K{ arg1 . Index } > { arg2 } received.") ;
247
+ Console . WriteLine ( $ "K{ keyboard . Index } > { c } received.") ;
220
248
}
221
249
222
- private static void MouseOnMouseMove ( IMouse arg1 , Vector2 arg2 )
250
+ private static void MouseOnMouseMove ( IMouse mouse , Vector2 pos )
223
251
{
224
- Console . WriteLine ( $ "M{ arg1 . Index } > Moved: { arg2 } ") ;
252
+ Console . WriteLine ( $ "M{ mouse . Index } > Moved: { pos } ") ;
225
253
}
226
254
227
- private static void MouseOnScroll ( IMouse arg1 , ScrollWheel arg2 )
255
+ private static void MouseOnScroll ( IMouse mouse , ScrollWheel sw )
228
256
{
229
- Console . WriteLine ( $ "K{ arg1 . Index } > Scrolled: ({ arg2 . X } , { arg2 . Y } )") ;
257
+ Console . WriteLine ( $ "K{ mouse . Index } > Scrolled: ({ sw . X } , { sw . Y } )") ;
230
258
}
231
259
232
- private static void MouseOnMouseDown ( IMouse arg1 , MouseButton arg2 )
260
+ private static void MouseOnMouseDown ( IMouse mouse , MouseButton button )
233
261
{
234
- Console . WriteLine ( $ "M{ arg1 . Index } > { arg2 } down.") ;
262
+ Console . WriteLine ( $ "M{ mouse . Index } > { button } down.") ;
235
263
}
236
264
237
- private static void MouseOnMouseUp ( IMouse arg1 , MouseButton arg2 )
265
+ private static void MouseOnMouseUp ( IMouse mouse , MouseButton button )
238
266
{
239
- Console . WriteLine ( $ "M{ arg1 . Index } > { arg2 } up.") ;
267
+ Console . WriteLine ( $ "M{ mouse . Index } > { button } up.") ;
240
268
}
241
269
242
- private static void MouseOnClick ( IMouse arg1 , MouseButton arg2 , Vector2 pos )
270
+ private static void MouseOnClick ( IMouse mouse , MouseButton button , Vector2 pos )
243
271
{
244
- Console . WriteLine ( $ "M{ arg1 . Index } > { arg2 } single click.") ;
272
+ Console . WriteLine ( $ "M{ mouse . Index } > { button } single click.") ;
245
273
}
246
274
247
- private static void MouseOnDoubleClick ( IMouse arg1 , MouseButton arg2 , Vector2 pos )
275
+ private static void MouseOnDoubleClick ( IMouse mouse , MouseButton button , Vector2 pos )
248
276
{
249
- Console . WriteLine ( $ "M{ arg1 . Index } > { arg2 } double click.") ;
277
+ Console . WriteLine ( $ "M{ mouse . Index } > { button } double click.") ;
250
278
}
251
279
252
- private static void KeyboardOnKeyUp ( IKeyboard arg1 , Key arg2 , int _ )
280
+ private static void KeyboardOnKeyUp ( IKeyboard keyboard , Key key , int scancode )
253
281
{
254
- Console . WriteLine ( $ "K{ arg1 . Index } > { arg2 } up.") ;
282
+ Console . WriteLine ( $ "K{ keyboard . Index } > key={ key } scancode={ scancode } up.") ;
283
+
284
+ if ( key != Key . Unknown )
285
+ {
286
+ int keyIndex = _keysDown . IndexOf ( key ) ;
287
+ if ( keyIndex < 0 )
288
+ Console . WriteLine ( $ "[ERROR] Double key up detected? K{ keyboard . Index } > { key } up.") ;
289
+ else
290
+ _keysDown . RemoveAt ( keyIndex ) ;
291
+ }
292
+
293
+ int scancodeIndex = _scancodesDown . IndexOf ( scancode ) ;
294
+ if ( scancodeIndex < 0 )
295
+ Console . WriteLine ( $ "[ERROR] Double scancode up detected? K{ keyboard . Index } > { scancode } up.") ;
296
+ else
297
+ _scancodesDown . RemoveAt ( scancodeIndex ) ;
255
298
}
256
299
257
- private static void KeyboardOnKeyDown ( IKeyboard arg1 , Key arg2 , int _ )
300
+ private static void KeyboardOnKeyDown ( IKeyboard keyboard , Key key , int scancode )
258
301
{
259
- Console . WriteLine ( $ "K{ arg1 . Index } > { arg2 } down.") ;
302
+ Console . WriteLine ( $ "K{ keyboard . Index } > key={ key } scancode={ scancode } down.") ;
303
+
304
+ if ( key != Key . Unknown )
305
+ {
306
+ if ( _keysDown . Contains ( key ) )
307
+ Console . WriteLine ( $ "[ERROR] Double key down detected? K{ keyboard . Index } > { key } down.") ;
308
+ else
309
+ _keysDown . Add ( key ) ;
310
+ }
311
+
312
+ if ( _scancodesDown . Contains ( scancode ) )
313
+ Console . WriteLine ( $ "[ERROR] Double scancode down detected? K{ keyboard . Index } > { scancode } down.") ;
314
+ else
315
+ _scancodesDown . Add ( scancode ) ;
260
316
}
261
317
}
262
- }
318
+ }
0 commit comments