forked from AerysBat/XNALara
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKeyboardEventHandler.cs
39 lines (35 loc) · 1.28 KB
/
KeyboardEventHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Input;
namespace XNALara
{
public class KeyboardEventHandler
{
private static List<Keys> keysDown = new List<Keys>();
private static List<Keys> keysPressed = new List<Keys>();
private static List<Keys> keysReleased = new List<Keys>();
public static void ProcessKeyboardState(KeyboardState keyboardState) {
keysPressed.Clear();
Keys[] keysCurrentlyDown = keyboardState.GetPressedKeys();
foreach (Keys keyCurrentlyDown in keysCurrentlyDown) {
if (!keysDown.Contains(keyCurrentlyDown)) {
keysPressed.Add(keyCurrentlyDown);
}
}
keysReleased.Clear();
foreach (Keys keyDown in keysDown) {
if (Array.IndexOf<Keys>(keysCurrentlyDown, keyDown) < 0) {
keysReleased.Add(keyDown);
}
}
keysDown.Clear();
keysDown.AddRange(keysCurrentlyDown);
}
public static bool HasKeyBeenPressed(Keys key) {
return keysPressed.Contains(key);
}
public static bool HasKeyBeenReleased(Keys key) {
return keysReleased.Contains(key);
}
}
}