babalae/better-genshin-impact · error · ArgumentOutOfRangeException

键盘按键请使用ToInputKey方法

Error message

键盘按键请使用ToInputKey方法

What it means

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.

Source

Thrown at BetterGenshinImpact/Core/Config/KeyBindingsConfig.cs:497

        }
    }

    /// <summary>
    /// 将KeyId转换为MouseButton
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    /// <exception cref="ArgumentOutOfRangeException"></exception>
    public static MouseButton ToMouseButton(this KeyId value)
    {
        return value switch
        {
            KeyId.MouseLeftButton => MouseButton.Left,
            KeyId.MouseRightButton => MouseButton.Right,
            KeyId.MouseMiddleButton => MouseButton.Middle,
            KeyId.MouseSideButton1 => MouseButton.XButton1,
            KeyId.MouseSideButton2 => MouseButton.XButton2,
            _ => throw new ArgumentOutOfRangeException(nameof(value), "键盘按键请使用ToInputKey方法"),
        };
    }

    /// <summary>
    /// [实验] 将KeyId转换为WinForm中的Keys(用于兼容按键连发功能)
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static Keys ToWinFormKeys(this KeyId value)
    {
        try
        {
            return Enum.Parse<Keys>(value.ToInputKey().ToString());
        }
        catch
        {
            return default;
        }

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Branch on whether KeyId is a mouse button before calling ToMouseButton.
  2. Use ToInputKey for keyboard keys.
  3. Validate the binding category in config so the wrong type never reaches ToMouseButton.

Example fix

// before
var mb = binding.KeyId.ToMouseButton();

// after
if (binding.KeyId is KeyId.MouseLeftButton or KeyId.MouseRightButton
    or KeyId.MouseMiddleButton or KeyId.MouseSideButton1 or KeyId.MouseSideButton2)
{
    var mb = binding.KeyId.ToMouseButton();
}
else
{
    var k = binding.KeyId.ToInputKey();
}
Defensive patterns

Strategy: validation

Validate before calling

static readonly HashSet<KeyId> MouseKeys = new()
{
    KeyId.MouseLeftButton, KeyId.MouseRightButton, KeyId.MouseMiddleButton,
    KeyId.MouseSideButton1, KeyId.MouseSideButton2
};
if (MouseKeys.Contains(binding.KeyId))
    var mb = binding.KeyId.ToMouseButton();
else
    var k = binding.KeyId.ToInputKey();

Type guard

static bool IsMouseButton(KeyId k) => k is KeyId.MouseLeftButton or KeyId.MouseRightButton or KeyId.MouseMiddleButton or KeyId.MouseSideButton1 or KeyId.MouseSideButton2;

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/9acbd6bce78f643a. Report an issue: GitHub.