Skip to content

Commit

Permalink
move keyboard query into FNAPlatform
Browse files Browse the repository at this point in the history
  • Loading branch information
thatcosmonaut committed Dec 29, 2023
1 parent eb826d3 commit 9218f9b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
14 changes: 10 additions & 4 deletions src/FNAPlatform/FNAPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ static FNAPlatform()
PollEvents = SDL2_FNAPlatform.PollEvents;
GetGraphicsAdapters = SDL2_FNAPlatform.GetGraphicsAdapters;
GetCurrentDisplayMode = SDL2_FNAPlatform.GetCurrentDisplayMode;
GetKeyboardScancodeArray = SDL2_FNAPlatform.GetKeyboardScancodeArray;
ScancodeToKey = SDL2_FNAPlatform.ScancodeToKey;
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 @@ -262,12 +263,17 @@ ref bool textInputSuppress
public delegate DisplayMode GetCurrentDisplayModeFunc(int adapterIndex);
public static readonly GetCurrentDisplayModeFunc GetCurrentDisplayMode;

// Should return a pointer to an array that does not need to be freed by the client.
public delegate IntPtr GetKeyboardScancodeArrayFunc(out int numkeys);
public static readonly GetKeyboardScancodeArrayFunc GetKeyboardScancodeArray;

// Maps scancode array position to Key. To be used in conjunction with GetKeyboardScancodeArray.
public delegate Keys ScancodeToKeyFunc(int scancode);
public static readonly ScancodeToKeyFunc ScancodeToKey;

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

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

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

Expand Down
13 changes: 9 additions & 4 deletions src/FNAPlatform/SDL2_FNAPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2858,14 +2858,19 @@ public static Keys GetKeyFromScancode(Keys scancode)
return Keys.None;
}

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

public static Keys ScancodeToKey(int scancode)
{
if (INTERNAL_scanMap.TryGetValue(scancode, out Keys key))
{
return scancode;
return key;
}

return SDL.SDL_Scancode.SDL_SCANCODE_UNKNOWN;
return Keys.None;
}

#endregion
Expand Down
11 changes: 5 additions & 6 deletions src/Input/Keyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@ public static class Keyboard
/// <returns>Current keyboard state.</returns>
public unsafe static KeyboardState GetState()
{
// SDL_GetKeyboardState returns a pointer that does not need to be freed.
byte* state = (byte*) SDL2.SDL.SDL_GetKeyboardState(out int numkeys);
// Returns a pointer that does not need to be freed.
byte* state = (byte*) FNAPlatform.GetKeyboardScancodeArray(out int numkeys);

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

Expand Down

0 comments on commit 9218f9b

Please sign in to comment.