BizHawk/BizHawk.MultiClient/Input/Keyboard.cs

149 lines
4.0 KiB
C#
Raw Normal View History

2011-01-11 02:55:51 +00:00
using System.Collections.Generic;
using System.Text;
2011-01-11 02:55:51 +00:00
using SlimDX;
using SlimDX.DirectInput;
namespace BizHawk.MultiClient
{
2011-06-19 23:31:58 +00:00
public static class KeyInput
{
private static DirectInput dinput;
private static Keyboard keyboard;
private static KeyboardState state = new KeyboardState();
private static List<Key> unpressedKeys = new List<Key>();
2011-01-11 02:55:51 +00:00
2011-06-19 23:31:58 +00:00
public static void Initialize()
{
if (dinput == null)
2011-06-19 23:31:58 +00:00
dinput = new DirectInput();
2011-01-11 02:55:51 +00:00
2011-06-19 23:31:58 +00:00
if (keyboard == null || keyboard.Disposed)
keyboard = new Keyboard(dinput);
keyboard.SetCooperativeLevel(Global.MainForm.Handle, CooperativeLevel.Background | CooperativeLevel.Nonexclusive);
2011-06-19 23:31:58 +00:00
}
2011-01-11 02:55:51 +00:00
2011-06-19 23:31:58 +00:00
public static void Update()
{
if (keyboard.Acquire().IsFailure)
return;
if (keyboard.Poll().IsFailure)
return;
2011-01-11 02:55:51 +00:00
2011-06-19 23:31:58 +00:00
state = keyboard.GetCurrentState();
if (Result.Last.IsFailure)
return;
2011-01-11 02:55:51 +00:00
2011-06-19 23:31:58 +00:00
unpressedKeys.RemoveAll(key => state.IsReleased(key));
}
2011-01-11 02:55:51 +00:00
2011-06-19 23:31:58 +00:00
public static KeyboardState State { get { return state; } }
2011-01-11 02:55:51 +00:00
2011-06-19 23:31:58 +00:00
public static void Unpress(Key key)
{
if (unpressedKeys.Contains(key))
return;
unpressedKeys.Add(key);
}
2011-01-11 02:55:51 +00:00
2011-06-19 23:31:58 +00:00
public static bool IsPressed(Key key)
{
if (unpressedKeys.Contains(key))
return false;
if (state.IsPressed(key))
return true;
if (key == Key.LeftShift && state.IsPressed(Key.RightShift))
return true;
if (key == Key.LeftControl && state.IsPressed(Key.RightControl))
return true;
if (key == Key.LeftAlt && state.IsPressed(Key.RightAlt))
return true;
2011-06-19 23:31:58 +00:00
return false;
}
public static string GetPressedKey()
2011-06-19 23:31:58 +00:00
{
if (state.PressedKeys.Count == 0)
return null;
Key? key = null;
for (int i=0; i<state.PressedKeys.Count; i++)
{
if (state.PressedKeys[i].IsModifier())
continue;
key = state.PressedKeys[i];
break;
}
if (key == null)
return null;
string keystr = GetModifierKeys() ?? "";
return keystr + key;
2011-06-19 23:31:58 +00:00
}
public static bool ShiftModifier
{
get
{
if (state.IsPressed(Key.LeftShift)) return true;
if (state.IsPressed(Key.RightShift)) return true;
return false;
}
}
public static bool CtrlModifier
{
get
{
if (state.IsPressed(Key.LeftControl)) return true;
if (state.IsPressed(Key.RightControl)) return true;
return false;
}
}
public static bool AltModifier
{
get
{
if (state.IsPressed(Key.LeftAlt)) return true;
if (state.IsPressed(Key.RightAlt)) return true;
return false;
}
}
public static System.Windows.Forms.Keys GetModifierKeysAsKeys()
{
System.Windows.Forms.Keys ret = System.Windows.Forms.Keys.None;
if (ShiftModifier) ret |= System.Windows.Forms.Keys.Shift;
if (CtrlModifier) ret |= System.Windows.Forms.Keys.Control;
if (AltModifier) ret |= System.Windows.Forms.Keys.Alt;
return ret;
}
public static string GetModifierKeys()
{
StringBuilder sb = new StringBuilder(16);
if (ShiftModifier) sb.Append("Shift+");
if (CtrlModifier) sb.Append("Ctrl+");
if (AltModifier) sb.Append("Alt+");
if (sb.Length == 0) return null;
return sb.ToString();
}
2011-06-19 23:31:58 +00:00
}
internal static class KeyExtensions
{
public static bool IsModifier(this Key key)
{
if (key == Key.LeftShift) return true;
if (key == Key.RightShift) return true;
if (key == Key.LeftControl) return true;
if (key == Key.RightControl) return true;
if (key == Key.LeftAlt) return true;
if (key == Key.RightAlt) return true;
return false;
}
}
2011-01-11 02:55:51 +00:00
}