2013-09-27 21:07:23 +00:00
|
|
|
#region License
|
|
|
|
//
|
|
|
|
// The Open Toolkit Library License
|
|
|
|
//
|
|
|
|
// Copyright (c) 2006 - 2013 Stefanos Apostolopoulos for the Open Toolkit library.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
// so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
#endregion
|
|
|
|
|
2013-09-27 12:41:37 +00:00
|
|
|
using System;
|
|
|
|
using OpenTK.Input;
|
|
|
|
|
|
|
|
namespace OpenTK.Platform.SDL2
|
|
|
|
{
|
|
|
|
class Sdl2Keyboard : IKeyboardDriver2
|
|
|
|
{
|
2013-09-30 10:22:25 +00:00
|
|
|
static readonly Sdl2KeyMap KeyMap = new Sdl2KeyMap();
|
|
|
|
KeyboardState state;
|
|
|
|
|
2013-09-27 12:41:37 +00:00
|
|
|
public Sdl2Keyboard()
|
|
|
|
{
|
2013-09-30 10:22:25 +00:00
|
|
|
state.IsConnected = true;
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 12:20:33 +00:00
|
|
|
#region Private Members
|
2013-09-30 10:22:25 +00:00
|
|
|
|
2013-09-30 12:20:33 +00:00
|
|
|
// 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)
|
2013-09-30 10:22:25 +00:00
|
|
|
{
|
|
|
|
Key key;
|
2013-09-30 11:51:31 +00:00
|
|
|
bool pressed = e.state != 0;
|
|
|
|
if (KeyMap.TryGetValue(e.keysym.scancode, out key))
|
2013-09-30 10:22:25 +00:00
|
|
|
{
|
2013-09-30 11:51:31 +00:00
|
|
|
state.SetKeyState(key, (byte)e.keysym.scancode, pressed);
|
|
|
|
}
|
2013-09-30 10:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2013-09-27 12:41:37 +00:00
|
|
|
#region IKeyboardDriver2 Members
|
|
|
|
|
|
|
|
public KeyboardState GetState()
|
|
|
|
{
|
2013-09-30 12:20:33 +00:00
|
|
|
//UpdateModifiers(); // Fixme
|
2013-09-30 10:22:25 +00:00
|
|
|
return state;
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public KeyboardState GetState(int index)
|
|
|
|
{
|
2013-09-30 12:20:33 +00:00
|
|
|
//UpdateModifiers(); // Fixme
|
2013-09-30 10:22:25 +00:00
|
|
|
if (index == 0)
|
|
|
|
return state;
|
|
|
|
else
|
|
|
|
return new KeyboardState();
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public string GetDeviceName(int index)
|
|
|
|
{
|
2013-09-30 10:22:25 +00:00
|
|
|
return "SDL2 Default Keyboard";
|
2013-09-27 12:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|