diff --git a/BepInEx.InputHotkeyBlock/BepInEx.InputHotkeyBlock.csproj b/BepInEx.InputHotkeyBlock/BepInEx.InputHotkeyBlock.csproj new file mode 100644 index 0000000..09942fc --- /dev/null +++ b/BepInEx.InputHotkeyBlock/BepInEx.InputHotkeyBlock.csproj @@ -0,0 +1,62 @@ + + + + + Debug + AnyCPU + {31BD63CB-75D9-4AA1-B97D-31F9DE3DDAEE} + Library + Properties + BepInEx.InputHotkeyBlock + BepInEx.InputHotkeyBlock + v3.5 + 512 + true + + + false + none + false + ..\bin\ + DEBUG;TRACE + prompt + 4 + + + none + true + ..\bin\ + TRACE + prompt + 4 + + + + ..\lib\0Harmony.dll + False + + + ..\lib\BepInEx.dll + False + + + + + + + + + ..\lib\UnityEngine.dll + False + + + ..\lib\UnityEngine.UI.dll + False + + + + + + + + \ No newline at end of file diff --git a/BepInEx.InputHotkeyBlock/InputHotkeyBlock.cs b/BepInEx.InputHotkeyBlock/InputHotkeyBlock.cs new file mode 100644 index 0000000..4b5455a --- /dev/null +++ b/BepInEx.InputHotkeyBlock/InputHotkeyBlock.cs @@ -0,0 +1,73 @@ +using HarmonyLib; +using System; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; + +namespace BepInEx +{ + /// + /// Intercepts GetKey to prevent plugin hotkeys from firing while typing in an input field + /// + [BepInPlugin(GUID, PluginName, Version)] + public class InputHotkeyBlock : BaseUnityPlugin + { + public const string GUID = "BepInEx.InputHotkeyBlock"; + public const string PluginName = "Input Hotkey Block"; + public const string Version = "1.3"; + + private static Type TMPInputFieldType; + + internal void Main() + { + //Try to get the type of the TextMeshPro InputField, if present + TMPInputFieldType = Type.GetType("TMPro.TMP_InputField, Unity.TextMeshPro"); + if (TMPInputFieldType == null) + TMPInputFieldType = Type.GetType("TMPro.TMP_InputField, TextMeshPro-1.0.55.56.0b12"); + + Harmony.CreateAndPatchAll(typeof(Hooks)); + } + + /// + /// Check if an input field is selected + /// + private static bool HotkeyBlock() + { + //UI elements from some mods + if (GUIUtility.keyboardControl > 0) + return false; + + if (EventSystem.current?.currentSelectedGameObject != null) + { + //TextMeshPro InputField + if (TMPInputFieldType != null) + if (EventSystem.current.currentSelectedGameObject.GetComponent(TMPInputFieldType) != null) + return false; + + //Other InputFields + if (EventSystem.current.currentSelectedGameObject.GetComponent() != null) + return false; + } + return true; + } + + /// + /// GetKey hooks. When HotkeyBlock returns false the GetKey functions will be prevented from running. + /// + internal static partial class Hooks + { + [HarmonyPrefix, HarmonyPatch(typeof(Input), nameof(Input.GetKey), typeof(KeyCode))] + internal static bool GetKeyCode() => HotkeyBlock(); + [HarmonyPrefix, HarmonyPatch(typeof(Input), nameof(Input.GetKey), typeof(string))] + internal static bool GetKeyString() => HotkeyBlock(); + [HarmonyPrefix, HarmonyPatch(typeof(Input), nameof(Input.GetKeyDown), typeof(KeyCode))] + internal static bool GetKeyDownCode() => HotkeyBlock(); + [HarmonyPrefix, HarmonyPatch(typeof(Input), nameof(Input.GetKeyDown), typeof(string))] + internal static bool GetKeyDownString() => HotkeyBlock(); + [HarmonyPrefix, HarmonyPatch(typeof(Input), nameof(Input.GetKeyUp), typeof(KeyCode))] + internal static bool GetKeyUpCode() => HotkeyBlock(); + [HarmonyPrefix, HarmonyPatch(typeof(Input), nameof(Input.GetKeyUp), typeof(string))] + internal static bool GetKeyUpString() => HotkeyBlock(); + } + } +} diff --git a/BepInEx.InputHotkeyBlock/Properties/AssemblyInfo.cs b/BepInEx.InputHotkeyBlock/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..f3edaf1 --- /dev/null +++ b/BepInEx.InputHotkeyBlock/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.InteropServices; +using static BepInEx.InputHotkeyBlock; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("BepInEx.InputHotkeyBlock")] +[assembly: AssemblyDescription(PluginName)] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("BepInEx.InputHotkeyBlock")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("31bd63cb-75d9-4aa1-b97d-31f9de3ddaee")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion(Version)] +[assembly: AssemblyFileVersion(Version)] \ No newline at end of file diff --git a/BepInEx.Utility.sln b/BepInEx.Utility.sln index e675e21..b387e6a 100644 --- a/BepInEx.Utility.sln +++ b/BepInEx.Utility.sln @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BepInEx.EnableResize", "Bep EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BepInEx.EnableFullScreenToggle", "BepInEx.EnableFullScreenToggle\BepInEx.EnableFullScreenToggle.csproj", "{D7B4A73A-1C1A-4C52-8FA6-3B52C2240C29}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BepInEx.InputHotkeyBlock", "BepInEx.InputHotkeyBlock\BepInEx.InputHotkeyBlock.csproj", "{31BD63CB-75D9-4AA1-B97D-31F9DE3DDAEE}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {D7B4A73A-1C1A-4C52-8FA6-3B52C2240C29}.Debug|Any CPU.Build.0 = Debug|Any CPU {D7B4A73A-1C1A-4C52-8FA6-3B52C2240C29}.Release|Any CPU.ActiveCfg = Release|Any CPU {D7B4A73A-1C1A-4C52-8FA6-3B52C2240C29}.Release|Any CPU.Build.0 = Release|Any CPU + {31BD63CB-75D9-4AA1-B97D-31F9DE3DDAEE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {31BD63CB-75D9-4AA1-B97D-31F9DE3DDAEE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {31BD63CB-75D9-4AA1-B97D-31F9DE3DDAEE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {31BD63CB-75D9-4AA1-B97D-31F9DE3DDAEE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE