MoonWorks/src/Input/Keyboard.cs

55 lines
1.3 KiB
C#
Raw Normal View History

2021-01-19 07:29:07 +00:00
using System;
using System.Runtime.InteropServices;
using SDL2;
namespace MoonWorks.Input
2021-01-19 07:29:07 +00:00
{
public class Keyboard
{
2021-01-22 22:41:34 +00:00
private ButtonState[] Keys { get; }
2021-01-19 07:29:07 +00:00
private int numKeys;
internal Keyboard()
{
SDL.SDL_GetKeyboardState(out numKeys);
2021-01-22 22:41:34 +00:00
Keys = new ButtonState[numKeys];
2021-01-19 07:29:07 +00:00
foreach (Keycode keycode in Enum.GetValues(typeof(Keycode)))
{
2021-01-22 22:41:34 +00:00
Keys[(int)keycode] = new ButtonState();
2021-01-19 07:29:07 +00:00
}
}
internal void Update()
{
IntPtr keyboardState = SDL.SDL_GetKeyboardState(out _);
foreach (int keycode in Enum.GetValues(typeof(Keycode)))
{
var keyDown = Marshal.ReadByte(keyboardState, keycode);
2021-01-22 22:41:34 +00:00
Keys[keycode].Update(Conversions.ByteToBool(keyDown));
2021-01-19 07:29:07 +00:00
}
}
public bool IsDown(Keycode keycode)
{
2021-01-22 22:41:34 +00:00
return Keys[(int)keycode].IsDown;
2021-01-19 07:29:07 +00:00
}
public bool IsPressed(Keycode keycode)
{
2021-01-22 22:41:34 +00:00
return Keys[(int)keycode].IsPressed;
2021-01-19 07:29:07 +00:00
}
public bool IsHeld(Keycode keycode)
{
2021-01-22 22:41:34 +00:00
return Keys[(int)keycode].IsHeld;
2021-01-19 07:29:07 +00:00
}
2021-01-22 22:41:34 +00:00
public bool IsReleased(Keycode keycode)
2021-01-19 07:29:07 +00:00
{
2021-01-22 22:41:34 +00:00
return Keys[(int)keycode].IsReleased;
2021-01-19 07:29:07 +00:00
}
}
}