BizHawk/BizHawk.MultiClient/Input/Keyboard.cs

109 lines
2.8 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();
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
public static KeyboardState State { get { return state; } }
2011-01-11 02:55:51 +00:00
2011-07-10 06:24:04 +00:00
2011-06-19 23:31:58 +00:00
public static bool IsPressed(Key key)
{
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 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;
}
}
2011-07-10 02:14:58 +00:00
public static Input.ModifierKey GetModifierKeysAsKeys()
{
2011-07-10 02:14:58 +00:00
Input.ModifierKey ret = Input.ModifierKey.None;
if (ShiftModifier) ret |= Input.ModifierKey.Shift;
if (CtrlModifier) ret |= Input.ModifierKey.Control;
if (AltModifier) ret |= Input.ModifierKey.Alt;
return ret;
}
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;
}
}
}