mirror of
https://github.com/Ryujinx/Opentk.git
synced 2024-12-24 01:35:32 +00:00
Use scancodes for KeyboardDevice states
This resolves the issue where different layouts would return different keys when pressing the same physical key. A new API must be introduced for text input.
This commit is contained in:
parent
5e35411dd1
commit
c53773f4ec
|
@ -31,92 +31,93 @@ using OpenTK.Input;
|
|||
|
||||
namespace OpenTK.Platform.SDL2
|
||||
{
|
||||
using Code = SDL.SDL_Keycode;
|
||||
using Code = SDL.SDL_Scancode;
|
||||
|
||||
class Sdl2KeyMap : Dictionary<SDL.SDL_Keycode, Key>
|
||||
class Sdl2KeyMap : Dictionary<SDL.SDL_Scancode, Key>
|
||||
{
|
||||
public Sdl2KeyMap()
|
||||
{
|
||||
Add(Code.SDLK_ESCAPE, Key.Escape);
|
||||
Add(Code.SDL_SCANCODE_ESCAPE, Key.Escape);
|
||||
|
||||
// Function keys
|
||||
for (int i = 0; i < 12; i++)
|
||||
{
|
||||
Add(Code.SDLK_F1 + i, Key.F1 + i);
|
||||
Add(Code.SDL_SCANCODE_F1 + i, Key.F1 + i);
|
||||
}
|
||||
|
||||
// Number keys (0-9)
|
||||
for (int i = 0; i <= 9; i++)
|
||||
Add(Code.SDL_SCANCODE_0, Key.Number0);
|
||||
for (int i = 1; i < 9; i++)
|
||||
{
|
||||
Add(Code.SDLK_0 + i, Key.Number0 + i);
|
||||
Add(Code.SDL_SCANCODE_1 + i, Key.Number1 + i);
|
||||
}
|
||||
|
||||
// Letters (A-Z)
|
||||
for (int i = 0; i < 26; i++)
|
||||
{
|
||||
Add(Code.SDLK_a + i, Key.A + i);
|
||||
Add(Code.SDL_SCANCODE_A + i, Key.A + i);
|
||||
}
|
||||
|
||||
Add(Code.SDLK_TAB, Key.Tab);
|
||||
Add(Code.SDLK_CAPSLOCK, Key.CapsLock);
|
||||
Add(Code.SDLK_LCTRL, Key.ControlLeft);
|
||||
Add(Code.SDLK_LSHIFT, Key.ShiftLeft);
|
||||
Add(Code.SDLK_LALT, Key.WinLeft);
|
||||
Add(Code.SDLK_MENU, Key.AltLeft);
|
||||
Add(Code.SDLK_SPACE, Key.Space);
|
||||
Add(Code.SDLK_RALT, Key.AltRight);
|
||||
Add(Code.SDL_SCANCODE_TAB, Key.Tab);
|
||||
Add(Code.SDL_SCANCODE_CAPSLOCK, Key.CapsLock);
|
||||
Add(Code.SDL_SCANCODE_LCTRL, Key.ControlLeft);
|
||||
Add(Code.SDL_SCANCODE_LSHIFT, Key.ShiftLeft);
|
||||
Add(Code.SDL_SCANCODE_LALT, Key.WinLeft);
|
||||
Add(Code.SDL_SCANCODE_MENU, Key.AltLeft);
|
||||
Add(Code.SDL_SCANCODE_SPACE, Key.Space);
|
||||
Add(Code.SDL_SCANCODE_RALT, Key.AltRight);
|
||||
//Add(Code., Key.WinRight);
|
||||
Add(Code.SDLK_APPLICATION, Key.Menu);
|
||||
Add(Code.SDLK_RCTRL, Key.ControlRight);
|
||||
Add(Code.SDLK_RSHIFT, Key.ShiftRight);
|
||||
Add(Code.SDLK_RETURN, Key.Enter);
|
||||
Add(Code.SDLK_BACKSPACE, Key.BackSpace);
|
||||
Add(Code.SDL_SCANCODE_APPLICATION, Key.Menu);
|
||||
Add(Code.SDL_SCANCODE_RCTRL, Key.ControlRight);
|
||||
Add(Code.SDL_SCANCODE_RSHIFT, Key.ShiftRight);
|
||||
Add(Code.SDL_SCANCODE_RETURN, Key.Enter);
|
||||
Add(Code.SDL_SCANCODE_BACKSPACE, Key.BackSpace);
|
||||
|
||||
Add(Code.SDLK_SEMICOLON, Key.Semicolon); // Varies by keyboard, ;: on Win2K/US
|
||||
Add(Code.SDLK_SLASH, Key.Slash); // Varies by keyboard, /? on Win2K/US
|
||||
Add(Code.SDL_SCANCODE_SEMICOLON, Key.Semicolon); // Varies by keyboard, ;: on Win2K/US
|
||||
Add(Code.SDL_SCANCODE_SLASH, Key.Slash); // Varies by keyboard, /? on Win2K/US
|
||||
//Add(Code., Key.Tilde); // Varies by keyboard, `~ on Win2K/US
|
||||
Add(Code.SDLK_LEFTBRACKET, Key.BracketLeft); // Varies by keyboard, [{ on Win2K/US
|
||||
Add(Code.SDLK_BACKSLASH, Key.BackSlash); // Varies by keyboard, \| on Win2K/US
|
||||
Add(Code.SDLK_RIGHTBRACKET, Key.BracketRight); // Varies by keyboard, ]} on Win2K/US
|
||||
Add(Code.SDLK_QUOTE, Key.Quote); // Varies by keyboard, '" on Win2K/US
|
||||
Add(Code.SDLK_PLUS, Key.Plus); // Invariant: +
|
||||
Add(Code.SDLK_COMMA, Key.Comma); // Invariant: ,
|
||||
Add(Code.SDLK_MINUS, Key.Minus); // Invariant: -
|
||||
Add(Code.SDLK_PERIOD, Key.Period); // Invariant: .
|
||||
Add(Code.SDL_SCANCODE_LEFTBRACKET, Key.BracketLeft); // Varies by keyboard, [{ on Win2K/US
|
||||
Add(Code.SDL_SCANCODE_BACKSLASH, Key.BackSlash); // Varies by keyboard, \| on Win2K/US
|
||||
Add(Code.SDL_SCANCODE_RIGHTBRACKET, Key.BracketRight); // Varies by keyboard, ]} on Win2K/US
|
||||
Add(Code.SDL_SCANCODE_APOSTROPHE, Key.Quote); // Varies by keyboard, '" on Win2K/US
|
||||
Add(Code.SDL_SCANCODE_EQUALS, Key.Plus);
|
||||
Add(Code.SDL_SCANCODE_COMMA, Key.Comma); // Invariant: ,
|
||||
Add(Code.SDL_SCANCODE_MINUS, Key.Minus); // Invariant: -
|
||||
Add(Code.SDL_SCANCODE_PERIOD, Key.Period); // Invariant: .
|
||||
|
||||
Add(Code.SDLK_HOME, Key.Home);
|
||||
Add(Code.SDLK_END, Key.End);
|
||||
Add(Code.SDLK_DELETE, Key.Delete);
|
||||
Add(Code.SDLK_PAGEUP, Key.PageUp);
|
||||
Add(Code.SDLK_PAGEDOWN, Key.PageDown);
|
||||
Add(Code.SDLK_PAUSE, Key.Pause);
|
||||
Add(Code.SDLK_NUMLOCKCLEAR, Key.NumLock);
|
||||
Add(Code.SDL_SCANCODE_HOME, Key.Home);
|
||||
Add(Code.SDL_SCANCODE_END, Key.End);
|
||||
Add(Code.SDL_SCANCODE_DELETE, Key.Delete);
|
||||
Add(Code.SDL_SCANCODE_PAGEUP, Key.PageUp);
|
||||
Add(Code.SDL_SCANCODE_PAGEDOWN, Key.PageDown);
|
||||
Add(Code.SDL_SCANCODE_PAUSE, Key.Pause);
|
||||
Add(Code.SDL_SCANCODE_NUMLOCKCLEAR, Key.NumLock);
|
||||
|
||||
Add(Code.SDLK_SCROLLLOCK, Key.ScrollLock);
|
||||
Add(Code.SDLK_PRINTSCREEN, Key.PrintScreen);
|
||||
Add(Code.SDLK_CLEAR, Key.Clear);
|
||||
Add(Code.SDLK_INSERT, Key.Insert);
|
||||
Add(Code.SDL_SCANCODE_SCROLLLOCK, Key.ScrollLock);
|
||||
Add(Code.SDL_SCANCODE_PRINTSCREEN, Key.PrintScreen);
|
||||
Add(Code.SDL_SCANCODE_CLEAR, Key.Clear);
|
||||
Add(Code.SDL_SCANCODE_INSERT, Key.Insert);
|
||||
|
||||
Add(Code.SDLK_SLEEP, Key.Sleep);
|
||||
Add(Code.SDL_SCANCODE_SLEEP, Key.Sleep);
|
||||
|
||||
// Keypad
|
||||
for (int i = 0; i < 9; i++)
|
||||
{
|
||||
Add(Code.SDLK_KP_1 + i, Key.Keypad1 + i);
|
||||
Add(Code.SDL_SCANCODE_KP_1 + i, Key.Keypad1 + i);
|
||||
}
|
||||
Add(Code.SDLK_KP_0, Key.Keypad0); // Note: SDL2 goes KP_1..KP_9, then KP_0
|
||||
Add(Code.SDL_SCANCODE_KP_0, Key.Keypad0); // Note: SDL2 goes KP_1..KP_9, then KP_0
|
||||
|
||||
Add(Code.SDLK_KP_DECIMAL, Key.KeypadDecimal);
|
||||
Add(Code.SDLK_KP_PLUS, Key.KeypadAdd);
|
||||
Add(Code.SDLK_KP_MINUS, Key.KeypadSubtract);
|
||||
Add(Code.SDLK_KP_DIVIDE, Key.KeypadDivide);
|
||||
Add(Code.SDLK_KP_MULTIPLY, Key.KeypadMultiply);
|
||||
Add(Code.SDL_SCANCODE_KP_DECIMAL, Key.KeypadDecimal);
|
||||
Add(Code.SDL_SCANCODE_KP_PLUS, Key.KeypadAdd);
|
||||
Add(Code.SDL_SCANCODE_KP_MINUS, Key.KeypadSubtract);
|
||||
Add(Code.SDL_SCANCODE_KP_DIVIDE, Key.KeypadDivide);
|
||||
Add(Code.SDL_SCANCODE_KP_MULTIPLY, Key.KeypadMultiply);
|
||||
|
||||
// Navigation
|
||||
Add(Code.SDLK_UP, Key.Up);
|
||||
Add(Code.SDLK_DOWN, Key.Down);
|
||||
Add(Code.SDLK_LEFT, Key.Left);
|
||||
Add(Code.SDLK_RIGHT, Key.Right);
|
||||
Add(Code.SDL_SCANCODE_UP, Key.Up);
|
||||
Add(Code.SDL_SCANCODE_DOWN, Key.Down);
|
||||
Add(Code.SDL_SCANCODE_LEFT, Key.Left);
|
||||
Add(Code.SDL_SCANCODE_RIGHT, Key.Right);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,9 +45,21 @@ namespace OpenTK.Platform.SDL2
|
|||
public void ProcessKeyboardEvent(SDL.SDL_KeyboardEvent e)
|
||||
{
|
||||
Key key;
|
||||
if (KeyMap.TryGetValue(e.keysym.sym, out key))
|
||||
bool pressed = e.state != 0;
|
||||
if (KeyMap.TryGetValue(e.keysym.scancode, out key))
|
||||
{
|
||||
state.SetKeyState(key, (byte)e.keysym.scancode, e.state != 0);
|
||||
state.SetKeyState(key, (byte)e.keysym.scancode, pressed);
|
||||
}
|
||||
|
||||
switch (e.keysym.mod)
|
||||
{
|
||||
case SDL.SDL_Keymod.KMOD_ALT:
|
||||
state.SetKeyState(Key.AltLeft, (byte)e.keysym.scancode, pressed);
|
||||
break;
|
||||
|
||||
case SDL.SDL_Keymod.KMOD_CAPS:
|
||||
state.SetKeyState(Key.CapsLock, (byte)e.keysym.scancode, pressed);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -114,16 +114,22 @@ namespace OpenTK.Platform.SDL2
|
|||
}
|
||||
}
|
||||
|
||||
static Key TranslateKey(SDL.SDL_Keycode key)
|
||||
static Key TranslateKey(SDL.SDL_Scancode scan)
|
||||
{
|
||||
Key result = Key.Unknown;
|
||||
if (map.ContainsKey(key))
|
||||
if (map.ContainsKey(scan))
|
||||
{
|
||||
result = map[key];
|
||||
result = map[scan];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static Key TranslateKey(SDL.SDL_Keycode key)
|
||||
{
|
||||
SDL.SDL_Scancode scan = SDL.SDL_GetScancodeFromKey(key);
|
||||
return TranslateKey(scan);
|
||||
}
|
||||
|
||||
static int FilterEvents(IntPtr user_data, IntPtr e)
|
||||
{
|
||||
var type = (SDL.SDL_EventType)Marshal.ReadInt32(e);
|
||||
|
@ -324,7 +330,7 @@ namespace OpenTK.Platform.SDL2
|
|||
case SDL.SDL_EventType.SDL_KEYUP:
|
||||
bool key_pressed = e.key.state == SDL.SDL_PRESSED;
|
||||
var key = e.key.keysym;
|
||||
keyboard.SetKey(TranslateKey(key.sym), (uint)key.scancode, key_pressed);
|
||||
keyboard.SetKey(TranslateKey(key.scancode), (uint)key.scancode, key_pressed);
|
||||
break;
|
||||
|
||||
case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN:
|
||||
|
|
Loading…
Reference in a new issue