Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
KeyboardEventPoller.cs
1using System.Collections.Concurrent;
2using System.Collections.ObjectModel;
3
5
9public sealed class KeyboardEventPoller
10{
14 public static KeyboardEventPoller? Instance { get; private set; }
15
24 public static void Initialize(IKeyboardAdapter adapter)
25 {
26 Instance = new KeyboardEventPoller(adapter);
27 }
28
35 public event Action<KeyDownEventArgs>? KeyDown;
36
37 // Hot path uses int key codes.
38 private readonly Dictionary<int, KeyEventConfiguration> _keyConfigs = new();
39 private readonly Dictionary<int, bool> _previousPressed = new();
40
41 // Apply monitoring changes on the engine thread to avoid ToList() allocations.
42 private readonly ConcurrentQueue<Action> _pendingOps = new();
43
44 private KeyboardEventPoller() { }
45
47 {
48 Adapter = adapter;
49 }
50
54 public IKeyboardAdapter? Adapter { get; private set; }
55
59 public bool PauseAllKeyEvents { get; set; }
60
66 internal void PollForEvents(long tick)
67 {
69 return;
70
71 var adapter = Adapter;
72
73 if (adapter is null)
74 return;
75
76 // Apply any pending adds/removes on the engine thread.
77 while (_pendingOps.TryDequeue(out var op))
78 op();
79
80 var mods = adapter.CurrentKeyboardModifiers;
81
82 foreach (var kvp in _keyConfigs)
83 {
84 int keyCode = kvp.Key;
85 var config = kvp.Value;
86
87 bool currentlyPressed = adapter.IsDown(keyCode);
88 bool previouslyPressed = _previousPressed.TryGetValue(keyCode, out var prev) && prev;
89
90 // Transition: not pressed -> pressed
91 if (currentlyPressed && !previouslyPressed)
92 {
93 _previousPressed[keyCode] = true;
94 config._lastEventTick = tick;
95
96 KeyDown?.Invoke(new KeyDownEventArgs(config, mods, KeyAction.Pressed));
97 continue;
98 }
99
100 // Still pressed -> maybe repeated (throttled)
101 if (currentlyPressed && previouslyPressed)
102 {
103 if (!config.IsPaused && config.ReadyForNextEvent(tick))
104 {
105 config._lastEventTick = tick;
106 KeyDown?.Invoke(new KeyDownEventArgs(config, mods, KeyAction.Repeated));
107 }
108
109 continue;
110 }
111
112 // Transition: pressed -> not pressed
113 if (!currentlyPressed && previouslyPressed)
114 {
115 _previousPressed[keyCode] = false;
116 KeyDown?.Invoke(new KeyDownEventArgs(config, mods, KeyAction.Released));
117 }
118
119 // not pressed && not previously pressed => nothing
120 }
121 }
122
132 int keyCode,
133 string? displayName = null,
134 double timeBetweenEvents = -1,
135 bool isPaused = false)
136 {
137 _pendingOps.Enqueue(() =>
138 {
139 if (timeBetweenEvents < 0)
140 timeBetweenEvents = Engine.Instance.Configuration.TimeBetweenKeyboardEvents;
141
142 var name = string.IsNullOrWhiteSpace(displayName) ? keyCode.ToString() : displayName;
143 var config = new KeyEventConfiguration(name, timeBetweenEvents, isPaused);
144
145 _keyConfigs[keyCode] = config;
146
147 var adapter = Adapter;
148 _previousPressed[keyCode] = adapter?.IsDown(keyCode) ?? false;
149 });
150 }
151
161 public void StopMonitoringKey(int keyCode)
162 {
163 _pendingOps.Enqueue(() =>
164 {
165 _keyConfigs.Remove(keyCode);
166 _previousPressed.Remove(keyCode);
167 });
168 }
169
179 public void StopMonitoringKey(string key)
180 {
181 if (int.TryParse(key, out int code))
182 StopMonitoringKey(code);
183 }
184
192 {
193 _pendingOps.Enqueue(() =>
194 {
195 _keyConfigs.Clear();
196 _previousPressed.Clear();
197 });
198 }
199
203 public IReadOnlyDictionary<int, KeyEventConfiguration> AllKeyConfigs =>
204 new ReadOnlyDictionary<int, KeyEventConfiguration>(_keyConfigs);
205}
Represents the core game engine singleton responsible for managing the main update loop,...
Definition Engine.cs:36
static Engine Instance
Gets the singleton instance of the Engine.
Definition Engine.cs:49
EngineConfiguration Configuration
Gets the current engine configuration settings.
Definition Engine.cs:952
Provides data for keyboard key events, including information about the key that was pressed,...
Represents the configuration for keyboard key events, including key identification and timing control...
Cross-platform singleton keyboard handler with throttling and manual input state feeding.
void StopMonitoringAllKeys()
Unregisters all keys from event monitoring, clearing all key configurations and state tracking....
bool PauseAllKeyEvents
Pauses all key events globally.
IReadOnlyDictionary< int, KeyEventConfiguration > AllKeyConfigs
Exposes monitored key configurations. Treat as engine-thread-only unless you add external synchroniza...
void StopMonitoringKey(int keyCode)
Unregisters a specific key from event monitoring by its platform-agnostic key code....
void StopMonitoringKey(string key)
Unregisters a specific key from event monitoring by parsing its string representation....
static void Initialize(IKeyboardAdapter adapter)
Initializes the singleton instance of the KeyboardEventPoller with the specified keyboard adapter....
Action< KeyDownEventArgs >? KeyDown
Occurs when a monitored keyboard key is pressed, released, or repeated based on its configuration and...
void StartMonitoringKey(int keyCode, string? displayName=null, double timeBetweenEvents=-1, bool isPaused=false)
Primary API: monitor by platform-agnostic key code. Platform layers (WinForms, SDL,...
IKeyboardAdapter? Adapter
Gets the current keyboard adapter in use.
static ? KeyboardEventPoller Instance
Singleton instance of the KeyboardEventPoller class.
Represents an adapter for keyboard input, providing access to the currently pressed keys and active m...
KeyAction
Represents the type of action performed on a keyboard key, indicating whether the key was pressed,...
Definition KeyAction.cs:10