MoonWorks/src/Input/Keyboard.cs

110 lines
2.3 KiB
C#
Raw Permalink Normal View History

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;
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; }
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-07-12 23:09:23 +00:00
Keys = new KeyboardButton[numKeys];
2022-03-18 18:46:44 +00:00
foreach (KeyCode keycode in Enum.GetValues(typeof(KeyCode)))
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-03-18 18:46:44 +00:00
foreach (int keycode in Enum.GetValues(typeof(KeyCode)))
2022-02-23 05:14:32 +00:00
{
2022-07-12 23:09:23 +00:00
var button = Keys[keycode];
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-03-18 18:46:44 +00:00
if (TextInputBindings.TryGetValue((KeyCode) keycode, out var textIndex))
2022-02-23 05:14:32 +00:00
{
Inputs.OnTextInput(TextInputCharacters[(textIndex)]);
}
2022-03-18 18:46:44 +00:00
else if (IsDown(KeyCode.LeftControl) && (KeyCode) 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 IsDown(KeyCode keycode)
2022-02-23 05:14:32 +00:00
{
return Keys[(int) keycode].IsDown;
}
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-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-07-12 23:09:23 +00:00
public KeyboardButton Button(KeyCode keycode)
2022-03-07 18:54:52 +00:00
{
return Keys[(int) keycode];
}
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
}