BizHawk/BizHawk.Client.EmuHawk/Input/Keyboard.cs

63 lines
1.5 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using SlimDX;
2011-01-11 02:55:51 +00:00
using SlimDX.DirectInput;
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()
{
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(GlobalWin.MainForm.Handle, CooperativeLevel.Background | CooperativeLevel.Nonexclusive);
keyboard.Properties.BufferSize = 8;
2011-06-19 23:31:58 +00:00
}
2011-01-11 02:55:51 +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
{
EventList.Clear();
2011-06-19 23:31:58 +00:00
if (keyboard.Acquire().IsFailure)
return EmptyList;
2011-06-19 23:31:58 +00:00
if (keyboard.Poll().IsFailure)
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
return EventList;
2011-06-19 23:31:58 +00:00
}
2011-01-11 02:55:51 +00:00
public struct KeyEvent
{
public Key Key;
public bool Pressed;
}
2011-01-11 02:55:51 +00:00
2011-06-19 23:31:58 +00:00
}
}