Merge KeyboardMapping into KeyInput and cleanup

This commit is contained in:
YoshiRulz 2020-10-16 13:44:36 +10:00
parent 7f5e777f75
commit 9d348c9c8b
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
3 changed files with 292 additions and 463 deletions

View File

@ -53,7 +53,8 @@ namespace BizHawk.Bizware.DirectX
}
}
public IEnumerable<KeyEvent> ProcessHostKeyboards() => KeyInput.Update(_config).Concat(IPCKeyInput.Update());
public IEnumerable<KeyEvent> ProcessHostKeyboards() => KeyInput.Update(_config ?? throw new NullReferenceException("o noes"))
.Concat(IPCKeyInput.Update());
public void UpdateConfig(Config config) => _config = config;
}

View File

@ -1,23 +1,33 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using BizHawk.Client.Common;
using SlimDX;
using SlimDX.DirectInput;
using static BizHawk.Common.Win32Imports;
using DInputKey = SlimDX.DirectInput.Key;
using OpenTKKey = OpenTK.Input.Key;
using WinFormsKey = System.Windows.Forms.Keys;
namespace BizHawk.Bizware.DirectX
{
internal static class KeyInput
{
private static readonly object SyncObj = new object();
private static readonly List<KeyEvent> EventList = new List<KeyEvent>();
private static DirectInput _directInput;
private static Keyboard _keyboard;
private static DirectInput? _directInput;
private static Keyboard? _keyboard;
private static readonly object _lockObj = new object();
public static void Initialize(IntPtr mainFormHandle)
{
lock (SyncObj)
lock (_lockObj)
{
Cleanup();
@ -31,68 +41,296 @@ namespace BizHawk.Bizware.DirectX
public static void Cleanup()
{
lock (SyncObj)
lock (_lockObj)
{
if (_keyboard != null)
{
_keyboard.Dispose();
_keyboard = null;
}
if (_directInput != null)
{
_directInput.Dispose();
_directInput = null;
}
_keyboard?.Dispose();
_keyboard = null;
_directInput?.Dispose();
_directInput = null;
}
}
internal static readonly Dictionary<Key, OpenTK.Input.Key> KeyEnumMap = new Dictionary<Key, OpenTK.Input.Key>
{
// A-Z
{Key.A, OpenTK.Input.Key.A}, {Key.B, OpenTK.Input.Key.B}, {Key.C, OpenTK.Input.Key.C}, {Key.D, OpenTK.Input.Key.D}, {Key.E, OpenTK.Input.Key.E}, {Key.F, OpenTK.Input.Key.F}, {Key.G, OpenTK.Input.Key.G}, {Key.H, OpenTK.Input.Key.H}, {Key.I, OpenTK.Input.Key.I}, {Key.J, OpenTK.Input.Key.J}, {Key.K, OpenTK.Input.Key.K}, {Key.L, OpenTK.Input.Key.L}, {Key.M, OpenTK.Input.Key.M}, {Key.N, OpenTK.Input.Key.N}, {Key.O, OpenTK.Input.Key.O}, {Key.P, OpenTK.Input.Key.P}, {Key.Q, OpenTK.Input.Key.Q}, {Key.R, OpenTK.Input.Key.R}, {Key.S, OpenTK.Input.Key.S}, {Key.T, OpenTK.Input.Key.T}, {Key.U, OpenTK.Input.Key.U}, {Key.V, OpenTK.Input.Key.V}, {Key.W, OpenTK.Input.Key.W}, {Key.X, OpenTK.Input.Key.X}, {Key.Y, OpenTK.Input.Key.Y}, {Key.Z, OpenTK.Input.Key.Z},
// 0-9
{Key.D1, OpenTK.Input.Key.Number1}, {Key.D2, OpenTK.Input.Key.Number2}, {Key.D3, OpenTK.Input.Key.Number3}, {Key.D4, OpenTK.Input.Key.Number4}, {Key.D5, OpenTK.Input.Key.Number5}, {Key.D6, OpenTK.Input.Key.Number6}, {Key.D7, OpenTK.Input.Key.Number7}, {Key.D8, OpenTK.Input.Key.Number8}, {Key.D9, OpenTK.Input.Key.Number9}, {Key.D0, OpenTK.Input.Key.Number0},
// misc. printables (ASCII order)
{Key.Space, OpenTK.Input.Key.Space}, {Key.Apostrophe, OpenTK.Input.Key.Quote}, {Key.Comma, OpenTK.Input.Key.Comma}, {Key.Minus, OpenTK.Input.Key.Minus}, {Key.Period, OpenTK.Input.Key.Period}, {Key.Slash, OpenTK.Input.Key.Slash}, {Key.Semicolon, OpenTK.Input.Key.Semicolon}, {Key.Equals, OpenTK.Input.Key.Plus}, {Key.LeftBracket, OpenTK.Input.Key.BracketLeft}, {Key.Backslash, OpenTK.Input.Key.BackSlash}, {Key.RightBracket, OpenTK.Input.Key.BracketRight}, {Key.Grave, OpenTK.Input.Key.Tilde},
// misc. (alphabetically)
{Key.Backspace, OpenTK.Input.Key.BackSpace}, {Key.CapsLock, OpenTK.Input.Key.CapsLock}, {Key.Delete, OpenTK.Input.Key.Delete}, {Key.DownArrow, OpenTK.Input.Key.Down}, {Key.End, OpenTK.Input.Key.End}, {Key.Return, OpenTK.Input.Key.Enter}, {Key.Escape, OpenTK.Input.Key.Escape}, {Key.Home, OpenTK.Input.Key.Home}, {Key.Insert, OpenTK.Input.Key.Insert}, {Key.LeftArrow, OpenTK.Input.Key.Left}, {Key.Oem102, OpenTK.Input.Key.NonUSBackSlash}, {Key.NumberLock, OpenTK.Input.Key.NumLock}, {Key.PageDown, OpenTK.Input.Key.PageDown}, {Key.PageUp, OpenTK.Input.Key.PageUp}, {Key.Pause, OpenTK.Input.Key.Pause}, {Key.PrintScreen, OpenTK.Input.Key.PrintScreen}, {Key.RightArrow, OpenTK.Input.Key.Right}, {Key.ScrollLock, OpenTK.Input.Key.ScrollLock}, {Key.Tab, OpenTK.Input.Key.Tab}, {Key.UpArrow, OpenTK.Input.Key.Up},
// modifier
{Key.LeftWindowsKey, OpenTK.Input.Key.WinLeft}, {Key.RightWindowsKey, OpenTK.Input.Key.WinRight}, {Key.LeftControl, OpenTK.Input.Key.ControlLeft}, {Key.RightControl, OpenTK.Input.Key.ControlRight}, {Key.LeftAlt, OpenTK.Input.Key.AltLeft}, {Key.RightAlt, OpenTK.Input.Key.AltRight}, {Key.LeftShift, OpenTK.Input.Key.ShiftLeft}, {Key.RightShift, OpenTK.Input.Key.ShiftRight},
// function
{Key.F1, OpenTK.Input.Key.F1}, {Key.F2, OpenTK.Input.Key.F2}, {Key.F3, OpenTK.Input.Key.F3}, {Key.F4, OpenTK.Input.Key.F4}, {Key.F5, OpenTK.Input.Key.F5}, {Key.F6, OpenTK.Input.Key.F6}, {Key.F7, OpenTK.Input.Key.F7}, {Key.F8, OpenTK.Input.Key.F8}, {Key.F9, OpenTK.Input.Key.F9}, {Key.F10, OpenTK.Input.Key.F10}, {Key.F11, OpenTK.Input.Key.F11}, {Key.F12, OpenTK.Input.Key.F12},
// keypad (alphabetically)
{Key.NumberPad0, OpenTK.Input.Key.Keypad0}, {Key.NumberPad1, OpenTK.Input.Key.Keypad1}, {Key.NumberPad2, OpenTK.Input.Key.Keypad2}, {Key.NumberPad3, OpenTK.Input.Key.Keypad3}, {Key.NumberPad4, OpenTK.Input.Key.Keypad4}, {Key.NumberPad5, OpenTK.Input.Key.Keypad5}, {Key.NumberPad6, OpenTK.Input.Key.Keypad6}, {Key.NumberPad7, OpenTK.Input.Key.Keypad7}, {Key.NumberPad8, OpenTK.Input.Key.Keypad8}, {Key.NumberPad9, OpenTK.Input.Key.Keypad9}, {Key.NumberPadPlus, OpenTK.Input.Key.KeypadAdd}, {Key.NumberPadPeriod, OpenTK.Input.Key.KeypadDecimal}, {Key.NumberPadSlash, OpenTK.Input.Key.KeypadDivide}, {Key.NumberPadEnter, OpenTK.Input.Key.KeypadEnter}, {Key.NumberPadStar, OpenTK.Input.Key.KeypadMultiply}, {Key.NumberPadMinus, OpenTK.Input.Key.KeypadSubtract}
};
public static IEnumerable<KeyEvent> Update(Config config)
{
OpenTK.Input.Key Mapped(Key k) => KeyEnumMap[config.HandleAlternateKeyboardLayouts ? KeyboardMapping.Handle(k) : k];
OpenTKKey Mapped(DInputKey k) => KeyEnumMap[config.HandleAlternateKeyboardLayouts ? MapToRealKeyViaScanCode(k) : k];
lock (SyncObj)
lock (_lockObj)
{
EventList.Clear();
if (_keyboard == null || _keyboard.Acquire().IsFailure || _keyboard.Poll().IsFailure) return Enumerable.Empty<KeyEvent>();
if (_keyboard == null || _keyboard.Acquire().IsFailure || _keyboard.Poll().IsFailure)
return EventList;
for (; ; )
var eventList = new List<KeyEvent>();
while (true)
{
var events = _keyboard.GetBufferedData();
if (Result.Last.IsFailure || events.Count == 0)
break;
if (Result.Last.IsFailure || events.Count == 0) return eventList;
foreach (var e in events)
{
foreach (var k in e.PressedKeys)
EventList.Add(new KeyEvent { Key = Mapped(k), Pressed = true });
foreach (var k in e.ReleasedKeys)
EventList.Add(new KeyEvent { Key = Mapped(k), Pressed = false });
foreach (var k in e.PressedKeys) eventList.Add(new KeyEvent { Key = Mapped(k), Pressed = true });
foreach (var k in e.ReleasedKeys) eventList.Add(new KeyEvent { Key = Mapped(k), Pressed = false });
}
}
return EventList;
}
}
private static WinFormsKey MapWin32VirtualScanCodeToVirtualKey(uint scanCode)
{
const uint MAPVK_VSC_TO_VK_EX = 0x03;
return (WinFormsKey) MapVirtualKey(scanCode, MAPVK_VSC_TO_VK_EX);
}
private static DInputKey MapToRealKeyViaScanCode(DInputKey key)
{
if (key == DInputKey.Unknown) return DInputKey.Unknown;
var scanCode = key switch
{
DInputKey.D0 => 0x0BU,
DInputKey.D1 => 0x02U,
DInputKey.D2 => 0x03U,
DInputKey.D3 => 0x04U,
DInputKey.D4 => 0x05U,
DInputKey.D5 => 0x06U,
DInputKey.D6 => 0x07U,
DInputKey.D7 => 0x08U,
DInputKey.D8 => 0x09U,
DInputKey.D9 => 0x0AU,
DInputKey.A => 0x1EU,
DInputKey.B => 0x30U,
DInputKey.C => 0x2EU,
DInputKey.D => 0x20U,
DInputKey.E => 0x12U,
DInputKey.F => 0x21U,
DInputKey.G => 0x22U,
DInputKey.H => 0x23U,
DInputKey.I => 0x17U,
DInputKey.J => 0x24U,
DInputKey.K => 0x25U,
DInputKey.L => 0x26U,
DInputKey.M => 0x32U,
DInputKey.N => 0x31U,
DInputKey.O => 0x18U,
DInputKey.P => 0x19U,
DInputKey.Q => 0x10U,
DInputKey.R => 0x13U,
DInputKey.S => 0x1FU,
DInputKey.T => 0x14U,
DInputKey.U => 0x16U,
DInputKey.V => 0x2FU,
DInputKey.W => 0x11U,
DInputKey.X => 0x2DU,
DInputKey.Y => 0x15U,
DInputKey.Z => 0x2CU,
DInputKey.AbntC1 => 0x73U,
DInputKey.AbntC2 => 0x7EU,
DInputKey.Apostrophe => 0x28U,
DInputKey.Applications => 0xDDU,
DInputKey.AT => 0x91U,
DInputKey.AX => 0x96U,
DInputKey.Backspace => 0x0EU,
DInputKey.Backslash => 0x2BU,
DInputKey.Calculator => 0xA1U,
DInputKey.CapsLock => 0x3AU,
DInputKey.Colon => 0x92U,
DInputKey.Comma => 0x33U,
DInputKey.Convert => 0x79U,
DInputKey.Delete => 0xD3U,
DInputKey.DownArrow => 0xD0U,
DInputKey.End => 0xCFU,
DInputKey.Equals => 0x0DU,
DInputKey.Escape => 0x01U,
DInputKey.F1 => 0x3BU,
DInputKey.F2 => 0x3CU,
DInputKey.F3 => 0x3DU,
DInputKey.F4 => 0x3EU,
DInputKey.F5 => 0x3FU,
DInputKey.F6 => 0x40U,
DInputKey.F7 => 0x41U,
DInputKey.F8 => 0x42U,
DInputKey.F9 => 0x43U,
DInputKey.F10 => 0x44U,
DInputKey.F11 => 0x57U,
DInputKey.F12 => 0x58U,
DInputKey.F13 => 0x64U,
DInputKey.F14 => 0x65U,
DInputKey.F15 => 0x66U,
DInputKey.Grave => 0x29U,
DInputKey.Home => 0xC7U,
DInputKey.Insert => 0xD2U,
DInputKey.Kana => 0x70U,
DInputKey.Kanji => 0x94U,
DInputKey.LeftBracket => 0x1AU,
DInputKey.LeftControl => 0x1DU,
DInputKey.LeftArrow => 0xCBU,
DInputKey.LeftAlt => 0x38U,
DInputKey.LeftShift => 0x2AU,
DInputKey.LeftWindowsKey => 0xDBU,
DInputKey.Mail => 0xECU,
DInputKey.MediaSelect => 0xEDU,
DInputKey.MediaStop => 0xA4U,
DInputKey.Minus => 0x0CU,
DInputKey.Mute => 0xA0U,
DInputKey.MyComputer => 0xEBU,
DInputKey.NextTrack => 0x99U,
DInputKey.NoConvert => 0x7BU,
DInputKey.NumberLock => 0x45U,
DInputKey.NumberPad0 => 0x52U,
DInputKey.NumberPad1 => 0x4FU,
DInputKey.NumberPad2 => 0x50U,
DInputKey.NumberPad3 => 0x51U,
DInputKey.NumberPad4 => 0x4BU,
DInputKey.NumberPad5 => 0x4CU,
DInputKey.NumberPad6 => 0x4DU,
DInputKey.NumberPad7 => 0x47U,
DInputKey.NumberPad8 => 0x48U,
DInputKey.NumberPad9 => 0x49U,
DInputKey.NumberPadComma => 0xB3U,
DInputKey.NumberPadEnter => 0x9CU,
DInputKey.NumberPadEquals => 0x8DU,
DInputKey.NumberPadMinus => 0x4AU,
DInputKey.NumberPadPeriod => 0x53U,
DInputKey.NumberPadPlus => 0x4EU,
DInputKey.NumberPadSlash => 0xB5U,
DInputKey.NumberPadStar => 0x37U,
DInputKey.Oem102 => 0x56U,
DInputKey.PageDown => 0xD1U,
DInputKey.PageUp => 0xC9U,
DInputKey.Pause => 0xC5U,
DInputKey.Period => 0x34U,
DInputKey.PlayPause => 0xA2U,
DInputKey.Power => 0xDEU,
DInputKey.PreviousTrack => 0x90U,
DInputKey.RightBracket => 0x1BU,
DInputKey.RightControl => 0x9DU,
DInputKey.Return => 0x1CU,
DInputKey.RightArrow => 0xCDU,
DInputKey.RightAlt => 0xB8U,
DInputKey.RightShift => 0x36U,
DInputKey.RightWindowsKey => 0xDCU,
DInputKey.ScrollLock => 0x46U,
DInputKey.Semicolon => 0x27U,
DInputKey.Slash => 0x35U,
DInputKey.Sleep => 0xDFU,
DInputKey.Space => 0x39U,
DInputKey.Stop => 0x95U,
DInputKey.PrintScreen => 0xB7U,
DInputKey.Tab => 0x0FU,
DInputKey.Underline => 0x93U,
DInputKey.Unlabeled => 0x97U,
DInputKey.UpArrow => 0xC8U,
DInputKey.VolumeDown => 0xAEU,
DInputKey.VolumeUp => 0xB0U,
DInputKey.Wake => 0xE3U,
DInputKey.WebBack => 0xEAU,
DInputKey.WebFavorites => 0xE6U,
DInputKey.WebForward => 0xE9U,
DInputKey.WebHome => 0xB2U,
DInputKey.WebRefresh => 0xE7U,
DInputKey.WebSearch => 0xE5U,
DInputKey.WebStop => 0xE8U,
DInputKey.Yen => 0x7DU,
_ => throw new Exception("this should never be hit, every enum member has been accounted for")
};
return MapWin32VirtualScanCodeToVirtualKey(scanCode) switch
{
WinFormsKey.D0 => DInputKey.D0,
WinFormsKey.D1 => DInputKey.D1,
WinFormsKey.D2 => DInputKey.D2,
WinFormsKey.D3 => DInputKey.D3,
WinFormsKey.D4 => DInputKey.D4,
WinFormsKey.D5 => DInputKey.D5,
WinFormsKey.D6 => DInputKey.D6,
WinFormsKey.D7 => DInputKey.D7,
WinFormsKey.D8 => DInputKey.D8,
WinFormsKey.D9 => DInputKey.D9,
WinFormsKey.A => DInputKey.A,
WinFormsKey.B => DInputKey.B,
WinFormsKey.C => DInputKey.C,
WinFormsKey.D => DInputKey.D,
WinFormsKey.E => DInputKey.E,
WinFormsKey.F => DInputKey.F,
WinFormsKey.G => DInputKey.G,
WinFormsKey.H => DInputKey.H,
WinFormsKey.I => DInputKey.I,
WinFormsKey.J => DInputKey.J,
WinFormsKey.K => DInputKey.K,
WinFormsKey.L => DInputKey.L,
WinFormsKey.M => DInputKey.M,
WinFormsKey.N => DInputKey.N,
WinFormsKey.O => DInputKey.O,
WinFormsKey.P => DInputKey.P,
WinFormsKey.Q => DInputKey.Q,
WinFormsKey.R => DInputKey.R,
WinFormsKey.S => DInputKey.S,
WinFormsKey.T => DInputKey.T,
WinFormsKey.U => DInputKey.U,
WinFormsKey.V => DInputKey.V,
WinFormsKey.W => DInputKey.W,
WinFormsKey.X => DInputKey.X,
WinFormsKey.Y => DInputKey.Y,
WinFormsKey.Z => DInputKey.Z,
WinFormsKey.OemQuotes => DInputKey.Apostrophe,
WinFormsKey.Back => DInputKey.Backspace,
WinFormsKey.OemPipe => DInputKey.Backslash,
WinFormsKey.Capital => DInputKey.CapsLock,
WinFormsKey.Oemcomma => DInputKey.Comma,
WinFormsKey.Oemplus => DInputKey.Equals,
WinFormsKey.Escape => DInputKey.Escape,
WinFormsKey.F1 => DInputKey.F1,
WinFormsKey.F2 => DInputKey.F2,
WinFormsKey.F3 => DInputKey.F3,
WinFormsKey.F4 => DInputKey.F4,
WinFormsKey.F5 => DInputKey.F5,
WinFormsKey.F6 => DInputKey.F6,
WinFormsKey.F7 => DInputKey.F7,
WinFormsKey.F8 => DInputKey.F8,
WinFormsKey.F9 => DInputKey.F9,
WinFormsKey.F10 => DInputKey.F10,
WinFormsKey.F11 => DInputKey.F11,
WinFormsKey.F12 => DInputKey.F12,
WinFormsKey.F13 => DInputKey.F13,
WinFormsKey.F14 => DInputKey.F14,
WinFormsKey.F15 => DInputKey.F15,
WinFormsKey.Oemtilde => DInputKey.Grave,
WinFormsKey.OemOpenBrackets => DInputKey.LeftBracket,
WinFormsKey.LControlKey => DInputKey.LeftControl,
WinFormsKey.LMenu => DInputKey.LeftAlt,
WinFormsKey.LShiftKey => DInputKey.LeftShift,
WinFormsKey.OemMinus => DInputKey.Minus,
WinFormsKey.NumLock => DInputKey.NumberLock,
WinFormsKey.Subtract => DInputKey.NumberPadMinus,
WinFormsKey.Add => DInputKey.NumberPadPlus,
WinFormsKey.Multiply => DInputKey.NumberPadStar,
WinFormsKey.OemBackslash => DInputKey.Oem102,
WinFormsKey.OemPeriod => DInputKey.Period,
WinFormsKey.OemCloseBrackets => DInputKey.RightBracket,
WinFormsKey.Return => DInputKey.Return,
WinFormsKey.RShiftKey => DInputKey.RightShift,
WinFormsKey.Scroll => DInputKey.ScrollLock,
WinFormsKey.OemSemicolon => DInputKey.Semicolon,
WinFormsKey.OemQuestion => DInputKey.Slash,
WinFormsKey.Space => DInputKey.Space,
WinFormsKey.Tab => DInputKey.Tab,
_ => DInputKey.Unknown
};
}
internal static readonly Dictionary<DInputKey, OpenTKKey> KeyEnumMap = new Dictionary<DInputKey, OpenTKKey>
{
// A-Z
{DInputKey.A, OpenTKKey.A}, {DInputKey.B, OpenTKKey.B}, {DInputKey.C, OpenTKKey.C}, {DInputKey.D, OpenTKKey.D}, {DInputKey.E, OpenTKKey.E}, {DInputKey.F, OpenTKKey.F}, {DInputKey.G, OpenTKKey.G}, {DInputKey.H, OpenTKKey.H}, {DInputKey.I, OpenTKKey.I}, {DInputKey.J, OpenTKKey.J}, {DInputKey.K, OpenTKKey.K}, {DInputKey.L, OpenTKKey.L}, {DInputKey.M, OpenTKKey.M}, {DInputKey.N, OpenTKKey.N}, {DInputKey.O, OpenTKKey.O}, {DInputKey.P, OpenTKKey.P}, {DInputKey.Q, OpenTKKey.Q}, {DInputKey.R, OpenTKKey.R}, {DInputKey.S, OpenTKKey.S}, {DInputKey.T, OpenTKKey.T}, {DInputKey.U, OpenTKKey.U}, {DInputKey.V, OpenTKKey.V}, {DInputKey.W, OpenTKKey.W}, {DInputKey.X, OpenTKKey.X}, {DInputKey.Y, OpenTKKey.Y}, {DInputKey.Z, OpenTKKey.Z},
// 0-9
{DInputKey.D1, OpenTKKey.Number1}, {DInputKey.D2, OpenTKKey.Number2}, {DInputKey.D3, OpenTKKey.Number3}, {DInputKey.D4, OpenTKKey.Number4}, {DInputKey.D5, OpenTKKey.Number5}, {DInputKey.D6, OpenTKKey.Number6}, {DInputKey.D7, OpenTKKey.Number7}, {DInputKey.D8, OpenTKKey.Number8}, {DInputKey.D9, OpenTKKey.Number9}, {DInputKey.D0, OpenTKKey.Number0},
// misc. printables (ASCII order)
{DInputKey.Space, OpenTKKey.Space}, {DInputKey.Apostrophe, OpenTKKey.Quote}, {DInputKey.Comma, OpenTKKey.Comma}, {DInputKey.Minus, OpenTKKey.Minus}, {DInputKey.Period, OpenTKKey.Period}, {DInputKey.Slash, OpenTKKey.Slash}, {DInputKey.Semicolon, OpenTKKey.Semicolon}, {DInputKey.Equals, OpenTKKey.Plus}, {DInputKey.LeftBracket, OpenTKKey.BracketLeft}, {DInputKey.Backslash, OpenTKKey.BackSlash}, {DInputKey.RightBracket, OpenTKKey.BracketRight}, {DInputKey.Grave, OpenTKKey.Tilde},
// misc. (alphabetically)
{DInputKey.Backspace, OpenTKKey.BackSpace}, {DInputKey.CapsLock, OpenTKKey.CapsLock}, {DInputKey.Delete, OpenTKKey.Delete}, {DInputKey.DownArrow, OpenTKKey.Down}, {DInputKey.End, OpenTKKey.End}, {DInputKey.Return, OpenTKKey.Enter}, {DInputKey.Escape, OpenTKKey.Escape}, {DInputKey.Home, OpenTKKey.Home}, {DInputKey.Insert, OpenTKKey.Insert}, {DInputKey.LeftArrow, OpenTKKey.Left}, {DInputKey.Oem102, OpenTKKey.NonUSBackSlash}, {DInputKey.NumberLock, OpenTKKey.NumLock}, {DInputKey.PageDown, OpenTKKey.PageDown}, {DInputKey.PageUp, OpenTKKey.PageUp}, {DInputKey.Pause, OpenTKKey.Pause}, {DInputKey.PrintScreen, OpenTKKey.PrintScreen}, {DInputKey.RightArrow, OpenTKKey.Right}, {DInputKey.ScrollLock, OpenTKKey.ScrollLock}, {DInputKey.Tab, OpenTKKey.Tab}, {DInputKey.UpArrow, OpenTKKey.Up},
// modifier
{DInputKey.LeftWindowsKey, OpenTKKey.WinLeft}, {DInputKey.RightWindowsKey, OpenTKKey.WinRight}, {DInputKey.LeftControl, OpenTKKey.ControlLeft}, {DInputKey.RightControl, OpenTKKey.ControlRight}, {DInputKey.LeftAlt, OpenTKKey.AltLeft}, {DInputKey.RightAlt, OpenTKKey.AltRight}, {DInputKey.LeftShift, OpenTKKey.ShiftLeft}, {DInputKey.RightShift, OpenTKKey.ShiftRight},
// function
{DInputKey.F1, OpenTKKey.F1}, {DInputKey.F2, OpenTKKey.F2}, {DInputKey.F3, OpenTKKey.F3}, {DInputKey.F4, OpenTKKey.F4}, {DInputKey.F5, OpenTKKey.F5}, {DInputKey.F6, OpenTKKey.F6}, {DInputKey.F7, OpenTKKey.F7}, {DInputKey.F8, OpenTKKey.F8}, {DInputKey.F9, OpenTKKey.F9}, {DInputKey.F10, OpenTKKey.F10}, {DInputKey.F11, OpenTKKey.F11}, {DInputKey.F12, OpenTKKey.F12},
// keypad (alphabetically)
{DInputKey.NumberPad0, OpenTKKey.Keypad0}, {DInputKey.NumberPad1, OpenTKKey.Keypad1}, {DInputKey.NumberPad2, OpenTKKey.Keypad2}, {DInputKey.NumberPad3, OpenTKKey.Keypad3}, {DInputKey.NumberPad4, OpenTKKey.Keypad4}, {DInputKey.NumberPad5, OpenTKKey.Keypad5}, {DInputKey.NumberPad6, OpenTKKey.Keypad6}, {DInputKey.NumberPad7, OpenTKKey.Keypad7}, {DInputKey.NumberPad8, OpenTKKey.Keypad8}, {DInputKey.NumberPad9, OpenTKKey.Keypad9}, {DInputKey.NumberPadPlus, OpenTKKey.KeypadAdd}, {DInputKey.NumberPadPeriod, OpenTKKey.KeypadDecimal}, {DInputKey.NumberPadSlash, OpenTKKey.KeypadDivide}, {DInputKey.NumberPadEnter, OpenTKKey.KeypadEnter}, {DInputKey.NumberPadStar, OpenTKKey.KeypadMultiply}, {DInputKey.NumberPadMinus, OpenTKKey.KeypadSubtract}
};
}
}

View File

@ -1,410 +0,0 @@
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using SlimDX.DirectInput;
namespace BizHawk.Bizware.DirectX
{
internal static class KeyboardMapping
{
private const uint MAPVK_VSC_TO_VK_EX = 0x03;
public static Key Handle(Key key)
{
ScanCode inputScanCode = SlimDXScanCodeMap[(int)key];
Keys virtualKey = (Keys)BizHawk.Common.Win32Imports.MapVirtualKey((uint)inputScanCode, MAPVK_VSC_TO_VK_EX);
ScanCode standardScanCode = GetStandardScanCode(virtualKey);
if (standardScanCode == 0)
standardScanCode = inputScanCode;
return ScanCodeToSlimDXKey[standardScanCode];
}
private static ScanCode GetStandardScanCode(Keys virtualKey)
{
switch (virtualKey)
{
case Keys.Escape: return ScanCode.Escape;
case Keys.D1: return ScanCode.D1;
case Keys.D2: return ScanCode.D2;
case Keys.D3: return ScanCode.D3;
case Keys.D4: return ScanCode.D4;
case Keys.D5: return ScanCode.D5;
case Keys.D6: return ScanCode.D6;
case Keys.D7: return ScanCode.D7;
case Keys.D8: return ScanCode.D8;
case Keys.D9: return ScanCode.D9;
case Keys.D0: return ScanCode.D0;
case Keys.OemMinus: return ScanCode.Minus;
case Keys.Oemplus: return ScanCode.Equals;
case Keys.Back: return ScanCode.Back;
case Keys.Tab: return ScanCode.Tab;
case Keys.Q: return ScanCode.Q;
case Keys.W: return ScanCode.W;
case Keys.E: return ScanCode.E;
case Keys.R: return ScanCode.R;
case Keys.T: return ScanCode.T;
case Keys.Y: return ScanCode.Y;
case Keys.U: return ScanCode.U;
case Keys.I: return ScanCode.I;
case Keys.O: return ScanCode.O;
case Keys.P: return ScanCode.P;
case Keys.OemOpenBrackets: return ScanCode.LBracket;
case Keys.OemCloseBrackets: return ScanCode.RBracket;
case Keys.Return: return ScanCode.Return;
case Keys.LControlKey: return ScanCode.LControl;
case Keys.A: return ScanCode.A;
case Keys.S: return ScanCode.S;
case Keys.D: return ScanCode.D;
case Keys.F: return ScanCode.F;
case Keys.G: return ScanCode.G;
case Keys.H: return ScanCode.H;
case Keys.J: return ScanCode.J;
case Keys.K: return ScanCode.K;
case Keys.L: return ScanCode.L;
case Keys.OemSemicolon: return ScanCode.Semicolon;
case Keys.OemQuotes: return ScanCode.Apostrophe;
case Keys.Oemtilde: return ScanCode.Grave;
case Keys.LShiftKey: return ScanCode.LShift;
case Keys.OemPipe: return ScanCode.Backslash;
case Keys.Z: return ScanCode.Z;
case Keys.X: return ScanCode.X;
case Keys.C: return ScanCode.C;
case Keys.V: return ScanCode.V;
case Keys.B: return ScanCode.B;
case Keys.N: return ScanCode.N;
case Keys.M: return ScanCode.M;
case Keys.Oemcomma: return ScanCode.Comma;
case Keys.OemPeriod: return ScanCode.Period;
case Keys.OemQuestion: return ScanCode.Slash;
case Keys.RShiftKey: return ScanCode.RShift;
case Keys.Multiply: return ScanCode.Multiply;
case Keys.LMenu: return ScanCode.LMenu;
case Keys.Space: return ScanCode.Space;
case Keys.Capital: return ScanCode.Capital;
case Keys.F1: return ScanCode.F1;
case Keys.F2: return ScanCode.F2;
case Keys.F3: return ScanCode.F3;
case Keys.F4: return ScanCode.F4;
case Keys.F5: return ScanCode.F5;
case Keys.F6: return ScanCode.F6;
case Keys.F7: return ScanCode.F7;
case Keys.F8: return ScanCode.F8;
case Keys.F9: return ScanCode.F9;
case Keys.F10: return ScanCode.F10;
case Keys.NumLock: return ScanCode.NumLock;
case Keys.Scroll: return ScanCode.Scroll;
case Keys.Subtract: return ScanCode.Subtract;
case Keys.Add: return ScanCode.Add;
case Keys.OemBackslash: return ScanCode.Oem_102;
case Keys.F11: return ScanCode.F11;
case Keys.F12: return ScanCode.F12;
case Keys.F13: return ScanCode.F13;
case Keys.F14: return ScanCode.F14;
case Keys.F15: return ScanCode.F15;
}
return 0;
}
private enum ScanCode
{
Escape = 0x01,
D1 = 0x02,
D2 = 0x03,
D3 = 0x04,
D4 = 0x05,
D5 = 0x06,
D6 = 0x07,
D7 = 0x08,
D8 = 0x09,
D9 = 0x0A,
D0 = 0x0B,
Minus = 0x0C,
Equals = 0x0D,
Back = 0x0E,
Tab = 0x0F,
Q = 0x10,
W = 0x11,
E = 0x12,
R = 0x13,
T = 0x14,
Y = 0x15,
U = 0x16,
I = 0x17,
O = 0x18,
P = 0x19,
LBracket = 0x1A,
RBracket = 0x1B,
Return = 0x1C,
LControl = 0x1D,
A = 0x1E,
S = 0x1F,
D = 0x20,
F = 0x21,
G = 0x22,
H = 0x23,
J = 0x24,
K = 0x25,
L = 0x26,
Semicolon = 0x27,
Apostrophe = 0x28,
Grave = 0x29,
LShift = 0x2A,
Backslash = 0x2B,
Z = 0x2C,
X = 0x2D,
C = 0x2E,
V = 0x2F,
B = 0x30,
N = 0x31,
M = 0x32,
Comma = 0x33,
Period = 0x34,
Slash = 0x35,
RShift = 0x36,
Multiply = 0x37,
LMenu = 0x38,
Space = 0x39,
Capital = 0x3A,
F1 = 0x3B,
F2 = 0x3C,
F3 = 0x3D,
F4 = 0x3E,
F5 = 0x3F,
F6 = 0x40,
F7 = 0x41,
F8 = 0x42,
F9 = 0x43,
F10 = 0x44,
NumLock = 0x45,
Scroll = 0x46,
NumPad7 = 0x47,
NumPad8 = 0x48,
NumPad9 = 0x49,
Subtract = 0x4A,
NumPad4 = 0x4B,
NumPad5 = 0x4C,
NumPad6 = 0x4D,
Add = 0x4E,
NumPad1 = 0x4F,
NumPad2 = 0x50,
NumPad3 = 0x51,
NumPad0 = 0x52,
Decimal = 0x53,
Oem_102 = 0x56,
F11 = 0x57,
F12 = 0x58,
F13 = 0x64,
F14 = 0x65,
F15 = 0x66,
Kana = 0x70,
Abnt_C1 = 0x73,
Convert = 0x79,
NoConvert = 0x7B,
Yen = 0x7D,
Abnt_C2 = 0x7E,
NumPadEquals = 0x8D,
PrevTrack = 0x90,
AT = 0x91,
Colon = 0x92,
Underline = 0x93,
Kanji = 0x94,
Stop = 0x95,
AX = 0x96,
Unlabeled = 0x97,
NextTrack = 0x99,
NumPadEnter = 0x9C,
RControl = 0x9D,
Mute = 0xA0,
Calculator = 0xA1,
PlayPause = 0xA2,
MediaStop = 0xA4,
VolumeDown = 0xAE,
VolumeUp = 0xB0,
WebHome = 0xB2,
NumPadComma = 0xB3,
Divide = 0xB5,
SysRq = 0xB7,
RMenu = 0xB8,
Pause = 0xC5,
Home = 0xC7,
Up = 0xC8,
Prior = 0xC9,
Left = 0xCB,
Right = 0xCD,
End = 0xCF,
Down = 0xD0,
Next = 0xD1,
Insert = 0xD2,
Delete = 0xD3,
LWin = 0xDB,
RWin = 0xDC,
Apps = 0xDD,
Power = 0xDE,
Sleep = 0xDF,
Wake = 0xE3,
WebSearch = 0xE5,
WebFavorites = 0xE6,
WebRefresh = 0xE7,
WebStop = 0xE8,
WebForward = 0xE9,
WebBack = 0xEA,
MyComputer = 0xEB,
Mail = 0xEC,
MediaSelect = 0xED
}
private static readonly ScanCode[] SlimDXScanCodeMap = new ScanCode[]
{
ScanCode.D0, // 0
ScanCode.D1, // 1
ScanCode.D2, // 2
ScanCode.D3, // 3
ScanCode.D4, // 4
ScanCode.D5, // 5
ScanCode.D6, // 6
ScanCode.D7, // 7
ScanCode.D8, // 8
ScanCode.D9, // 9
ScanCode.A, // 10
ScanCode.B, // 11
ScanCode.C, // 12
ScanCode.D, // 13
ScanCode.E, // 14
ScanCode.F, // 15
ScanCode.G, // 16
ScanCode.H, // 17
ScanCode.I, // 18
ScanCode.J, // 19
ScanCode.K, // 20
ScanCode.L, // 21
ScanCode.M, // 22
ScanCode.N, // 23
ScanCode.O, // 24
ScanCode.P, // 25
ScanCode.Q, // 26
ScanCode.R, // 27
ScanCode.S, // 28
ScanCode.T, // 29
ScanCode.U, // 30
ScanCode.V, // 31
ScanCode.W, // 32
ScanCode.X, // 33
ScanCode.Y, // 34
ScanCode.Z, // 35
ScanCode.Abnt_C1, // 36
ScanCode.Abnt_C2, // 37
ScanCode.Apostrophe, // 38
ScanCode.Apps, // 39
ScanCode.AT, // 40
ScanCode.AX, // 41
ScanCode.Back, // 42
ScanCode.Backslash, // 43
ScanCode.Calculator, // 44
ScanCode.Capital, // 45
ScanCode.Colon, // 46
ScanCode.Comma, // 47
ScanCode.Convert, // 48
ScanCode.Delete, // 49
ScanCode.Down, // 50
ScanCode.End, // 51
ScanCode.Equals, // 52
ScanCode.Escape, // 53
ScanCode.F1, // 54
ScanCode.F2, // 55
ScanCode.F3, // 56
ScanCode.F4, // 57
ScanCode.F5, // 58
ScanCode.F6, // 59
ScanCode.F7, // 60
ScanCode.F8, // 61
ScanCode.F9, // 62
ScanCode.F10, // 63
ScanCode.F11, // 64
ScanCode.F12, // 65
ScanCode.F13, // 66
ScanCode.F14, // 67
ScanCode.F15, // 68
ScanCode.Grave, // 69
ScanCode.Home, // 70
ScanCode.Insert, // 71
ScanCode.Kana, // 72
ScanCode.Kanji, // 73
ScanCode.LBracket, // 74
ScanCode.LControl, // 75
ScanCode.Left, // 76
ScanCode.LMenu, // 77
ScanCode.LShift, // 78
ScanCode.LWin, // 79
ScanCode.Mail, // 80
ScanCode.MediaSelect, // 81
ScanCode.MediaStop, // 82
ScanCode.Minus, // 83
ScanCode.Mute, // 84
ScanCode.MyComputer, // 85
ScanCode.NextTrack, // 86
ScanCode.NoConvert, // 87
ScanCode.NumLock, // 88
ScanCode.NumPad0, // 89
ScanCode.NumPad1, // 90
ScanCode.NumPad2, // 91
ScanCode.NumPad3, // 92
ScanCode.NumPad4, // 93
ScanCode.NumPad5, // 94
ScanCode.NumPad6, // 95
ScanCode.NumPad7, // 96
ScanCode.NumPad8, // 97
ScanCode.NumPad9, // 98
ScanCode.NumPadComma, // 99
ScanCode.NumPadEnter, // 100
ScanCode.NumPadEquals, // 101
ScanCode.Subtract, // 102
ScanCode.Decimal, // 103
ScanCode.Add, // 104
ScanCode.Divide, // 105
ScanCode.Multiply, // 106
ScanCode.Oem_102, // 107
ScanCode.Next, // 108
ScanCode.Prior, // 109
ScanCode.Pause, // 110
ScanCode.Period, // 111
ScanCode.PlayPause, // 112
ScanCode.Power, // 113
ScanCode.PrevTrack, // 114
ScanCode.RBracket, // 115
ScanCode.RControl, // 116
ScanCode.Return, // 117
ScanCode.Right, // 118
ScanCode.RMenu, // 119
ScanCode.RShift, // 120
ScanCode.RWin, // 121
ScanCode.Scroll, // 122
ScanCode.Semicolon, // 123
ScanCode.Slash, // 124
ScanCode.Sleep, // 125
ScanCode.Space, // 126
ScanCode.Stop, // 127
ScanCode.SysRq, // 128
ScanCode.Tab, // 129
ScanCode.Underline, // 130
ScanCode.Unlabeled, // 131
ScanCode.Up, // 132
ScanCode.VolumeDown, // 133
ScanCode.VolumeUp, // 134
ScanCode.Wake, // 135
ScanCode.WebBack, // 136
ScanCode.WebFavorites, // 137
ScanCode.WebForward, // 138
ScanCode.WebHome, // 139
ScanCode.WebRefresh, // 140
ScanCode.WebSearch, // 141
ScanCode.WebStop, // 142
ScanCode.Yen, // 143
0 // 144
};
private static readonly Dictionary<ScanCode, Key> ScanCodeToSlimDXKey =
SlimDXScanCodeMap
.Select((n, i) => new { Value = n, Index = i })
.ToDictionary(n => n.Value, n => (Key)n.Index);
}
}