{"record":{"id":"9acbd6bce78f643a","repo":"babalae/better-genshin-impact","slug":"toinputkey","errorCode":null,"errorMessage":"键盘按键请使用ToInputKey方法","messagePattern":"键盘按键请使用ToInputKey方法","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/Core/Config/KeyBindingsConfig.cs","lineNumber":497,"sourceCode":"        }\n    }\n\n    /// <summary>\n    /// 将KeyId转换为MouseButton\n    /// </summary>\n    /// <param name=\"value\"></param>\n    /// <returns></returns>\n    /// <exception cref=\"ArgumentOutOfRangeException\"></exception>\n    public static MouseButton ToMouseButton(this KeyId value)\n    {\n        return value switch\n        {\n            KeyId.MouseLeftButton => MouseButton.Left,\n            KeyId.MouseRightButton => MouseButton.Right,\n            KeyId.MouseMiddleButton => MouseButton.Middle,\n            KeyId.MouseSideButton1 => MouseButton.XButton1,\n            KeyId.MouseSideButton2 => MouseButton.XButton2,\n            _ => throw new ArgumentOutOfRangeException(nameof(value), \"键盘按键请使用ToInputKey方法\"),\n        };\n    }\n\n    /// <summary>\n    /// [实验] 将KeyId转换为WinForm中的Keys（用于兼容按键连发功能）\n    /// </summary>\n    /// <param name=\"value\"></param>\n    /// <returns></returns>\n    public static Keys ToWinFormKeys(this KeyId value)\n    {\n        try\n        {\n            return Enum.Parse<Keys>(value.ToInputKey().ToString());\n        }\n        catch\n        {\n            return default;\n        }","sourceCodeStart":479,"sourceCodeEnd":515,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Core/Config/KeyBindingsConfig.cs#L479-L515","documentation":"KeyBindingsConfig.ToMouseButton maps only the five mouse-button KeyId values (Left, Right, Middle, Side1, Side2) to System.Windows.Input.MouseButton. Any keyboard key or KeyId.None falls through to the default arm and throws ArgumentOutOfRangeException with a message directing the caller to ToInputKey. This enforces the split between mouse and keyboard key handling.","triggerScenarios":"Calling a keyboard KeyId.ToMouseButton() (e.g. KeyId.A, KeyId.Space); calling on KeyId.None; mixing a single binding into a mouse-button context without checking the category.","commonSituations":"A binding slot that can hold either a mouse or keyboard key; config migrated so a former mouse slot now holds a keyboard key; code that assumes all bindings are mouse buttons.","solutions":["Branch on whether KeyId is a mouse button before calling ToMouseButton.","Use ToInputKey for keyboard keys.","Validate the binding category in config so the wrong type never reaches ToMouseButton."],"exampleFix":"// before\nvar mb = binding.KeyId.ToMouseButton();\n\n// after\nif (binding.KeyId is KeyId.MouseLeftButton or KeyId.MouseRightButton\n    or KeyId.MouseMiddleButton or KeyId.MouseSideButton1 or KeyId.MouseSideButton2)\n{\n    var mb = binding.KeyId.ToMouseButton();\n}\nelse\n{\n    var k = binding.KeyId.ToInputKey();\n}","handlingStrategy":"validation","validationCode":"static readonly HashSet<KeyId> MouseKeys = new()\n{\n    KeyId.MouseLeftButton, KeyId.MouseRightButton, KeyId.MouseMiddleButton,\n    KeyId.MouseSideButton1, KeyId.MouseSideButton2\n};\nif (MouseKeys.Contains(binding.KeyId))\n    var mb = binding.KeyId.ToMouseButton();\nelse\n    var k = binding.KeyId.ToInputKey();","typeGuard":"static bool IsMouseButton(KeyId k) => k is KeyId.MouseLeftButton or KeyId.MouseRightButton or KeyId.MouseMiddleButton or KeyId.MouseSideButton1 or KeyId.MouseSideButton2;","tryCatchPattern":null,"preventionTips":["Categorize bindings as mouse vs keyboard at config time.","Branch on the key category before calling the specific converter.","Avoid generic 'convert this KeyId' code paths."],"tags":["keybinding","mouse","argument-validation"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}