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)
76 while (_pendingOps.TryDequeue(out var op))
79 var mods = adapter.CurrentKeyboardModifiers;
81 foreach (var kvp
in _keyConfigs)
83 int keyCode = kvp.Key;
84 var config = kvp.Value;
86 bool currentlyPressed = adapter.IsDown(keyCode);
87 bool previouslyPressed = _previousPressed.TryGetValue(keyCode, out var prev) && prev;
90 if (currentlyPressed && !previouslyPressed)
92 _previousPressed[keyCode] =
true;
93 config._lastEventTick = tick;
100 if (currentlyPressed && previouslyPressed)
102 if (!config.IsPaused && config.ReadyForNextEvent(tick))
104 config._lastEventTick = tick;
111 if (!currentlyPressed && previouslyPressed)
113 _previousPressed[keyCode] =
false;
129 public void StartMonitoringKey(
int keyCode,
string? displayName =
null,
double timeBetweenEvents = -1,
bool isPaused =
false)
131 _pendingOps.Enqueue(() =>
133 if (timeBetweenEvents < 0)
136 var name =
string.IsNullOrWhiteSpace(displayName) ? keyCode.ToString() : displayName;
139 _keyConfigs[keyCode] = config;
142 _previousPressed[keyCode] = adapter?.IsDown(keyCode) ??
false;
157 _pendingOps.Enqueue(() =>
159 _keyConfigs.Remove(keyCode);
160 _previousPressed.Remove(keyCode);
175 if (
int.TryParse(key, out
int code))
187 _pendingOps.Enqueue(() =>
190 _previousPressed.Clear();
198 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.