Skip to content

Commit

Permalink
Use SDL_GetKeyboardState instead of event polling
Browse files Browse the repository at this point in the history
  • Loading branch information
thatcosmonaut authored and flibitijibibo committed Jan 1, 2024
1 parent a592086 commit 88f940f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/FNAPlatform/FNAPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ static FNAPlatform()
PollEvents = SDL2_FNAPlatform.PollEvents;
GetGraphicsAdapters = SDL2_FNAPlatform.GetGraphicsAdapters;
GetCurrentDisplayMode = SDL2_FNAPlatform.GetCurrentDisplayMode;
GetKeyboardState = SDL2_FNAPlatform.GetKeyboardState;
GetKeyFromScancode = SDL2_FNAPlatform.GetKeyFromScancode;
IsTextInputActive = SDL2_FNAPlatform.IsTextInputActive;
StartTextInput = SDL2.SDL.SDL_StartTextInput;
Expand Down Expand Up @@ -261,6 +262,9 @@ ref bool textInputSuppress
public delegate DisplayMode GetCurrentDisplayModeFunc(int adapterIndex);
public static readonly GetCurrentDisplayModeFunc GetCurrentDisplayMode;

public delegate void GetKeyboardStateFunc(List<Keys> activeKeys);
public static readonly GetKeyboardStateFunc GetKeyboardState;

public delegate Keys GetKeyFromScancodeFunc(Keys scancode);
public static readonly GetKeyFromScancodeFunc GetKeyFromScancode;

Expand Down
95 changes: 77 additions & 18 deletions src/FNAPlatform/SDL2_FNAPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ internal static class SDL2_FNAPlatform

#endregion

#region Keyboard State

// Used for special copy-paste text input
private static bool LeftControlDown = false;
private static bool RightControlDown = false;

#endregion

#region Init/Exit Methods

public static string ProgramInit(LaunchParameters args)
Expand Down Expand Up @@ -954,31 +962,39 @@ ref bool textInputSuppress
if (evt.type == SDL.SDL_EventType.SDL_KEYDOWN)
{
Keys key = ToXNAKey(ref evt.key.keysym);
if (!Keyboard.keys.Contains(key))
if (key == Keys.LeftControl)
{
LeftControlDown = true;
}
else if (key == Keys.RightControl)
{
RightControlDown = true;
}

if (evt.key.repeat == 0)
{
Keyboard.keys.Add(key);
int textIndex;
if (FNAPlatform.TextInputBindings.TryGetValue(key, out textIndex))
{
textInputControlDown[textIndex] = true;
TextInputEXT.OnTextInput(FNAPlatform.TextInputCharacters[textIndex]);
}
else if ((Keyboard.keys.Contains(Keys.LeftControl) || Keyboard.keys.Contains(Keys.RightControl))
else if ((LeftControlDown || RightControlDown)
&& key == Keys.V)
{
textInputControlDown[6] = true;
TextInputEXT.OnTextInput(FNAPlatform.TextInputCharacters[6]);
textInputSuppress = true;
}
}
else if (evt.key.repeat > 0)
else
{
int textIndex;
if (FNAPlatform.TextInputBindings.TryGetValue(key, out textIndex))
{
TextInputEXT.OnTextInput(FNAPlatform.TextInputCharacters[textIndex]);
}
else if ((Keyboard.keys.Contains(Keys.LeftControl) || Keyboard.keys.Contains(Keys.RightControl))
else if ((LeftControlDown || RightControlDown)
&& key == Keys.V)
{
TextInputEXT.OnTextInput(FNAPlatform.TextInputCharacters[6]);
Expand All @@ -988,19 +1004,26 @@ ref bool textInputSuppress
else if (evt.type == SDL.SDL_EventType.SDL_KEYUP)
{
Keys key = ToXNAKey(ref evt.key.keysym);
if (Keyboard.keys.Remove(key))

if (key == Keys.LeftControl)
{
int value;
if (FNAPlatform.TextInputBindings.TryGetValue(key, out value))
{
textInputControlDown[value] = false;
}
else if (((!Keyboard.keys.Contains(Keys.LeftControl) && !Keyboard.keys.Contains(Keys.RightControl)) && textInputControlDown[6])
|| key == Keys.V)
{
textInputControlDown[6] = false;
textInputSuppress = false;
}
LeftControlDown = false;
}
else if (key == Keys.RightControl)
{
RightControlDown = false;
}

int value;
if (FNAPlatform.TextInputBindings.TryGetValue(key, out value))
{
textInputControlDown[value] = false;
}
else if (((!LeftControlDown && !RightControlDown) && textInputControlDown[6])
|| key == Keys.V)
{
textInputControlDown[6] = false;
textInputSuppress = false;
}
}

Expand Down Expand Up @@ -1215,7 +1238,7 @@ ref bool textInputSuppress
}
}

else if (evt.type == SDL.SDL_EventType.SDL_TEXTEDITING)
else if (evt.type == SDL.SDL_EventType.SDL_TEXTEDITING)
{
int bytes = MeasureStringLength(evt.edit.text);
if (bytes > 0)
Expand Down Expand Up @@ -2843,6 +2866,42 @@ public static Keys GetKeyFromScancode(Keys scancode)
return Keys.None;
}

public static unsafe void GetKeyboardState(List<Keys> activeKeys)
{
int numkeys;
byte* state = (byte*) SDL.SDL_GetKeyboardState(out numkeys);
if (UseScancodes)
{
for (int i = 0; i < numkeys; i += 1)
{
if (state[i] != 0)
{
Keys key;
if (INTERNAL_scanMap.TryGetValue(i, out key))
{
activeKeys.Add(key);
}
}
}
}
else
{
for (int i = 0; i < numkeys; i += 1)
{
if (state[i] != 0)
{
Keys key;
if (INTERNAL_keyMap.TryGetValue(
(int) SDL.SDL_GetKeyFromScancode((SDL.SDL_Scancode) i),
out key
)) {
activeKeys.Add(key);
}
}
}
}
}

#endregion

#region Private Static Win32 WM_PAINT Interop
Expand Down
12 changes: 7 additions & 5 deletions src/Input/Keyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ public static class Keyboard
/// Returns the current keyboard state.
/// </summary>
/// <returns>Current keyboard state.</returns>
public static KeyboardState GetState()
public unsafe static KeyboardState GetState()
{
return new KeyboardState(keys);
activeKeys.Clear();
FNAPlatform.GetKeyboardState(activeKeys);
return new KeyboardState(activeKeys);
}

/// <summary>
Expand All @@ -37,7 +39,7 @@ public static KeyboardState GetState()
/// <returns>Current keyboard state.</returns>
public static KeyboardState GetState(PlayerIndex playerIndex)
{
return new KeyboardState(keys);
return GetState();
}

#endregion
Expand All @@ -51,9 +53,9 @@ public static Keys GetKeyFromScancodeEXT(Keys scancode)

#endregion

#region Internal Static Variables
#region Private Static Variables

internal static List<Keys> keys = new List<Keys>();
private static List<Keys> activeKeys = new List<Keys>();

#endregion
}
Expand Down

0 comments on commit 88f940f

Please sign in to comment.