From 5965f7199a8ad323fbdba9c18cc80dbba544cce6 Mon Sep 17 00:00:00 2001 From: MelekTaus Moribundus Date: Tue, 30 Jun 2026 19:53:26 +0200 Subject: [PATCH] Suppress global hotkeys while a text field is focused The save-dialog name field is a focused TMP_InputField, but global discrete hotkeys kept reading the keyboard every frame, so typing a name like "Checkers" cycled the pose (C) and shut the server down (K). Add a shared TypingGuard.IsTyping check (focused TMP/legacy InputField) and short-circuit the global handlers on it: pose cycle, network dev keys, mode switch, and paint undo/redo. Movement was already gated out via ControlEnabled. Co-Authored-By: Claude Opus 4.8 --- Assets/Scripts/GameModeManager.cs | 1 + Assets/Scripts/MeshPainter.Undo.cs | 1 + Assets/Scripts/NetworkBootstrap.cs | 1 + Assets/Scripts/PlayerPoseController.cs | 1 + Assets/Scripts/TypingGuard.cs | 30 ++++++++++++++++++++++++++ Assets/Scripts/TypingGuard.cs.meta | 2 ++ 6 files changed, 36 insertions(+) create mode 100644 Assets/Scripts/TypingGuard.cs create mode 100644 Assets/Scripts/TypingGuard.cs.meta diff --git a/Assets/Scripts/GameModeManager.cs b/Assets/Scripts/GameModeManager.cs index 21f3113..269d228 100644 --- a/Assets/Scripts/GameModeManager.cs +++ b/Assets/Scripts/GameModeManager.cs @@ -347,6 +347,7 @@ public class GameModeManager : MonoBehaviour private bool WasSwitchPressed() { + if (TypingGuard.IsTyping) return false; // don't switch modes while typing in a text field #if ENABLE_INPUT_SYSTEM return Keyboard.current != null && Keyboard.current[switchKey].wasPressedThisFrame; #else diff --git a/Assets/Scripts/MeshPainter.Undo.cs b/Assets/Scripts/MeshPainter.Undo.cs index 1d5df5a..1a04d6b 100644 --- a/Assets/Scripts/MeshPainter.Undo.cs +++ b/Assets/Scripts/MeshPainter.Undo.cs @@ -241,6 +241,7 @@ public partial class MeshPainter { undo = false; redo = false; + if (TypingGuard.IsTyping) return; // let Ctrl+Z/Y edit text instead of the painting #if ENABLE_INPUT_SYSTEM var kb = Keyboard.current; if (kb != null) diff --git a/Assets/Scripts/NetworkBootstrap.cs b/Assets/Scripts/NetworkBootstrap.cs index 6613f58..a894b5f 100644 --- a/Assets/Scripts/NetworkBootstrap.cs +++ b/Assets/Scripts/NetworkBootstrap.cs @@ -38,6 +38,7 @@ public class NetworkBootstrap : MonoBehaviour private void Update() { if (!enableHotkeys) return; + if (TypingGuard.IsTyping) return; // don't fire H/C/J/K while typing in a text field #if ENABLE_INPUT_SYSTEM var kb = Keyboard.current; if (kb == null || _net == null) return; diff --git a/Assets/Scripts/PlayerPoseController.cs b/Assets/Scripts/PlayerPoseController.cs index 4f1592a..77772cf 100644 --- a/Assets/Scripts/PlayerPoseController.cs +++ b/Assets/Scripts/PlayerPoseController.cs @@ -341,6 +341,7 @@ public class PlayerPoseController : MonoBehaviour private bool CycleKeyPressed() { + if (TypingGuard.IsTyping) return false; // don't cycle pose while typing in a text field #if ENABLE_INPUT_SYSTEM var keyboard = Keyboard.current; return keyboard != null && keyboard[cycleKey].wasPressedThisFrame; diff --git a/Assets/Scripts/TypingGuard.cs b/Assets/Scripts/TypingGuard.cs new file mode 100644 index 0000000..e8c163e --- /dev/null +++ b/Assets/Scripts/TypingGuard.cs @@ -0,0 +1,30 @@ +using UnityEngine.EventSystems; +using TMPro; + +/// +/// True while the user is editing a focused on-screen text field (e.g. the paint HUD's +/// "save as" name input). Global gameplay/dev hotkeys consult this so typing a name like +/// "Checkers" doesn't also fire single-key shortcuts (C = cycle pose, K = shutdown, +/// the mode-switch key, Ctrl+Z = undo, ...). +/// +public static class TypingGuard +{ + /// A text input field is currently focused and capturing keystrokes. + public static bool IsTyping + { + get + { + var es = EventSystem.current; + if (!es) return false; + + var go = es.currentSelectedGameObject; + if (!go) return false; + + var tmp = go.GetComponent(); + if (tmp) return tmp.isFocused; + + var legacy = go.GetComponent(); + return legacy && legacy.isFocused; + } + } +} diff --git a/Assets/Scripts/TypingGuard.cs.meta b/Assets/Scripts/TypingGuard.cs.meta new file mode 100644 index 0000000..57a99c9 --- /dev/null +++ b/Assets/Scripts/TypingGuard.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: c30f56f99c2725a4e861c19a5e83e0aa \ No newline at end of file