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 committed Dec 28, 2023
1 parent 5f5ac87 commit 33aa4d0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 25 deletions.
5 changes: 5 additions & 0 deletions src/FNAPlatform/FNAPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using SDL2;
#endregion

namespace Microsoft.Xna.Framework
Expand Down Expand Up @@ -112,6 +113,7 @@ static FNAPlatform()
GetGraphicsAdapters = SDL2_FNAPlatform.GetGraphicsAdapters;
GetCurrentDisplayMode = SDL2_FNAPlatform.GetCurrentDisplayMode;
GetKeyFromScancode = SDL2_FNAPlatform.GetKeyFromScancode;
GetScancodeFromKey = SDL2_FNAPlatform.GetScancodeFromKey;
IsTextInputActive = SDL2_FNAPlatform.IsTextInputActive;
StartTextInput = SDL2.SDL.SDL_StartTextInput;
StopTextInput = SDL2.SDL.SDL_StopTextInput;
Expand Down Expand Up @@ -264,6 +266,9 @@ ref bool textInputSuppress
public delegate Keys GetKeyFromScancodeFunc(Keys scancode);
public static readonly GetKeyFromScancodeFunc GetKeyFromScancode;

public delegate SDL.SDL_Scancode GetScancodeFromKeyFunc(Keys key);
public static readonly GetScancodeFromKeyFunc GetScancodeFromKey;

public delegate bool IsTextInputActiveFunc();
public static readonly IsTextInputActiveFunc IsTextInputActive;

Expand Down
67 changes: 46 additions & 21 deletions src/FNAPlatform/SDL2_FNAPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -954,53 +954,68 @@ 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)
{
Keyboard.LeftControlDown = true;
}
else if (key == Keys.RightControl)
{
Keyboard.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 ((Keyboard.LeftControlDown || Keyboard.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))
{
textInputControlDown[textIndex] = true;
TextInputEXT.OnTextInput(FNAPlatform.TextInputCharacters[textIndex]);
}
else if ((Keyboard.keys.Contains(Keys.LeftControl) || Keyboard.keys.Contains(Keys.RightControl))
else if ((Keyboard.LeftControlDown || Keyboard.RightControlDown)
&& key == Keys.V)
{
textInputControlDown[6] = true;
TextInputEXT.OnTextInput(FNAPlatform.TextInputCharacters[6]);
textInputSuppress = true;
}
}
}
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;
}
Keyboard.LeftControlDown = false;
}
else if (key == Keys.RightControl)
{
Keyboard.RightControlDown = false;
}

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

Expand Down Expand Up @@ -1215,7 +1230,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 +2858,16 @@ public static Keys GetKeyFromScancode(Keys scancode)
return Keys.None;
}

public static SDL.SDL_Scancode GetScancodeFromKey(Keys key)
{
if (INTERNAL_xnaMap.TryGetValue((int) key, out SDL.SDL_Scancode scancode))
{
return scancode;
}

return SDL.SDL_Scancode.SDL_SCANCODE_UNKNOWN;
}

#endregion

#region Private Static Win32 WM_PAINT Interop
Expand Down
30 changes: 26 additions & 4 deletions src/Input/Keyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,22 @@ 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);
// SDL_GetKeyboardState returns a pointer that does not need to be freed.
byte* state = (byte*) SDL2.SDL.SDL_GetKeyboardState(out int numkeys);

activeKeys.Clear();
foreach (Keys key in allKeys)
{
SDL2.SDL.SDL_Scancode scancode = FNAPlatform.GetScancodeFromKey(key);
if (state[(int) scancode] != 0)
{
activeKeys.Add(key);
}
}

return new KeyboardState(activeKeys);
}

/// <summary>
Expand All @@ -37,7 +50,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 @@ -53,7 +66,16 @@ public static Keys GetKeyFromScancodeEXT(Keys scancode)

#region Internal Static Variables

internal static List<Keys> keys = new List<Keys>();
// Used for special copy-paste text input
internal static bool LeftControlDown = false;
internal static bool RightControlDown = false;

#endregion

#region Private Static Variables

private static Keys[] allKeys = (Keys[]) Enum.GetValues(typeof(Keys));
private static List<Keys> activeKeys = new List<Keys>();

#endregion
}
Expand Down

0 comments on commit 33aa4d0

Please sign in to comment.