146 lines
5.6 KiB
C#
146 lines
5.6 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// Bidirectional mapping between legacy UnityEngine.KeyCode and Unity Input System
|
|
/// keyboard control paths (e.g. KeyCode.D <-> "<Keyboard>/d").
|
|
///
|
|
/// The gameplay binding store persists paths (Input System canonical format), while
|
|
/// most game/UI code still speaks KeyCode. This map keeps both representations in sync
|
|
/// without changing any judgement/binding business logic.
|
|
/// </summary>
|
|
public static class InputSystemKeyMap
|
|
{
|
|
// KeyCode -> Input System control name (the part after "<Keyboard>/").
|
|
private static readonly Dictionary<KeyCode, string> keyCodeToControl = new Dictionary<KeyCode, string>();
|
|
private static readonly Dictionary<string, KeyCode> controlToKeyCode = new Dictionary<string, KeyCode>();
|
|
|
|
private const string KeyboardPrefix = "<Keyboard>/";
|
|
|
|
static InputSystemKeyMap()
|
|
{
|
|
// Letters
|
|
for (char c = 'a'; c <= 'z'; c++)
|
|
{
|
|
KeyCode kc = (KeyCode)System.Enum.Parse(typeof(KeyCode), char.ToUpper(c).ToString());
|
|
Register(kc, c.ToString());
|
|
}
|
|
|
|
// Top-row digits (KeyCode.Alpha0..9 -> "1".."0")
|
|
Register(KeyCode.Alpha0, "0");
|
|
Register(KeyCode.Alpha1, "1");
|
|
Register(KeyCode.Alpha2, "2");
|
|
Register(KeyCode.Alpha3, "3");
|
|
Register(KeyCode.Alpha4, "4");
|
|
Register(KeyCode.Alpha5, "5");
|
|
Register(KeyCode.Alpha6, "6");
|
|
Register(KeyCode.Alpha7, "7");
|
|
Register(KeyCode.Alpha8, "8");
|
|
Register(KeyCode.Alpha9, "9");
|
|
|
|
// Numpad
|
|
Register(KeyCode.Keypad0, "numpad0");
|
|
Register(KeyCode.Keypad1, "numpad1");
|
|
Register(KeyCode.Keypad2, "numpad2");
|
|
Register(KeyCode.Keypad3, "numpad3");
|
|
Register(KeyCode.Keypad4, "numpad4");
|
|
Register(KeyCode.Keypad5, "numpad5");
|
|
Register(KeyCode.Keypad6, "numpad6");
|
|
Register(KeyCode.Keypad7, "numpad7");
|
|
Register(KeyCode.Keypad8, "numpad8");
|
|
Register(KeyCode.Keypad9, "numpad9");
|
|
Register(KeyCode.KeypadDivide, "numpadDivide");
|
|
Register(KeyCode.KeypadMultiply, "numpadMultiply");
|
|
Register(KeyCode.KeypadMinus, "numpadMinus");
|
|
Register(KeyCode.KeypadPlus, "numpadPlus");
|
|
Register(KeyCode.KeypadEnter, "numpadEnter");
|
|
Register(KeyCode.KeypadPeriod, "numpadPeriod");
|
|
Register(KeyCode.KeypadEquals, "numpadEquals");
|
|
|
|
// Whitespace / editing
|
|
Register(KeyCode.Space, "space");
|
|
Register(KeyCode.Return, "enter");
|
|
Register(KeyCode.Tab, "tab");
|
|
Register(KeyCode.Backspace, "backspace");
|
|
Register(KeyCode.Delete, "delete");
|
|
Register(KeyCode.Escape, "escape");
|
|
|
|
// Modifiers
|
|
Register(KeyCode.LeftShift, "leftShift");
|
|
Register(KeyCode.RightShift, "rightShift");
|
|
Register(KeyCode.LeftControl, "leftCtrl");
|
|
Register(KeyCode.RightControl, "rightCtrl");
|
|
Register(KeyCode.LeftAlt, "leftAlt");
|
|
Register(KeyCode.RightAlt, "rightAlt");
|
|
Register(KeyCode.LeftCommand, "leftMeta");
|
|
Register(KeyCode.RightCommand, "rightMeta");
|
|
Register(KeyCode.CapsLock, "capsLock");
|
|
|
|
// Arrows
|
|
Register(KeyCode.UpArrow, "upArrow");
|
|
Register(KeyCode.DownArrow, "downArrow");
|
|
Register(KeyCode.LeftArrow, "leftArrow");
|
|
Register(KeyCode.RightArrow, "rightArrow");
|
|
|
|
// Navigation
|
|
Register(KeyCode.Home, "home");
|
|
Register(KeyCode.End, "end");
|
|
Register(KeyCode.PageUp, "pageUp");
|
|
Register(KeyCode.PageDown, "pageDown");
|
|
Register(KeyCode.Insert, "insert");
|
|
|
|
// Punctuation / symbols
|
|
Register(KeyCode.Quote, "quote");
|
|
Register(KeyCode.Semicolon, "semicolon");
|
|
Register(KeyCode.Comma, "comma");
|
|
Register(KeyCode.Period, "period");
|
|
Register(KeyCode.Slash, "slash");
|
|
Register(KeyCode.Backslash, "backslash");
|
|
Register(KeyCode.LeftBracket, "leftBracket");
|
|
Register(KeyCode.RightBracket, "rightBracket");
|
|
Register(KeyCode.Minus, "minus");
|
|
Register(KeyCode.Equals, "equals");
|
|
Register(KeyCode.BackQuote, "backquote");
|
|
|
|
// Function keys
|
|
Register(KeyCode.F1, "f1");
|
|
Register(KeyCode.F2, "f2");
|
|
Register(KeyCode.F3, "f3");
|
|
Register(KeyCode.F4, "f4");
|
|
Register(KeyCode.F5, "f5");
|
|
Register(KeyCode.F6, "f6");
|
|
Register(KeyCode.F7, "f7");
|
|
Register(KeyCode.F8, "f8");
|
|
Register(KeyCode.F9, "f9");
|
|
Register(KeyCode.F10, "f10");
|
|
Register(KeyCode.F11, "f11");
|
|
Register(KeyCode.F12, "f12");
|
|
}
|
|
|
|
private static void Register(KeyCode kc, string control)
|
|
{
|
|
keyCodeToControl[kc] = control;
|
|
if (!controlToKeyCode.ContainsKey(control))
|
|
controlToKeyCode[control] = kc;
|
|
}
|
|
|
|
/// <summary>KeyCode -> full Input System path (e.g. "<Keyboard>/d"). Null if unmapped/None.</summary>
|
|
public static string KeyCodeToPath(KeyCode key)
|
|
{
|
|
if (key == KeyCode.None) return null;
|
|
if (keyCodeToControl.TryGetValue(key, out string control))
|
|
return KeyboardPrefix + control;
|
|
return null;
|
|
}
|
|
|
|
/// <summary>Full Input System path -> KeyCode. Returns KeyCode.None if unmapped.</summary>
|
|
public static KeyCode PathToKeyCode(string path)
|
|
{
|
|
if (string.IsNullOrEmpty(path)) return KeyCode.None;
|
|
string control = path.StartsWith(KeyboardPrefix) ? path.Substring(KeyboardPrefix.Length) : path;
|
|
if (controlToKeyCode.TryGetValue(control, out KeyCode kc))
|
|
return kc;
|
|
return KeyCode.None;
|
|
}
|
|
}
|