2022-02-23 05:14:32 +00:00
|
|
|
|
using System;
|
2021-03-25 22:57:26 +00:00
|
|
|
|
using System.Collections.Generic;
|
2021-01-19 07:29:07 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using SDL2;
|
|
|
|
|
|
2021-01-22 08:20:07 +00:00
|
|
|
|
namespace MoonWorks.Input
|
2021-01-19 07:29:07 +00:00
|
|
|
|
{
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public class Keyboard
|
|
|
|
|
{
|
|
|
|
|
private ButtonState[] Keys { get; }
|
|
|
|
|
private int numKeys;
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2021-03-25 22:57:26 +00:00
|
|
|
|
private static readonly char[] TextInputCharacters = new char[]
|
|
|
|
|
{
|
|
|
|
|
(char) 2, // Home
|
|
|
|
|
(char) 3, // End
|
|
|
|
|
(char) 8, // Backspace
|
|
|
|
|
(char) 9, // Tab
|
|
|
|
|
(char) 13, // Enter
|
|
|
|
|
(char) 127, // Delete
|
|
|
|
|
(char) 22 // Ctrl+V (Paste)
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
private static readonly Dictionary<Keycode, int> TextInputBindings = new Dictionary<Keycode, int>()
|
2021-03-25 22:57:26 +00:00
|
|
|
|
{
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{ Keycode.Home, 0 },
|
|
|
|
|
{ Keycode.End, 1 },
|
|
|
|
|
{ Keycode.Backspace, 2 },
|
|
|
|
|
{ Keycode.Tab, 3 },
|
|
|
|
|
{ Keycode.Return, 4 },
|
|
|
|
|
{ Keycode.Delete, 5 }
|
2021-03-25 22:57:26 +00:00
|
|
|
|
// Ctrl+V is special!
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
internal Keyboard()
|
|
|
|
|
{
|
|
|
|
|
SDL.SDL_GetKeyboardState(out numKeys);
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
Keys = new ButtonState[numKeys];
|
|
|
|
|
foreach (Keycode keycode in Enum.GetValues(typeof(Keycode)))
|
|
|
|
|
{
|
|
|
|
|
Keys[(int) keycode] = new ButtonState();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
internal void Update()
|
|
|
|
|
{
|
|
|
|
|
IntPtr keyboardState = SDL.SDL_GetKeyboardState(out _);
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
foreach (int keycode in Enum.GetValues(typeof(Keycode)))
|
|
|
|
|
{
|
|
|
|
|
var keyDown = Marshal.ReadByte(keyboardState, keycode);
|
|
|
|
|
Keys[keycode].Update(Conversions.ByteToBool(keyDown));
|
2021-03-25 22:57:26 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
if (Conversions.ByteToBool(keyDown))
|
|
|
|
|
{
|
|
|
|
|
if (TextInputBindings.TryGetValue((Keycode) keycode, out var textIndex))
|
|
|
|
|
{
|
|
|
|
|
Inputs.OnTextInput(TextInputCharacters[(textIndex)]);
|
|
|
|
|
}
|
|
|
|
|
else if (IsDown(Keycode.LeftControl) && (Keycode) keycode == Keycode.V)
|
|
|
|
|
{
|
|
|
|
|
Inputs.OnTextInput(TextInputCharacters[6]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public bool IsDown(Keycode keycode)
|
|
|
|
|
{
|
|
|
|
|
return Keys[(int) keycode].IsDown;
|
|
|
|
|
}
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public bool IsPressed(Keycode keycode)
|
|
|
|
|
{
|
|
|
|
|
return Keys[(int) keycode].IsPressed;
|
|
|
|
|
}
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public bool IsHeld(Keycode keycode)
|
|
|
|
|
{
|
|
|
|
|
return Keys[(int) keycode].IsHeld;
|
|
|
|
|
}
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public bool IsReleased(Keycode keycode)
|
|
|
|
|
{
|
|
|
|
|
return Keys[(int) keycode].IsReleased;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-19 07:29:07 +00:00
|
|
|
|
}
|