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 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
|
|
|
|
|
{
|
2022-07-08 23:47:12 +00:00
|
|
|
|
public bool AnyPressed { get; private set; }
|
2022-07-12 23:09:23 +00:00
|
|
|
|
public KeyboardButton AnyPressedButton { get; private set; }
|
2022-07-08 23:47:12 +00:00
|
|
|
|
|
2022-07-12 23:09:23 +00:00
|
|
|
|
public IntPtr State { get; private set; }
|
|
|
|
|
|
2022-12-13 08:34:16 +00:00
|
|
|
|
private KeyCode[] KeyCodes;
|
2022-07-12 23:09:23 +00:00
|
|
|
|
private KeyboardButton[] Keys { get; }
|
2022-02-23 05:14:32 +00:00
|
|
|
|
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-03-18 18:46:44 +00:00
|
|
|
|
private static readonly Dictionary<KeyCode, int> TextInputBindings = new Dictionary<KeyCode, int>()
|
2021-03-25 22:57:26 +00:00
|
|
|
|
{
|
2022-03-18 18:46:44 +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-12-13 08:34:16 +00:00
|
|
|
|
KeyCodes = Enum.GetValues<KeyCode>();
|
2022-07-12 23:09:23 +00:00
|
|
|
|
Keys = new KeyboardButton[numKeys];
|
2022-12-13 08:34:16 +00:00
|
|
|
|
|
|
|
|
|
foreach (KeyCode keycode in KeyCodes)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
2022-07-12 23:09:23 +00:00
|
|
|
|
Keys[(int) keycode] = new KeyboardButton(this, keycode);
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
internal void Update()
|
|
|
|
|
{
|
2022-07-08 23:47:12 +00:00
|
|
|
|
AnyPressed = false;
|
|
|
|
|
|
2022-07-12 23:09:23 +00:00
|
|
|
|
State = SDL.SDL_GetKeyboardState(out _);
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2022-12-13 08:34:16 +00:00
|
|
|
|
foreach (KeyCode keycode in KeyCodes)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
2022-12-13 08:34:16 +00:00
|
|
|
|
var button = Keys[(int) keycode];
|
2022-07-12 23:09:23 +00:00
|
|
|
|
button.Update();
|
2021-03-25 22:57:26 +00:00
|
|
|
|
|
2022-07-12 23:09:23 +00:00
|
|
|
|
if (button.IsPressed)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
2022-12-13 08:34:16 +00:00
|
|
|
|
if (TextInputBindings.TryGetValue(keycode, out var textIndex))
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
Inputs.OnTextInput(TextInputCharacters[(textIndex)]);
|
|
|
|
|
}
|
2022-12-13 08:34:16 +00:00
|
|
|
|
else if (IsDown(KeyCode.LeftControl) && keycode == KeyCode.V)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
Inputs.OnTextInput(TextInputCharacters[6]);
|
|
|
|
|
}
|
2022-07-08 23:47:12 +00:00
|
|
|
|
|
|
|
|
|
AnyPressed = true;
|
2022-07-12 23:09:23 +00:00
|
|
|
|
AnyPressedButton = button;
|
2022-07-08 23:47:12 +00:00
|
|
|
|
}
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2022-03-18 18:46:44 +00:00
|
|
|
|
public bool IsPressed(KeyCode keycode)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
return Keys[(int) keycode].IsPressed;
|
|
|
|
|
}
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2022-03-18 18:46:44 +00:00
|
|
|
|
public bool IsHeld(KeyCode keycode)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
return Keys[(int) keycode].IsHeld;
|
|
|
|
|
}
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2022-11-28 18:19:03 +00:00
|
|
|
|
public bool IsDown(KeyCode keycode)
|
|
|
|
|
{
|
|
|
|
|
return Keys[(int) keycode].IsDown;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 18:46:44 +00:00
|
|
|
|
public bool IsReleased(KeyCode keycode)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
return Keys[(int) keycode].IsReleased;
|
|
|
|
|
}
|
2022-03-07 18:54:52 +00:00
|
|
|
|
|
2022-11-28 18:19:03 +00:00
|
|
|
|
public bool IsIdle(KeyCode keycode)
|
|
|
|
|
{
|
|
|
|
|
return Keys[(int) keycode].IsIdle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsUp(KeyCode keycode)
|
|
|
|
|
{
|
|
|
|
|
return Keys[(int) keycode].IsUp;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-12 23:09:23 +00:00
|
|
|
|
public KeyboardButton Button(KeyCode keycode)
|
2022-03-07 18:54:52 +00:00
|
|
|
|
{
|
|
|
|
|
return Keys[(int) keycode];
|
|
|
|
|
}
|
2022-03-24 23:41:00 +00:00
|
|
|
|
|
|
|
|
|
public ButtonState ButtonState(KeyCode keycode)
|
|
|
|
|
{
|
|
|
|
|
return Keys[(int) keycode].State;
|
|
|
|
|
}
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
2021-01-19 07:29:07 +00:00
|
|
|
|
}
|