Gondwana Game Engine Development (master) - 2.6.1
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
WinFormsKeyboardAdapter.cs
1using System.Runtime.CompilerServices;
3using Microsoft.Extensions.Logging;
4
6
12public sealed class WinFormsKeyboardAdapter : IKeyboardAdapter, IMessageFilter, IDisposable
13{
29 public static Keys? GetKeyFromString(string keyName)
30 {
31 // Parse the received key string into the Keys enum (case-insensitive)
32 if (Enum.TryParse<Keys>(keyName, true, out var key))
33 return key;
34
35 Engine.Logger.LogWarning("Invalid key name: {KeyName}", keyName);
36 return null;
37 }
38
39 private readonly Control _lifetimeOwner;
40
41 // Windows VK codes are 0..255
42 private readonly int[] _down = new int[256];
43
44 // modifier bits published lock-free
45 private int _modsBits;
46
47 private bool _isDisposed;
48
53 (KeyboardModifierState)Volatile.Read(ref _modsBits);
54
55 internal WinFormsKeyboardAdapter(Control lifetimeOwner)
56 {
57 _lifetimeOwner = lifetimeOwner ?? throw new ArgumentNullException(nameof(lifetimeOwner));
58
59 Application.AddMessageFilter(this);
60 _lifetimeOwner.Disposed += OnOwnerDisposed;
61
62 Engine.Logger.LogInformation("WinFormsKeyboardAdapter initialized. Using IMessageFilter for key polling.");
63 }
64
65 private void OnOwnerDisposed(object? sender, EventArgs e) => Dispose();
66
72 // IMessageFilter: called on UI thread for each message.
73 public bool PreFilterMessage(ref Message m)
74 {
75 const int WM_KEYDOWN = 0x0100;
76 const int WM_KEYUP = 0x0101;
77 const int WM_SYSKEYDOWN = 0x0104;
78 const int WM_SYSKEYUP = 0x0105;
79
80 if (_isDisposed)
81 return false;
82
83 switch (m.Msg)
84 {
85 case WM_KEYDOWN:
86 case WM_SYSKEYDOWN:
87 SetDown(ExtractVk(m), true);
88 break;
89
90 case WM_KEYUP:
91 case WM_SYSKEYUP:
92 SetDown(ExtractVk(m), false);
93 break;
94 }
95
96 return false;
97 }
98
104 [MethodImpl(MethodImplOptions.AggressiveInlining)]
105 public bool IsDown(int keyCode)
106 {
107 int idx = keyCode & 0xFF;
108 return Volatile.Read(ref _down[idx]) != 0;
109 }
110
111 [MethodImpl(MethodImplOptions.AggressiveInlining)]
112 private void SetDown(int keyCode, bool down)
113 {
114 int idx = keyCode & 0xFF;
115 Volatile.Write(ref _down[idx], down ? 1 : 0);
116 UpdateMods();
117 }
118
119 [MethodImpl(MethodImplOptions.AggressiveInlining)]
120 private void UpdateMods()
121 {
122 int mods = 0;
123 var mk = Control.ModifierKeys;
124
125 if ((mk & Keys.Shift) != 0) mods |= (int)KeyboardModifierState.Shift;
126 if ((mk & Keys.Control) != 0) mods |= (int)KeyboardModifierState.Ctrl;
127 if ((mk & Keys.Alt) != 0) mods |= (int)KeyboardModifierState.Alt;
128
129 Volatile.Write(ref _modsBits, mods);
130 }
131
132 [MethodImpl(MethodImplOptions.AggressiveInlining)]
133 private static int ExtractVk(in Message m)
134 {
135 // WParam contains VK. Clamp to 16 bits and then the low 8 bits are standard VK range.
136 return m.WParam.ToInt32() & 0xFFFF;
137 }
138
142 public void Dispose()
143 {
144 if (_isDisposed)
145 return;
146
147 _isDisposed = true;
148
149 Application.RemoveMessageFilter(this);
150 _lifetimeOwner.Disposed -= OnOwnerDisposed;
151
152 Engine.Logger.LogInformation("WinFormsKeyboardAdapter disposed.");
153 }
154}
Represents the core game engine singleton responsible for managing the main update loop,...
Definition Engine.cs:36
static ILogger< Engine > Logger
Gets the logger instance used by the engine for diagnostic and informational messages.
Definition Engine.cs:59
bool IsDown(int keyCode)
Determines whether the specified key is currently pressed.
KeyboardModifierState CurrentKeyboardModifiers
Gets the current state of keyboard modifiers (Shift, Ctrl, Alt).
void Dispose()
Releases all resources used by the WinFormsKeyboardAdapter and removes the message filter.
bool PreFilterMessage(ref Message m)
Filters Windows messages to capture keyboard state changes for all key events.
static ? Keys GetKeyFromString(string keyName)
Attempts to convert a string representation of a key into its corresponding Keys value.
Represents an adapter for keyboard input, providing access to the currently pressed keys and active m...
KeyboardModifierState
Represents the state of keyboard modifier keys (Shift, Ctrl, Alt) that can be pressed simultaneously ...