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,14 @@ 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
+
36
40
public static Action OnLoad ( IView window ) =>
37
41
( ) =>
38
42
{
39
43
var input = window . CreateInput ( ) ;
44
+ window . Update += OnUpdate ( input ) ;
40
45
input . ConnectionChanged += DoConnect ;
41
46
Console . WriteLine ( "Now, go press buttons in the window and you'll see the feedback here." ) ;
42
47
foreach ( var gamepad in input . Gamepads )
@@ -69,44 +74,58 @@ public static Action OnLoad(IView window) =>
69
74
//};
70
75
} ;
71
76
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 )
73
92
{
74
- Console . WriteLine ( $ "G{ g . Index } > { t . Index } trigger moved: { t . Position } ") ;
93
+ Console . WriteLine ( $ "G{ gamepad . Index } > { trigger . Index } trigger moved: { trigger . Position } ") ;
75
94
}
76
95
77
- private static void GamepadOnThumbstickMoved ( IGamepad g , Thumbstick t )
96
+ private static void GamepadOnThumbstickMoved ( IGamepad gamepad , Thumbstick thumbstick )
78
97
{
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 } )") ;
80
99
}
81
100
82
- private static void JoystickOnHatMoved ( IJoystick arg1 , Hat arg2 )
101
+ private static void JoystickOnHatMoved ( IJoystick joystick , Hat hat )
83
102
{
84
- Console . WriteLine ( $ "J{ arg1 . Index } > { arg2 . Index } hat moved: { arg2 . Position } ") ;
103
+ Console . WriteLine ( $ "J{ joystick . Index } > { hat . Index } hat moved: { hat . Position } ") ;
85
104
}
86
105
87
- private static void JoystickOnAxisMoved ( IJoystick arg1 , Axis arg2 )
106
+ private static void JoystickOnAxisMoved ( IJoystick joystick , Axis axis )
88
107
{
89
- Console . WriteLine ( $ "J{ arg1 . Index } > { arg2 . Index } axis moved: { arg2 . Position } ") ;
108
+ Console . WriteLine ( $ "J{ joystick . Index } > { axis . Index } axis moved: { axis . Position } ") ;
90
109
}
91
110
92
- private static void JoystickOnButtonUp ( IJoystick arg1 , Button arg2 )
111
+ private static void JoystickOnButtonUp ( IJoystick joystick , Button button )
93
112
{
94
- Console . WriteLine ( $ "J{ arg1 . Index } > { arg2 . Name } down.") ;
113
+ Console . WriteLine ( $ "J{ joystick . Index } > { button . Name } down.") ;
95
114
}
96
115
97
- private static void JoystickOnButtonDown ( IJoystick arg1 , Button arg2 )
116
+ private static void JoystickOnButtonDown ( IJoystick joystick , Button button )
98
117
{
99
- Console . WriteLine ( $ "J{ arg1 . Index } > { arg2 . Name } down.") ;
118
+ Console . WriteLine ( $ "J{ joystick . Index } > { button . Name } down.") ;
100
119
}
101
120
102
- private static void InputGamepadOnButtonDown ( IGamepad arg1 , Button arg2 )
121
+ private static void InputGamepadOnButtonDown ( IGamepad gamepad , Button button )
103
122
{
104
- Console . WriteLine ( $ "G{ arg1 . Index } > { arg2 . Name } down. { ( int ) arg2 . Name } ") ;
123
+ Console . WriteLine ( $ "G{ gamepad . Index } > { button . Name } down. { ( int ) button . Name } ") ;
105
124
}
106
125
107
- private static void InputGamepadOnButtonUp ( IGamepad arg1 , Button arg2 )
126
+ private static void InputGamepadOnButtonUp ( IGamepad gamepad , Button button )
108
127
{
109
- Console . WriteLine ( $ "G{ arg1 . Index } > { arg2 . Name } up.") ;
128
+ Console . WriteLine ( $ "G{ gamepad . Index } > { button . Name } up.") ;
110
129
}
111
130
112
131
public static unsafe void DoConnect ( IInputDevice device , bool isConnected )
@@ -214,49 +233,66 @@ public static unsafe void DoConnect(IInputDevice device, bool isConnected)
214
233
}
215
234
}
216
235
217
- private static void KeyboardOnKeyChar ( IKeyboard arg1 , char arg2 )
236
+ private static void KeyboardOnKeyChar ( IKeyboard keyboard , char c )
218
237
{
219
- Console . WriteLine ( $ "K{ arg1 . Index } > { arg2 } received.") ;
238
+ Console . WriteLine ( $ "K{ keyboard . Index } > { c } received.") ;
220
239
}
221
240
222
- private static void MouseOnMouseMove ( IMouse arg1 , Vector2 arg2 )
241
+ private static void MouseOnMouseMove ( IMouse mouse , Vector2 pos )
223
242
{
224
- Console . WriteLine ( $ "M{ arg1 . Index } > Moved: { arg2 } ") ;
243
+ Console . WriteLine ( $ "M{ mouse . Index } > Moved: { pos } ") ;
225
244
}
226
245
227
- private static void MouseOnScroll ( IMouse arg1 , ScrollWheel arg2 )
246
+ private static void MouseOnScroll ( IMouse mouse , ScrollWheel sw )
228
247
{
229
- Console . WriteLine ( $ "K{ arg1 . Index } > Scrolled: ({ arg2 . X } , { arg2 . Y } )") ;
248
+ Console . WriteLine ( $ "K{ mouse . Index } > Scrolled: ({ sw . X } , { sw . Y } )") ;
230
249
}
231
250
232
- private static void MouseOnMouseDown ( IMouse arg1 , MouseButton arg2 )
251
+ private static void MouseOnMouseDown ( IMouse mouse , MouseButton button )
233
252
{
234
- Console . WriteLine ( $ "M{ arg1 . Index } > { arg2 } down.") ;
253
+ Console . WriteLine ( $ "M{ mouse . Index } > { button } down.") ;
235
254
}
236
255
237
- private static void MouseOnMouseUp ( IMouse arg1 , MouseButton arg2 )
256
+ private static void MouseOnMouseUp ( IMouse mouse , MouseButton button )
238
257
{
239
- Console . WriteLine ( $ "M{ arg1 . Index } > { arg2 } up.") ;
258
+ Console . WriteLine ( $ "M{ mouse . Index } > { button } up.") ;
240
259
}
241
260
242
- private static void MouseOnClick ( IMouse arg1 , MouseButton arg2 , Vector2 pos )
261
+ private static void MouseOnClick ( IMouse mouse , MouseButton button , Vector2 pos )
243
262
{
244
- Console . WriteLine ( $ "M{ arg1 . Index } > { arg2 } single click.") ;
263
+ Console . WriteLine ( $ "M{ mouse . Index } > { button } single click.") ;
245
264
}
246
265
247
- private static void MouseOnDoubleClick ( IMouse arg1 , MouseButton arg2 , Vector2 pos )
266
+ private static void MouseOnDoubleClick ( IMouse mouse , MouseButton button , Vector2 pos )
248
267
{
249
- Console . WriteLine ( $ "M{ arg1 . Index } > { arg2 } double click.") ;
268
+ Console . WriteLine ( $ "M{ mouse . Index } > { button } double click.") ;
250
269
}
251
270
252
- private static void KeyboardOnKeyUp ( IKeyboard arg1 , Key arg2 , int _ )
271
+ private static void KeyboardOnKeyUp ( IKeyboard keyboard , Key key , int scancode )
253
272
{
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
+ }
255
283
}
256
284
257
- private static void KeyboardOnKeyDown ( IKeyboard arg1 , Key arg2 , int _ )
285
+ private static void KeyboardOnKeyDown ( IKeyboard keyboard , Key key , int scancode )
258
286
{
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
+ }
260
296
}
261
297
}
262
- }
298
+ }
0 commit comments