Opentk/Source/OpenTK/Platform/SDL2/Sdl2Keyboard.cs
Stefanos A d81bedf5f8 Moved all input processing to Sdl2InputDriver
This reduces the duplication of code between Sdl2NativeWindow and
Sdl2InputDriver. Sdl2InputDriver is now solely responsible for handling
input.
2013-10-04 10:02:19 +02:00

137 lines
5.1 KiB
C#

#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
using System;
using System.Collections.Generic;
using OpenTK.Input;
namespace OpenTK.Platform.SDL2
{
class Sdl2Keyboard : IKeyboardDriver2, IKeyboardDriver
{
static readonly Sdl2KeyMap KeyMap = new Sdl2KeyMap();
KeyboardState state;
readonly List<KeyboardDevice> keyboards =
new List<KeyboardDevice>();
readonly IList<KeyboardDevice> keyboards_readonly;
public Sdl2Keyboard()
{
state.IsConnected = true;
keyboards.Add(new KeyboardDevice());
keyboards[0].Description = "Standard keyboard";
keyboards[0].NumberOfFunctionKeys = 12;
keyboards[0].NumberOfKeys = 101;
keyboards[0].NumberOfLeds = 3;
keyboards_readonly = keyboards.AsReadOnly();
}
#region Private Members
// 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;
var scancode = e.keysym.scancode;
if (KeyMap.TryGetValue(scancode, out key))
{
state.SetKeyState(key, (byte)scancode, pressed);
keyboards[0].SetKey(key, (byte)scancode, pressed);
}
}
#endregion
#region IKeyboardDriver Members
public IList<KeyboardDevice> Keyboard
{
get
{
return keyboards_readonly;
}
}
#endregion
#region IKeyboardDriver2 Members
public KeyboardState GetState()
{
//UpdateModifiers(); // Fixme
return state;
}
public KeyboardState GetState(int index)
{
//UpdateModifiers(); // Fixme
if (index == 0)
return state;
else
return new KeyboardState();
}
public string GetDeviceName(int index)
{
return "SDL2 Default Keyboard";
}
#endregion
}
}