Created
February 2, 2026 10:46
-
-
Save Arlorean/91e3deadb507ae3401352ec6ad3092d5 to your computer and use it in GitHub Desktop.
Unity legacy Input system mapping to new Input system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #if ENABLE_INPUT_SYSTEM | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Text; | |
| using UnityEngine; | |
| using UnityEngine.InputSystem; | |
| using UnityEngine.InputSystem.Controls; | |
| public static class Input { | |
| public static Vector3 mousePosition => Mouse.current.position.ReadValue(); | |
| public static Vector2 mouseScrollDelta => Mouse.current.scroll.ReadValue(); | |
| public static bool GetMouseButton(int button) => GetMouseButtonControl(button)?.isPressed ?? false; | |
| public static bool GetMouseButtonDown(int button) => GetMouseButtonControl(button)?.wasPressedThisFrame ?? false; | |
| public static bool GetMouseButtonUp(int button) => GetMouseButtonControl(button)?.wasReleasedThisFrame ?? false; | |
| public static bool anyKey => Keyboard.current.anyKey.isPressed; | |
| public static bool anyKeyDown => Keyboard.current.anyKey.wasPressedThisFrame; | |
| public static bool anyKeyUp => Keyboard.current.anyKey.wasReleasedThisFrame; | |
| public static bool GetKey(KeyCode key) => GetKeyControl(key)?.isPressed ?? false; | |
| public static bool GetKeyDown(KeyCode key) => GetKeyControl(key)?.wasPressedThisFrame ?? false; | |
| public static bool GetKeyUp(KeyCode key) => GetKeyControl(key)?.wasReleasedThisFrame ?? false; | |
| public static IReadOnlyCollection<KeyCode> allKeys => keyCodeToKey.Keys; | |
| static Dictionary<KeyCode, Key> keyCodeToKey = new() { | |
| { KeyCode.Backspace, Key.Backspace }, | |
| { KeyCode.Delete, Key.Delete }, | |
| { KeyCode.Tab, Key.Tab }, | |
| { KeyCode.Return, Key.Enter }, | |
| { KeyCode.Pause, Key.Pause }, | |
| { KeyCode.Escape, Key.Escape }, | |
| { KeyCode.Space, Key.Space }, | |
| { KeyCode.Keypad0, Key.Numpad0 }, | |
| { KeyCode.Keypad1, Key.Numpad1 }, | |
| { KeyCode.Keypad2, Key.Numpad2 }, | |
| { KeyCode.Keypad3, Key.Numpad3 }, | |
| { KeyCode.Keypad4, Key.Numpad4 }, | |
| { KeyCode.Keypad5, Key.Numpad5 }, | |
| { KeyCode.Keypad6, Key.Numpad6 }, | |
| { KeyCode.Keypad7, Key.Numpad7 }, | |
| { KeyCode.Keypad8, Key.Numpad8 }, | |
| { KeyCode.Keypad9, Key.Numpad9 }, | |
| { KeyCode.KeypadPeriod, Key.NumpadPeriod }, | |
| { KeyCode.KeypadDivide, Key.NumpadDivide }, | |
| { KeyCode.KeypadMultiply, Key.NumpadMultiply }, | |
| { KeyCode.KeypadMinus, Key.NumpadMinus }, | |
| { KeyCode.KeypadPlus, Key.NumpadPlus }, | |
| { KeyCode.KeypadEnter, Key.NumpadEnter }, | |
| { KeyCode.KeypadEquals, Key.NumpadEquals }, | |
| { KeyCode.UpArrow, Key.UpArrow }, | |
| { KeyCode.DownArrow, Key.DownArrow }, | |
| { KeyCode.RightArrow, Key.RightArrow }, | |
| { KeyCode.LeftArrow, Key.LeftArrow }, | |
| { KeyCode.Insert, Key.Insert }, | |
| { KeyCode.Home, Key.Home }, | |
| { KeyCode.End, Key.End }, | |
| { KeyCode.PageUp, Key.PageUp }, | |
| { KeyCode.PageDown, Key.PageDown }, | |
| { KeyCode.F1, Key.F1 }, | |
| { KeyCode.F2, Key.F2 }, | |
| { KeyCode.F3, Key.F3 }, | |
| { KeyCode.F4, Key.F4 }, | |
| { KeyCode.F5, Key.F5 }, | |
| { KeyCode.F6, Key.F6 }, | |
| { KeyCode.F7, Key.F7 }, | |
| { KeyCode.F8, Key.F8 }, | |
| { KeyCode.F9, Key.F9 }, | |
| { KeyCode.F10, Key.F10 }, | |
| { KeyCode.F11, Key.F11 }, | |
| { KeyCode.F12, Key.F12 }, | |
| { KeyCode.Alpha0, Key.Digit0 }, | |
| { KeyCode.Alpha1, Key.Digit1 }, | |
| { KeyCode.Alpha2, Key.Digit2 }, | |
| { KeyCode.Alpha3, Key.Digit3 }, | |
| { KeyCode.Alpha4, Key.Digit4 }, | |
| { KeyCode.Alpha5, Key.Digit5 }, | |
| { KeyCode.Alpha6, Key.Digit6 }, | |
| { KeyCode.Alpha7, Key.Digit7 }, | |
| { KeyCode.Alpha8, Key.Digit8 }, | |
| { KeyCode.Alpha9, Key.Digit9 }, | |
| { KeyCode.A, Key.A }, | |
| { KeyCode.B, Key.B }, | |
| { KeyCode.C, Key.C }, | |
| { KeyCode.D, Key.D }, | |
| { KeyCode.E, Key.E }, | |
| { KeyCode.F, Key.F }, | |
| { KeyCode.G, Key.G }, | |
| { KeyCode.H, Key.H }, | |
| { KeyCode.I, Key.I }, | |
| { KeyCode.J, Key.J }, | |
| { KeyCode.K, Key.K }, | |
| { KeyCode.L, Key.L }, | |
| { KeyCode.M, Key.M }, | |
| { KeyCode.N, Key.N }, | |
| { KeyCode.O, Key.O }, | |
| { KeyCode.P, Key.P }, | |
| { KeyCode.Q, Key.Q }, | |
| { KeyCode.R, Key.R }, | |
| { KeyCode.S, Key.S }, | |
| { KeyCode.T, Key.T }, | |
| { KeyCode.U, Key.U }, | |
| { KeyCode.V, Key.V }, | |
| { KeyCode.W, Key.W }, | |
| { KeyCode.X, Key.X }, | |
| { KeyCode.Y, Key.Y }, | |
| { KeyCode.Z, Key.Z }, | |
| { KeyCode.Numlock, Key.NumLock }, | |
| { KeyCode.CapsLock, Key.CapsLock }, | |
| { KeyCode.ScrollLock, Key.ScrollLock }, | |
| { KeyCode.RightShift, Key.RightShift }, | |
| { KeyCode.LeftShift, Key.LeftShift }, | |
| { KeyCode.RightControl, Key.RightCtrl }, | |
| { KeyCode.LeftControl, Key.LeftCtrl }, | |
| { KeyCode.RightAlt, Key.RightAlt }, | |
| { KeyCode.LeftAlt, Key.LeftAlt }, | |
| { KeyCode.LeftCommand, Key.LeftCommand }, | |
| { KeyCode.LeftWindows, Key.LeftWindows }, | |
| { KeyCode.RightCommand, Key.RightCommand }, | |
| { KeyCode.RightWindows, Key.RightWindows }, | |
| { KeyCode.Menu, Key.ContextMenu }, | |
| { KeyCode.Print, Key.PrintScreen }, | |
| { KeyCode.LeftBracket, Key.LeftBracket }, | |
| { KeyCode.RightBracket, Key.RightBracket }, | |
| { KeyCode.Semicolon, Key.Semicolon }, | |
| { KeyCode.Quote, Key.Quote }, | |
| { KeyCode.Comma, Key.Comma }, | |
| { KeyCode.Period, Key.Period }, | |
| { KeyCode.Slash, Key.Slash }, | |
| { KeyCode.Backslash, Key.Backslash }, | |
| { KeyCode.Minus, Key.Minus }, | |
| { KeyCode.Equals, Key.Equals }, | |
| { KeyCode.BackQuote, Key.Backquote }, | |
| }; | |
| public static string inputString => InputString.inputString; | |
| static KeyControl GetKeyControl(KeyCode keyCode) { | |
| if (keyCodeToKey.TryGetValue(keyCode, out var key)) { | |
| return Keyboard.current[key]; | |
| } | |
| else { | |
| Debug.LogError($"KeyCode '{keyCode}' does not have a corresponding KeyControl"); | |
| return null; | |
| } | |
| } | |
| static ButtonControl GetMouseButtonControl(int button) => button switch { | |
| 0 => Mouse.current.leftButton, | |
| 1 => Mouse.current.rightButton, | |
| 2 => Mouse.current.middleButton, | |
| _ => null | |
| }; | |
| } | |
| public static class InputString { | |
| static readonly StringBuilder typedThisFrame = new(); | |
| static int lastAppendFrame = -1; | |
| static bool hooked; | |
| static Keyboard subscribedKeyboard; | |
| // Keep a stable delegate instance so we can remove it later if needed | |
| static readonly Action<char> onTextInput = OnTextInput; | |
| static void EnsureHooked() { | |
| if (!hooked) { | |
| hooked = true; | |
| SubscribeToKeyboard(Keyboard.current); | |
| InputSystem.onDeviceChange += (device, change) => { | |
| if (device is Keyboard kb) { | |
| // Keyboard instances can change; resubscribe to the latest one. | |
| if (change == InputDeviceChange.Added || change == InputDeviceChange.Reconnected) { | |
| SubscribeToKeyboard(kb); | |
| } | |
| if (change == InputDeviceChange.Removed || change == InputDeviceChange.Disconnected) { | |
| UnsubscribeFromKeyboard(kb); | |
| } | |
| } | |
| }; | |
| } | |
| } | |
| static void SubscribeToKeyboard(Keyboard kb) { | |
| if (kb != null) { | |
| if (subscribedKeyboard != kb) { | |
| if (subscribedKeyboard != null) { | |
| subscribedKeyboard.onTextInput -= onTextInput; | |
| } | |
| subscribedKeyboard = kb; | |
| subscribedKeyboard.onTextInput += onTextInput; | |
| } | |
| } | |
| } | |
| static void UnsubscribeFromKeyboard(Keyboard kb) { | |
| if (kb != null) { | |
| kb.onTextInput -= onTextInput; | |
| if (subscribedKeyboard == kb) { | |
| subscribedKeyboard = null; | |
| } | |
| } | |
| } | |
| static void EnsureBufferIsForCurrentFrame() { | |
| // If we haven't appended anything this frame, ensure buffer is empty. | |
| if (Time.frameCount != lastAppendFrame) { | |
| typedThisFrame.Clear(); | |
| } | |
| } | |
| static void AppendThisFrame(char c) { | |
| if (Time.frameCount != lastAppendFrame) { | |
| typedThisFrame.Clear(); | |
| lastAppendFrame = Time.frameCount; | |
| } | |
| typedThisFrame.Append(c); | |
| } | |
| static void OnTextInput(char c) { | |
| // Old Input.inputString: ASCII only | |
| //if (c > 127) return; | |
| // Normalize CR to LF | |
| if (c == '\r') c = '\n'; | |
| AppendThisFrame(c); | |
| } | |
| public static string inputString { | |
| get { | |
| EnsureHooked(); | |
| EnsureBufferIsForCurrentFrame(); | |
| // Fallbacks for platforms where text input doesn't emit these reliably | |
| var kb = Keyboard.current; | |
| if (kb != null) { | |
| if (kb.backspaceKey.wasPressedThisFrame) { | |
| AppendThisFrame('\b'); | |
| } | |
| if (kb.enterKey.wasPressedThisFrame || kb.numpadEnterKey.wasPressedThisFrame) { | |
| AppendThisFrame('\n'); | |
| } | |
| } | |
| return typedThisFrame.ToString(); | |
| } | |
| } | |
| } | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment