diff --git a/src/SDL2/SDL2_FNAPlatform.cs b/src/SDL2/SDL2_FNAPlatform.cs index 7374886..24e30d3 100644 --- a/src/SDL2/SDL2_FNAPlatform.cs +++ b/src/SDL2/SDL2_FNAPlatform.cs @@ -7,23 +7,6 @@ */ #endregion -#region USE_SCANCODES Option -// #define USE_SCANCODES -/* XNA Keys are based on keycodes, rather than scancodes. - * - * With SDL2 you can actually pick between SDL_Keycode and SDL_Scancode, but - * scancodes will not be accurate to XNA4. The benefit is that scancodes will - * essentially ignore "foreign" keyboard layouts, making default keyboard - * layouts work out of the box everywhere (unless the actual symbol for the keys - * matters in your game). - * - * At the same time, the TextInputEXT extension will still read the actual chars - * correctly, so you can (mostly) have your cake and eat it too if you don't - * care about your bindings menu not making a lot of sense on foreign layouts. - * -flibit - */ -#endregion - #region Using Statements using System; using System.IO; @@ -157,6 +140,15 @@ namespace Microsoft.Xna.Framework osxUseSpaces = false; } + // Do we want to read keycodes or scancodes? + SDL2_KeyboardUtil.UseScancodes = Environment.GetEnvironmentVariable( + "FNA_KEYBOARD_USE_SCANCODES" + ) == "1"; + if (SDL2_KeyboardUtil.UseScancodes) + { + Log("Using scancodes instead of keycodes!"); + } + // Active Key List List keys = new List(); @@ -176,11 +168,7 @@ namespace Microsoft.Xna.Framework // Keyboard if (evt.type == SDL.SDL_EventType.SDL_KEYDOWN) { -#if USE_SCANCODES - Keys key = SDL2_KeyboardUtil.ToXNA(evt.key.keysym.scancode); -#else - Keys key = SDL2_KeyboardUtil.ToXNA(evt.key.keysym.sym); -#endif + Keys key = SDL2_KeyboardUtil.ToXNA(ref evt.key.keysym); if (!keys.Contains(key)) { keys.Add(key); @@ -213,11 +201,7 @@ namespace Microsoft.Xna.Framework } else if (evt.type == SDL.SDL_EventType.SDL_KEYUP) { -#if USE_SCANCODES - Keys key = SDL2_KeyboardUtil.ToXNA(evt.key.keysym.scancode); -#else - Keys key = SDL2_KeyboardUtil.ToXNA(evt.key.keysym.sym); -#endif + Keys key = SDL2_KeyboardUtil.ToXNA(ref evt.key.keysym); if (keys.Remove(key)) { if (key == Keys.Back) diff --git a/src/SDL2/SDL2_KeyboardUtil.cs b/src/SDL2/SDL2_KeyboardUtil.cs index 555bdaf..b0950b8 100644 --- a/src/SDL2/SDL2_KeyboardUtil.cs +++ b/src/SDL2/SDL2_KeyboardUtil.cs @@ -17,6 +17,12 @@ namespace Microsoft.Xna.Framework.Input { internal static class SDL2_KeyboardUtil { + #region Public Static Key Map Override + + public static bool UseScancodes = false; + + #endregion + #region Private SDL2->XNA Key Hashmaps /* From: http://blogs.msdn.com/b/shawnhar/archive/2007/07/02/twin-paths-to-garbage-collector-nirvana.aspx @@ -409,32 +415,29 @@ namespace Microsoft.Xna.Framework.Input #region Public SDL2<->XNA Key Conversion Methods - public static Keys ToXNA(SDL.SDL_Keycode key) + public static Keys ToXNA(ref SDL.SDL_Keysym key) { Keys retVal; - if (INTERNAL_keyMap.TryGetValue((int) key, out retVal)) + if (UseScancodes) { - return retVal; + if (INTERNAL_scanMap.TryGetValue((int) key.scancode, out retVal)) + { + return retVal; + } } else { - SDL2_FNAPlatform.Log("KEY MISSING FROM SDL2->XNA DICTIONARY: " + key.ToString()); - return Keys.None; - } - } - - public static Keys ToXNA(SDL.SDL_Scancode key) - { - Keys retVal; - if (INTERNAL_scanMap.TryGetValue((int) key, out retVal)) - { - return retVal; - } - else - { - SDL2_FNAPlatform.Log("SCANCODE MISSING FROM SDL2->XNA DICTIONARY: " + key.ToString()); - return Keys.None; + if (INTERNAL_keyMap.TryGetValue((int) key.sym, out retVal)) + { + return retVal; + } } + SDL2_FNAPlatform.Log( + "KEY/SCANCODE MISSING FROM SDL2->XNA DICTIONARY: " + + key.sym.ToString() + " " + + key.scancode.ToString() + ); + return Keys.None; } public static Keys KeyFromScancode(Keys scancode)