using System.Collections.Generic; using SlimDX; using SlimDX.DirectInput; namespace BizHawk.Client.EmuHawk { public static class KeyInput { private static DirectInput dinput; private static Keyboard keyboard; private static KeyboardState state = new KeyboardState(); public static void Initialize() { if (dinput == null) dinput = new DirectInput(); if (keyboard == null || keyboard.Disposed) keyboard = new Keyboard(dinput); keyboard.SetCooperativeLevel(GlobalWin.MainForm.Handle, CooperativeLevel.Background | CooperativeLevel.Nonexclusive); keyboard.Properties.BufferSize = 8; } static List EmptyList = new List(); static List EventList = new List(); public static IEnumerable Update() { EventList.Clear(); if (keyboard.Acquire().IsFailure) return EmptyList; 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 }); } } return EventList; } public struct KeyEvent { public Key Key; public bool Pressed; } } }