#region usings
using System;
using System.Collections.Generic;
using OpenTK.Input;
#endregion
namespace SM.Base.Controls
{
///
/// A static class to get keyboard inputs.
///
public static class Keyboard
{
private static KeyboardState? _keyboardState;
private static List _lastPressedKeys = new List();
///
/// True, when ANY key pressed.
///
public static bool IsAnyKeyPressed => _keyboardState?.IsAnyKeyDown == true;
internal static void SetStage()
{
if (_keyboardState.HasValue)
{
_lastPressedKeys = new List();
foreach (object o in Enum.GetValues(typeof(Key)))
if (_keyboardState.Value[(Key) o])
_lastPressedKeys.Add((Key) o);
}
_keyboardState = OpenTK.Input.Keyboard.GetState();
}
///
/// Checks if a key is down.
///
/// The key
/// If true, the method doesn't return true, when it was pressed one stage before.
///
public static bool IsDown(Key key, bool once = false)
{
return _keyboardState?[key] == true && !(once && _lastPressedKeys.Contains(key));
}
///
/// Checks if a key was down but not anymore.
///
///
///
public static bool WasDown(Key key)
{
return _keyboardState?[key] == false && _lastPressedKeys.Contains(key);
}
///
/// Check if a is up.
///
///
/// If true, the method doesn't return true, when it was up one stage before.
///
public static bool IsUp(Key key, bool once = false)
{
return _keyboardState?[key] == false && !(once && !_lastPressedKeys.Contains(key));
}
///
/// Checks if specific keys are down.
///
/// Startindex
/// Endindex
/// If true, it ignores keys that were down a state before.
/// True if any of the specific keys where found down.
/// The start index can't be greater then the end index.
public static bool AreSpecificKeysPressed(int startIndex, int endIndex, bool once = false)
{
if (startIndex > endIndex)
throw new ArgumentException("The startIndex is greater than the endIndex.", nameof(startIndex));
int length = endIndex - startIndex;
for (int i = 0; i < length + 1; i++)
{
int actualIndex = i + startIndex;
Key key = (Key) actualIndex;
if (IsDown(key, once)) return true;
}
return false;
}
///
/// Checks if any of the specific keys are pressed.
///
///
///
public static bool AreSpecificKeysPressed(params Key[] keys)
{
return AreSpecificKeysPressed(false, keys);
}
///
/// Checks if any of the specific keys are pressed.
///
/// If true, it ignores keys that were down a state before.
///
///
public static bool AreSpecificKeysPressed(bool once, params Key[] keys)
{
foreach (Key key in keys)
if (IsDown(key, once))
return true;
return false;
}
///
/// Checks if specific keys are down and returns the pressed keys.
///
/// Startindex
/// Endindex
///
/// If true, it ignores keys that were down a state before.
///
///
public static bool AreSpecificKeysPressed(int startIndex, int endIndex, out Key[] pressedKeys,
bool once = false)
{
if (startIndex > endIndex)
throw new ArgumentException("The startIndex is greater than the endIndex.", nameof(startIndex));
int length = endIndex - startIndex;
bool success = false;
List keys = new List();
for (int i = 0; i < length + 1; i++)
{
int actualIndex = i + startIndex;
Key key = (Key) actualIndex;
if (IsDown(key, once))
{
keys.Add(key);
success = true;
}
}
pressedKeys = keys.ToArray();
return success;
}
///
/// Checks if any of the specific keys are pressed and returns them.
///
///
///
///
public static bool AreSpecificKeysPressed(out Key[] pressedKey, params Key[] keys)
{
return AreSpecificKeysPressed(false, out pressedKey, keys);
}
///
/// Checks if any of the specific keys are pressed and returns them.
///
/// If true, it ignores keys that were down a state before.
///
///
///
public static bool AreSpecificKeysPressed(bool once, out Key[] pressedKeys, params Key[] keys)
{
List pressedKey = new List();
bool success = false;
foreach (Key key in keys)
if (IsDown(key, once))
{
pressedKey.Add(key);
success = true;
}
pressedKeys = pressedKey.ToArray();
return success;
}
}
}