2014-04-20 03:38:03 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using SlimDX;
|
2011-01-11 02:55:51 +00:00
|
|
|
|
using SlimDX.DirectInput;
|
|
|
|
|
|
2013-11-03 03:54:37 +00:00
|
|
|
|
namespace BizHawk.Client.EmuHawk
|
2011-01-11 02:55:51 +00:00
|
|
|
|
{
|
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()
|
|
|
|
|
{
|
2011-07-08 05:15:28 +00:00
|
|
|
|
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);
|
2013-11-03 16:07:58 +00:00
|
|
|
|
keyboard.SetCooperativeLevel(GlobalWin.MainForm.Handle, CooperativeLevel.Background | CooperativeLevel.Nonexclusive);
|
2014-04-20 03:38:03 +00:00
|
|
|
|
keyboard.Properties.BufferSize = 8;
|
2011-06-19 23:31:58 +00:00
|
|
|
|
}
|
2011-01-11 02:55:51 +00:00
|
|
|
|
|
2014-04-20 03:38:03 +00:00
|
|
|
|
static List<KeyEvent> EmptyList = new List<KeyEvent>();
|
|
|
|
|
static List<KeyEvent> EventList = new List<KeyEvent>();
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<KeyEvent> Update()
|
2011-06-19 23:31:58 +00:00
|
|
|
|
{
|
2014-04-20 03:38:03 +00:00
|
|
|
|
EventList.Clear();
|
|
|
|
|
|
2011-06-19 23:31:58 +00:00
|
|
|
|
if (keyboard.Acquire().IsFailure)
|
2014-04-20 03:38:03 +00:00
|
|
|
|
return EmptyList;
|
2011-06-19 23:31:58 +00:00
|
|
|
|
if (keyboard.Poll().IsFailure)
|
2014-04-20 03:38:03 +00:00
|
|
|
|
return EmptyList;
|
|
|
|
|
|
|
|
|
|
for (; ; )
|
|
|
|
|
{
|
|
|
|
|
var events = keyboard.GetBufferedData();
|
|
|
|
|
if (Result.Last.IsFailure)
|
|
|
|
|
return EventList;
|
|
|
|
|
if (events.Count == 0)
|
|
|
|
|
break;
|
|
|
|
|
foreach (var e in events)
|
|
|
|
|
{
|
|
|
|
|
foreach (var k in e.PressedKeys)
|
|
|
|
|
EventList.Add(new KeyEvent { Key = k, Pressed = true });
|
|
|
|
|
foreach (var k in e.ReleasedKeys)
|
|
|
|
|
EventList.Add(new KeyEvent { Key = k, Pressed = false });
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-01-11 02:55:51 +00:00
|
|
|
|
|
2014-04-20 03:38:03 +00:00
|
|
|
|
return EventList;
|
2011-06-19 23:31:58 +00:00
|
|
|
|
}
|
2011-01-11 02:55:51 +00:00
|
|
|
|
|
2014-04-20 03:38:03 +00:00
|
|
|
|
public struct KeyEvent
|
|
|
|
|
{
|
|
|
|
|
public Key Key;
|
|
|
|
|
public bool Pressed;
|
|
|
|
|
}
|
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;
|
2011-06-20 02:24:41 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-04 23:36:06 +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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-10 02:14:58 +00:00
|
|
|
|
public static Input.ModifierKey GetModifierKeysAsKeys()
|
2011-07-09 22:09:39 +00:00
|
|
|
|
{
|
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;
|
2011-07-09 22:09:39 +00:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-19 23:31:58 +00:00
|
|
|
|
}
|
2011-07-04 23:36:06 +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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-03-12 04:44:34 +00:00
|
|
|
|
}
|