Cleanup OTK_Keyboard
This commit is contained in:
parent
e2ac1aca84
commit
e2a496c652
|
@ -1,4 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
|
||||
using BizHawk.Client.Common;
|
||||
|
||||
|
@ -8,26 +11,6 @@ namespace BizHawk.Client.EmuHawk
|
|||
{
|
||||
public static class OTK_Keyboard
|
||||
{
|
||||
private static readonly Key[] KeyList = {
|
||||
Key.Unknown,
|
||||
|
||||
// A-Z
|
||||
Key.A, Key.B, Key.C, Key.D, Key.E, Key.F, Key.G, Key.H, Key.I, Key.J, Key.K, Key.L, Key.M, Key.N, Key.O, Key.P, Key.Q, Key.R, Key.S, Key.T, Key.U, Key.V, Key.W, Key.X, Key.Y, Key.Z,
|
||||
// 0-9
|
||||
Key.Number1, Key.Number2, Key.Number3, Key.Number4, Key.Number5, Key.Number6, Key.Number7, Key.Number8, Key.Number9, Key.Number0,
|
||||
// misc. printables (ASCII order)
|
||||
Key.Space, Key.Quote, Key.Comma, Key.Minus, Key.Period, Key.Slash, Key.Semicolon, Key.Plus, Key.BracketLeft, Key.BackSlash, Key.BracketRight, Key.Tilde,
|
||||
// misc. (alphabetically)
|
||||
Key.BackSpace, Key.CapsLock, Key.Delete, Key.Down, Key.End, Key.Enter, Key.Escape, Key.Home, Key.Insert, Key.Left, Key.Menu, Key.NonUSBackSlash, Key.NumLock, Key.PageDown, Key.PageUp, Key.Pause, Key.PrintScreen, Key.Right, Key.ScrollLock, Key.Tab, Key.Up,
|
||||
// modifier
|
||||
Key.WinLeft, Key.WinRight, Key.ControlLeft, Key.ControlRight, Key.AltLeft, Key.AltRight, Key.ShiftLeft, Key.ShiftRight,
|
||||
|
||||
// function
|
||||
Key.F1, Key.F2, Key.F3, Key.F4, Key.F5, Key.F6, Key.F7, Key.F8, Key.F9, Key.F10, Key.F11, Key.F12,
|
||||
// keypad (alphabetically)
|
||||
Key.Keypad0, Key.Keypad1, Key.Keypad2, Key.Keypad3, Key.Keypad4, Key.Keypad5, Key.Keypad6, Key.Keypad7, Key.Keypad8, Key.Keypad9, Key.KeypadAdd, Key.KeypadDecimal, Key.KeypadDivide, Key.KeypadEnter, Key.KeypadMultiply, Key.KeypadSubtract
|
||||
};
|
||||
|
||||
private static readonly IReadOnlyList<DistinctKey> KeyEnumMap = new List<DistinctKey>
|
||||
{
|
||||
DistinctKey.Unknown, // Unknown
|
||||
|
@ -163,7 +146,6 @@ namespace BizHawk.Client.EmuHawk
|
|||
DistinctKey.OemBackslash, // NonUSBackSlash
|
||||
};
|
||||
|
||||
private static readonly List<KeyEvent> EventList = new List<KeyEvent>();
|
||||
private static KeyboardState _kbState;
|
||||
|
||||
public static void Initialize ()
|
||||
|
@ -173,18 +155,10 @@ namespace BizHawk.Client.EmuHawk
|
|||
|
||||
public static IEnumerable<KeyEvent> Update()
|
||||
{
|
||||
EventList.Clear();
|
||||
var lastState = _kbState;
|
||||
KeyboardState newState;
|
||||
try
|
||||
{
|
||||
_kbState = Keyboard.GetState();
|
||||
foreach (var key in KeyList)
|
||||
{
|
||||
if (lastState.IsKeyUp(key) && _kbState.IsKeyDown(key))
|
||||
EventList.Add(new KeyEvent(KeyEnumMap[(int) key], pressed: true));
|
||||
else if (lastState.IsKeyDown(key) && _kbState.IsKeyUp(key))
|
||||
EventList.Add(new KeyEvent(KeyEnumMap[(int) key], pressed: false));
|
||||
}
|
||||
newState = Keyboard.GetState();
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
@ -192,54 +166,28 @@ namespace BizHawk.Client.EmuHawk
|
|||
// In rare cases (sometimes it takes up to 10 minutes to occur) it will
|
||||
// be updating the keyboard state when we call GetState() and choke.
|
||||
// Until I fix OpenTK, it's fine to just swallow it because input continues working.
|
||||
if (System.Diagnostics.Debugger.IsAttached)
|
||||
if (Debugger.IsAttached) Console.WriteLine("OpenTK Keyboard thread is angry.");
|
||||
return Enumerable.Empty<KeyEvent>();
|
||||
}
|
||||
|
||||
var lastState = _kbState;
|
||||
_kbState = newState;
|
||||
if (lastState == _kbState) return Enumerable.Empty<KeyEvent>();
|
||||
|
||||
var eventList = new List<KeyEvent>();
|
||||
for (var i = 1; i < 131; i++)
|
||||
{
|
||||
var key = (Key) i;
|
||||
if (lastState.IsKeyUp(key) && _kbState.IsKeyDown(key))
|
||||
{
|
||||
System.Console.WriteLine("OpenTK Keyboard thread is angry.");
|
||||
eventList.Add(new KeyEvent(KeyEnumMap[i], pressed: true));
|
||||
}
|
||||
else if (lastState.IsKeyDown(key) && _kbState.IsKeyUp(key))
|
||||
{
|
||||
eventList.Add(new KeyEvent(KeyEnumMap[i], pressed: false));
|
||||
}
|
||||
}
|
||||
return EventList;
|
||||
}
|
||||
|
||||
public static bool IsPressed(Key key)
|
||||
{
|
||||
return _kbState.IsKeyDown(key);
|
||||
}
|
||||
|
||||
public static bool ShiftModifier => IsPressed(Key.ShiftLeft) || IsPressed(Key.ShiftRight);
|
||||
|
||||
public static bool CtrlModifier => IsPressed(Key.ControlLeft) || IsPressed(Key.ControlRight);
|
||||
|
||||
public static bool AltModifier => IsPressed(Key.AltLeft) || IsPressed(Key.AltRight);
|
||||
|
||||
public static Input.ModifierKey GetModifierKeysAsKeys()
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
internal static class KeyExtensions
|
||||
{
|
||||
public static bool IsModifier (this Key key)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
case Key.ShiftLeft:
|
||||
case Key.ShiftRight:
|
||||
case Key.ControlLeft:
|
||||
case Key.ControlRight:
|
||||
case Key.AltLeft:
|
||||
case Key.AltRight:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return eventList;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue