Attempted to fix key modifier state

SDL2 does not report keydown and keyup events on modifiers keys until
you release them. Attempted to fix this using SDL_GetModifierState, but
this does not appear to work. Should hit upstream.
This commit is contained in:
Stefanos A. 2013-09-30 14:20:33 +02:00
parent 7fe73253a7
commit ea0c845eb0

View file

@ -40,9 +40,37 @@ namespace OpenTK.Platform.SDL2
state.IsConnected = true;
}
#region Public Members
#region Private Members
public void ProcessKeyboardEvent(SDL.SDL_KeyboardEvent e)
// Unfortunately, SDL does not report KeyDown events
// when a modifier (e.g. shift, alt, etc) is first pressed.
// It reports a keydown+keyup event pair when the modifier
// is *released* - which means that we cannot use modifiers
// for regular input (e.g. press control to fire a weapon.)
// For that reason, we should also poll the keyboard directly
// as necessary.
// Fixme: this does not appear to work as expected.
void UpdateModifiers()
{
SDL.SDL_Keymod mod = SDL.SDL_GetModState();
state.SetKeyState(Key.LAlt, (byte)SDL.SDL_Scancode.SDL_SCANCODE_LALT, (mod & SDL.SDL_Keymod.KMOD_LALT) != 0);
state.SetKeyState(Key.RAlt, (byte)SDL.SDL_Scancode.SDL_SCANCODE_RALT, (mod & SDL.SDL_Keymod.KMOD_RALT) != 0);
state.SetKeyState(Key.LControl, (byte)SDL.SDL_Scancode.SDL_SCANCODE_LCTRL, (mod & SDL.SDL_Keymod.KMOD_LCTRL) != 0);
state.SetKeyState(Key.RControl, (byte)SDL.SDL_Scancode.SDL_SCANCODE_RCTRL, (mod & SDL.SDL_Keymod.KMOD_RCTRL) != 0);
state.SetKeyState(Key.LShift, (byte)SDL.SDL_Scancode.SDL_SCANCODE_LSHIFT, (mod & SDL.SDL_Keymod.KMOD_LSHIFT) != 0);
state.SetKeyState(Key.RShift, (byte)SDL.SDL_Scancode.SDL_SCANCODE_RSHIFT, (mod & SDL.SDL_Keymod.KMOD_RSHIFT) != 0);
state.SetKeyState(Key.Menu, (byte)SDL.SDL_Scancode.SDL_SCANCODE_APPLICATION, (mod & SDL.SDL_Keymod.KMOD_GUI) != 0);
state.SetKeyState(Key.CapsLock, (byte)SDL.SDL_Scancode.SDL_SCANCODE_CAPSLOCK, (mod & SDL.SDL_Keymod.KMOD_CAPS) != 0);
state.SetKeyState(Key.NumLock, (byte)SDL.SDL_Scancode.SDL_SCANCODE_NUMLOCKCLEAR, (mod & SDL.SDL_Keymod.KMOD_NUM) != 0);
//state.SetKeyState(Key., (byte)SDL.SDL_Scancode.SDL_SCANCODE_MODE, (mod & SDL.SDL_Keymod.KMOD_MODE) != 0);
}
#endregion
#region Internal Members
internal void ProcessKeyboardEvent(SDL.SDL_KeyboardEvent e)
{
Key key;
bool pressed = e.state != 0;
@ -50,17 +78,6 @@ namespace OpenTK.Platform.SDL2
{
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;
}
}
#endregion
@ -69,11 +86,13 @@ namespace OpenTK.Platform.SDL2
public KeyboardState GetState()
{
//UpdateModifiers(); // Fixme
return state;
}
public KeyboardState GetState(int index)
{
//UpdateModifiers(); // Fixme
if (index == 0)
return state;
else