|
| 1 | +using UnityEngine; |
| 2 | +using UnityEngine.InputSystem; |
| 3 | +using UnityEngine.InputSystem.LowLevel; |
| 4 | + |
| 5 | +namespace BetterCartographyTable.Extensions; |
| 6 | + |
| 7 | +public static class KeyCodeExtensions |
| 8 | +{ |
| 9 | + public static string ToKeyHintString(this KeyCode keyCode) |
| 10 | + { |
| 11 | + var (isMouseButton, mouseButton) = ZInput.KeyCodeToMouseButton(keyCode, logWarning: false); |
| 12 | + if (isMouseButton) |
| 13 | + { |
| 14 | + var mouseString = mouseButton switch |
| 15 | + { |
| 16 | + MouseButton.Left => "$button_mouse0", |
| 17 | + MouseButton.Right => "$button_mouse1", |
| 18 | + MouseButton.Middle => "$button_mouse2", |
| 19 | + MouseButton.Forward => Mouse.current?.forwardButton.displayName ?? "Mouse Forward", |
| 20 | + MouseButton.Back => Mouse.current?.backButton.displayName ?? "Mouse Back", |
| 21 | + _ => null, |
| 22 | + }; |
| 23 | + if (mouseString is not null) return mouseString; |
| 24 | + } |
| 25 | + |
| 26 | + var key = ZInput.KeyCodeToKey(keyCode, logWarning: false); |
| 27 | + return key switch |
| 28 | + { |
| 29 | + Key.Comma => ",", |
| 30 | + Key.Period => ".", |
| 31 | + Key.Space => "$button_space", |
| 32 | + Key.LeftShift => "$button_lshift", |
| 33 | + Key.RightShift => "$button_rshift", |
| 34 | + Key.LeftAlt => "$button_lalt", |
| 35 | + Key.RightAlt => "$button_ralt", |
| 36 | + Key.LeftCtrl => "$button_lctrl", |
| 37 | + Key.RightCtrl => "$button_rctrl", |
| 38 | + Key.Enter => "$button_return", |
| 39 | + Key.NumpadEnter => "$button_return", |
| 40 | + Key.None => "$menu_none", |
| 41 | + _ => Keyboard.current?[key].displayName ?? key.ToString(), |
| 42 | + }; |
| 43 | + } |
| 44 | +} |
0 commit comments