2012-08-09 09:45:04 +00:00
|
|
|
#region Using Statements
|
2011-10-31 05:36:24 +00:00
|
|
|
using System;
|
2011-11-11 12:50:46 +00:00
|
|
|
using System.Collections.Generic;
|
2011-10-31 05:36:24 +00:00
|
|
|
|
|
|
|
#endregion // Using Statements
|
|
|
|
|
2012-08-09 09:45:04 +00:00
|
|
|
// This file is part of the ANX.Framework created by the
|
|
|
|
// "ANX.Framework developer group" and released under the Ms-PL license.
|
|
|
|
// For details see: http://anxframework.codeplex.com/license
|
2011-10-31 05:36:24 +00:00
|
|
|
|
|
|
|
namespace ANX.Framework.Input
|
|
|
|
{
|
|
|
|
public struct KeyboardState
|
|
|
|
{
|
2011-11-11 12:50:46 +00:00
|
|
|
#region Private Members
|
|
|
|
private KeyState[] keyState;
|
|
|
|
private List<Keys> pressedKeys;
|
|
|
|
|
|
|
|
#endregion // Private Members
|
|
|
|
|
2012-01-16 13:48:21 +00:00
|
|
|
public KeyboardState(params Keys[] keys)
|
2011-11-11 12:50:46 +00:00
|
|
|
{
|
|
|
|
pressedKeys = new List<Keys>();
|
|
|
|
pressedKeys.AddRange(keys);
|
|
|
|
|
|
|
|
keyState = new KeyState[255];
|
|
|
|
keyState.Initialize();
|
|
|
|
|
|
|
|
for (int i = 0; i < keys.Length; i++)
|
|
|
|
{
|
|
|
|
keyState[(int)keys[i]] = KeyState.Down;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsKeyDown(Keys key)
|
|
|
|
{
|
2011-11-14 19:37:05 +00:00
|
|
|
return keyState != null ? keyState[(int)key] == KeyState.Down : false;
|
2011-11-11 12:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsKeyUp(Keys key)
|
|
|
|
{
|
2011-11-21 19:33:09 +00:00
|
|
|
return keyState != null ? keyState[(int)key] == KeyState.Up : true;
|
2011-11-11 12:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
if (obj != null && obj.GetType() == typeof(KeyboardState))
|
|
|
|
{
|
2012-01-19 12:13:42 +00:00
|
|
|
return this == (KeyboardState)obj;
|
2011-11-11 12:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator ==(KeyboardState lhs, KeyboardState rhs)
|
|
|
|
{
|
|
|
|
if (lhs.keyState.Length != rhs.keyState.Length)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < lhs.keyState.Length; i++)
|
|
|
|
{
|
|
|
|
if (lhs.keyState[i] != rhs.keyState[i])
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator !=(KeyboardState lhs, KeyboardState rhs)
|
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public KeyState this[Keys key]
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return keyState[(int)key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Keys[] GetPressedKeys()
|
|
|
|
{
|
2012-01-21 10:06:06 +00:00
|
|
|
List<Keys> value = new List<Keys>();
|
|
|
|
for (int i = 0; i < keyState.Length; ++i)
|
|
|
|
{
|
|
|
|
if (keyState[i] == KeyState.Down)
|
|
|
|
{
|
|
|
|
value.Add((Keys)i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value.ToArray();
|
2011-11-11 12:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal void AddPressedKey(Keys key)
|
|
|
|
{
|
|
|
|
this.pressedKeys.Add(key);
|
|
|
|
this.keyState[(int)key] = KeyState.Down;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void RemovePressedKey(Keys key)
|
|
|
|
{
|
|
|
|
this.pressedKeys.Remove(key);
|
|
|
|
this.keyState[(int)key] = KeyState.Up;
|
|
|
|
}
|
2011-10-31 05:36:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|