[SDL2] Optimized Sdl2KeyCode translation

Also fixed Key.WinLeft, WinRight and Menu translation.
This commit is contained in:
thefiddler 2014-04-02 11:03:51 +02:00
parent 9f39aa1b88
commit 03a8a6da0e
3 changed files with 263 additions and 92 deletions

View file

@ -33,91 +33,270 @@ namespace OpenTK.Platform.SDL2
{ {
using Code = Scancode; using Code = Scancode;
class Sdl2KeyMap : Dictionary<Scancode, Key> class Sdl2KeyMap
{ {
public Sdl2KeyMap() public static Key GetKey(Code code)
{ {
Add(Code.ESCAPE, Key.Escape); switch (code)
// Function keys
for (int i = 0; i < 12; i++)
{ {
Add(Code.F1 + i, Key.F1 + i); case Code.ESCAPE:
return Key.Escape;
// Function keys
case Code.F1:
return Key.F1;
case Code.F2:
return Key.F2;
case Code.F3:
return Key.F3;
case Code.F4:
return Key.F4;
case Code.F5:
return Key.F5;
case Code.F6:
return Key.F6;
case Code.F7:
return Key.F7;
case Code.F8:
return Key.F8;
case Code.F9:
return Key.F9;
case Code.F10:
return Key.F10;
case Code.F11:
return Key.F11;
case Code.F12:
return Key.F12;
case Code.F13:
return Key.F13;
case Code.F14:
return Key.F14;
case Code.F15:
return Key.F15;
case Code.F16:
return Key.F16;
case Code.F17:
return Key.F17;
case Code.F18:
return Key.F18;
case Code.F19:
return Key.F19;
case Code.F20:
return Key.F20;
case Code.F21:
return Key.F21;
case Code.F22:
return Key.F22;
case Code.F23:
return Key.F23;
case Code.F24:
return Key.F24;
// Number keys (0-9)
case Code.Num0:
return Key.Number0;
case Code.Num1:
return Key.Number1;
case Code.Num2:
return Key.Number2;
case Code.Num3:
return Key.Number3;
case Code.Num4:
return Key.Number4;
case Code.Num5:
return Key.Number5;
case Code.Num6:
return Key.Number6;
case Code.Num7:
return Key.Number7;
case Code.Num8:
return Key.Number8;
case Code.Num9:
return Key.Number9;
// Letters (A-Z)
case Code.A:
return Key.A;
case Code.B:
return Key.B;
case Code.C:
return Key.C;
case Code.D:
return Key.D;
case Code.E:
return Key.E;
case Code.F:
return Key.F;
case Code.G:
return Key.G;
case Code.H:
return Key.H;
case Code.I:
return Key.I;
case Code.J:
return Key.J;
case Code.K:
return Key.K;
case Code.L:
return Key.L;
case Code.M:
return Key.M;
case Code.N:
return Key.N;
case Code.O:
return Key.O;
case Code.P:
return Key.P;
case Code.Q:
return Key.Q;
case Code.R:
return Key.R;
case Code.S:
return Key.S;
case Code.T:
return Key.T;
case Code.U:
return Key.U;
case Code.V:
return Key.V;
case Code.W:
return Key.W;
case Code.X:
return Key.X;
case Code.Y:
return Key.Y;
case Code.Z:
return Key.Z;
case Code.TAB:
return Key.Tab;
case Code.CAPSLOCK:
return Key.CapsLock;
case Code.LCTRL:
return Key.ControlLeft;
case Code.LSHIFT:
return Key.ShiftLeft;
case Code.LALT:
return Key.AltLeft;
case Code.MENU:
return Key.Menu;
case Code.LGUI:
return Key.WinLeft;
case Code.RGUI:
return Key.WinRight;
case Code.SPACE:
return Key.Space;
case Code.RALT:
return Key.AltRight;
//case Code.:
// return Key.WinRight;
case Code.APPLICATION:
return Key.Menu;
case Code.RCTRL:
return Key.ControlRight;
case Code.RSHIFT:
return Key.ShiftRight;
case Code.RETURN:
return Key.Enter;
case Code.BACKSPACE:
return Key.BackSpace;
case Code.SEMICOLON:
return Key.Semicolon; // Varies by keyboard: return ;: on Win2K/US
case Code.SLASH:
return Key.Slash; // Varies by keyboard: return /? on Win2K/US
case Code.GRAVE:
return Key.Tilde; // Varies by keyboard: return `~ on Win2K/US
case Code.LEFTBRACKET:
return Key.BracketLeft; // Varies by keyboard: return [{ on Win2K/US
case Code.BACKSLASH:
return Key.BackSlash; // Varies by keyboard: return \| on Win2K/US
case Code.RIGHTBRACKET:
return Key.BracketRight; // Varies by keyboard: return ]} on Win2K/US
case Code.APOSTROPHE:
return Key.Quote; // Varies by keyboard: return '" on Win2K/US
case Code.EQUALS:
return Key.Plus;
case Code.COMMA:
return Key.Comma; // Invariant: : return
case Code.MINUS:
return Key.Minus; // Invariant: -
case Code.PERIOD:
return Key.Period; // Invariant: .
case Code.HOME:
return Key.Home;
case Code.END:
return Key.End;
case Code.DELETE:
return Key.Delete;
case Code.PAGEUP:
return Key.PageUp;
case Code.PAGEDOWN:
return Key.PageDown;
case Code.PAUSE:
return Key.Pause;
case Code.NUMLOCKCLEAR:
return Key.NumLock;
case Code.SCROLLLOCK:
return Key.ScrollLock;
case Code.PRINTSCREEN:
return Key.PrintScreen;
case Code.CLEAR:
return Key.Clear;
case Code.INSERT:
return Key.Insert;
case Code.SLEEP:
return Key.Sleep;
// Keypad
case Code.KP_0:
return Key.Keypad0;
case Code.KP_1:
return Key.Keypad1;
case Code.KP_2:
return Key.Keypad2;
case Code.KP_3:
return Key.Keypad3;
case Code.KP_4:
return Key.Keypad4;
case Code.KP_5:
return Key.Keypad5;
case Code.KP_6:
return Key.Keypad6;
case Code.KP_7:
return Key.Keypad7;
case Code.KP_8:
return Key.Keypad8;
case Code.KP_9:
return Key.Keypad9;
case Code.KP_DECIMAL:
return Key.KeypadDecimal;
case Code.KP_PLUS:
return Key.KeypadAdd;
case Code.KP_MINUS:
return Key.KeypadSubtract;
case Code.KP_DIVIDE:
return Key.KeypadDivide;
case Code.KP_MULTIPLY:
return Key.KeypadMultiply;
// Navigation
case Code.UP:
return Key.Up;
case Code.DOWN:
return Key.Down;
case Code.LEFT:
return Key.Left;
case Code.RIGHT:
return Key.Right;
default:
return Key.Unknown;
} }
// Number keys (0-9)
Add(Code.Num0, Key.Number0);
for (int i = 0; i < 9; i++)
{
Add(Code.Num1 + i, Key.Number1 + i);
}
// Letters (A-Z)
for (int i = 0; i < 26; i++)
{
Add(Code.A + i, Key.A + i);
}
Add(Code.TAB, Key.Tab);
Add(Code.CAPSLOCK, Key.CapsLock);
Add(Code.LCTRL, Key.ControlLeft);
Add(Code.LSHIFT, Key.ShiftLeft);
Add(Code.LALT, Key.AltLeft);
Add(Code.MENU, Key.WinLeft);
Add(Code.SPACE, Key.Space);
Add(Code.RALT, Key.AltRight);
//Add(Code., Key.WinRight);
Add(Code.APPLICATION, Key.Menu);
Add(Code.RCTRL, Key.ControlRight);
Add(Code.RSHIFT, Key.ShiftRight);
Add(Code.RETURN, Key.Enter);
Add(Code.BACKSPACE, Key.BackSpace);
Add(Code.SEMICOLON, Key.Semicolon); // Varies by keyboard, ;: on Win2K/US
Add(Code.SLASH, Key.Slash); // Varies by keyboard, /? on Win2K/US
Add(Code.GRAVE, Key.Tilde); // Varies by keyboard, `~ on Win2K/US
Add(Code.LEFTBRACKET, Key.BracketLeft); // Varies by keyboard, [{ on Win2K/US
Add(Code.BACKSLASH, Key.BackSlash); // Varies by keyboard, \| on Win2K/US
Add(Code.RIGHTBRACKET, Key.BracketRight); // Varies by keyboard, ]} on Win2K/US
Add(Code.APOSTROPHE, Key.Quote); // Varies by keyboard, '" on Win2K/US
Add(Code.EQUALS, Key.Plus);
Add(Code.COMMA, Key.Comma); // Invariant: ,
Add(Code.MINUS, Key.Minus); // Invariant: -
Add(Code.PERIOD, Key.Period); // Invariant: .
Add(Code.HOME, Key.Home);
Add(Code.END, Key.End);
Add(Code.DELETE, Key.Delete);
Add(Code.PAGEUP, Key.PageUp);
Add(Code.PAGEDOWN, Key.PageDown);
Add(Code.PAUSE, Key.Pause);
Add(Code.NUMLOCKCLEAR, Key.NumLock);
Add(Code.SCROLLLOCK, Key.ScrollLock);
Add(Code.PRINTSCREEN, Key.PrintScreen);
Add(Code.CLEAR, Key.Clear);
Add(Code.INSERT, Key.Insert);
Add(Code.SLEEP, Key.Sleep);
// Keypad
for (int i = 0; i < 9; i++)
{
Add(Code.KP_1 + i, Key.Keypad1 + i);
}
Add(Code.KP_0, Key.Keypad0); // Note: SDL2 goes KP_1..KP_9, then KP_0
Add(Code.KP_DECIMAL, Key.KeypadDecimal);
Add(Code.KP_PLUS, Key.KeypadAdd);
Add(Code.KP_MINUS, Key.KeypadSubtract);
Add(Code.KP_DIVIDE, Key.KeypadDivide);
Add(Code.KP_MULTIPLY, Key.KeypadMultiply);
// Navigation
Add(Code.UP, Key.Up);
Add(Code.DOWN, Key.Down);
Add(Code.LEFT, Key.Left);
Add(Code.RIGHT, Key.Right);
} }
} }
} }

