1using System.Collections.Concurrent;
2using System.Collections.ObjectModel;
9public sealed class KeyboardEventPoller
14 public static KeyboardEventPoller?
Instance {
get;
private set; }
26 Instance =
new KeyboardEventPoller(adapter);
35 public event Action<KeyDownEventArgs>?
KeyDown;
38 private readonly Dictionary<int, KeyEventConfiguration> _keyConfigs =
new();
39 private readonly Dictionary<int, bool> _previousPressed =
new();
42 private readonly ConcurrentQueue<Action> _pendingOps =
new();
44 private KeyboardEventPoller() { }
66 internal void PollForEvents(
long tick)
77 while (_pendingOps.TryDequeue(out var op))
80 var mods = adapter.CurrentKeyboardModifiers;
82 foreach (var kvp
in _keyConfigs)
84 int keyCode = kvp.Key;
85 var config = kvp.Value;
87 bool currentlyPressed = adapter.IsDown(keyCode);
88 bool previouslyPressed = _previousPressed.TryGetValue(keyCode, out var prev) && prev;
91 if (currentlyPressed && !previouslyPressed)
93 _previousPressed[keyCode] =
true;
94 config._lastEventTick = tick;
101 if (currentlyPressed && previouslyPressed)
103 if (!config.IsPaused && config.ReadyForNextEvent(tick))
105 config._lastEventTick = tick;
113 if (!currentlyPressed && previouslyPressed)
115 _previousPressed[keyCode] =
false;
133 string? displayName =
null,
134 double timeBetweenEvents = -1,
135 bool isPaused =
false)
137 _pendingOps.Enqueue(() =>
139 if (timeBetweenEvents < 0)
142 var name =
string.IsNullOrWhiteSpace(displayName) ? keyCode.ToString() : displayName;
145 _keyConfigs[keyCode] = config;
148 _previousPressed[keyCode] = adapter?.IsDown(keyCode) ??
false;
163 _pendingOps.Enqueue(() =>
165 _keyConfigs.Remove(keyCode);
166 _previousPressed.Remove(keyCode);
181 if (
int.TryParse(key, out
int code))
193 _pendingOps.Enqueue(() =>
196 _previousPressed.Clear();
204 new ReadOnlyDictionary<int, KeyEventConfiguration>(_keyConfigs);
Represents the core game engine singleton responsible for managing the main update loop,...
static Engine Instance
Gets the singleton instance of the Engine.
EngineConfiguration Configuration
Gets the current engine configuration settings.