View file

@ -33,7 +33,6 @@ namespace OpenTK.Platform.SDL2
{ {
class Sdl2Keyboard : IKeyboardDriver2, IKeyboardDriver class Sdl2Keyboard : IKeyboardDriver2, IKeyboardDriver
{ {
static readonly Sdl2KeyMap KeyMap = new Sdl2KeyMap();
KeyboardState state; KeyboardState state;
readonly List<KeyboardDevice> keyboards = readonly List<KeyboardDevice> keyboards =
@ -84,10 +83,10 @@ namespace OpenTK.Platform.SDL2
internal void ProcessKeyboardEvent(KeyboardEvent e) internal void ProcessKeyboardEvent(KeyboardEvent e)
{ {
Key key;
bool pressed = e.State != 0; bool pressed = e.State != 0;
var scancode = e.Keysym.Scancode; var scancode = e.Keysym.Scancode;
if (KeyMap.TryGetValue(scancode, out key)) Key key = Sdl2KeyMap.GetKey(scancode);
if (key != Key.Unknown)
{ {
state.SetKeyState(key, (byte)scancode, pressed); state.SetKeyState(key, (byte)scancode, pressed);
keyboards[0].SetKey(key, (byte)scancode, pressed); keyboards[0].SetKey(key, (byte)scancode, pressed);

View file

@ -75,8 +75,6 @@ namespace OpenTK.Platform.SDL2
static readonly Dictionary<uint, Sdl2NativeWindow> windows = static readonly Dictionary<uint, Sdl2NativeWindow> windows =
new Dictionary<uint, Sdl2NativeWindow>(); new Dictionary<uint, Sdl2NativeWindow>();
static readonly Sdl2KeyMap map = new Sdl2KeyMap();
public Sdl2NativeWindow(int x, int y, int width, int height, public Sdl2NativeWindow(int x, int y, int width, int height,
string title, GameWindowFlags options, DisplayDevice device, string title, GameWindowFlags options, DisplayDevice device,
IInputDriver input_driver) IInputDriver input_driver)
@ -132,12 +130,7 @@ namespace OpenTK.Platform.SDL2
static Key TranslateKey(Scancode scan) static Key TranslateKey(Scancode scan)
{ {
Key result = Key.Unknown; return Sdl2KeyMap.GetKey(scan);
if (map.ContainsKey(scan))
{
result = map[scan];
}
return result;
} }
static Key TranslateKey(Keycode key) static Key TranslateKey(Keycode key)