babalae/better-genshin-impact · error · ArgumentException

键盘编码必须是VirtualKeyCodes枚举中的值,当前传入的 {key} 不合法

Error message

键盘编码必须是VirtualKeyCodes枚举中的值,当前传入的 {key} 不合法

What it means

Thrown by PostMessage.ToVk when User32Helper.ToVk fails to parse the key string into a Vanara User32.VK enum value. User32Helper.ToVk uppercases the key, prepends 'VK_' if missing, then calls Enum.Parse — any unrecognized name causes an exception that is re-thrown as this ArgumentException.

Source

Thrown at BetterGenshinImpact/Core/Script/Dependence/Simulator/PostMessage.cs:41

    public void KeyPress(string key)
    {
        _postMessageSimulator.KeyPressBackground(ToVk(key));
    }

    public void Click()
    {
        _postMessageSimulator.LeftButtonClick();
    }

    private static User32.VK ToVk(string key)
    {
        try
        {
            return User32Helper.ToVk(key);
        }
        catch
        {
            throw new ArgumentException($"键盘编码必须是VirtualKeyCodes枚举中的值,当前传入的 {key} 不合法");
        }
    }
}

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Verify the key string matches a Vanara User32.VK enum name (e.g. 'VK_SPACE', 'A', 'VK_F1').
  2. Check the Vanara.PInvoke User32.VK enumeration documentation for valid member names.
  3. Add a pre-validation step in your script that checks the key against a known list before calling KeyDown/KeyUp/KeyPress.

Example fix

// before
postMessage.keyDown('spacebar');
// after
postMessage.keyDown('VK_SPACE');
Defensive patterns

Strategy: validation

Validate before calling

static readonly HashSet<string> ValidKeys = new() { "VK_RETURN", "VK_SPACE", "VK_ESCAPE", /* ... */ };
string normalizedKey = key.ToUpper();
if (!normalizedKey.StartsWith("VK_")) normalizedKey = "VK_" + normalizedKey;
if (!Enum.TryParse<User32.VK>(normalizedKey, out _))
{
    throw new ArgumentException($"Invalid key: {key}");
}

Type guard

static bool IsValidKey(string key)
{
    var normalized = key.ToUpper();
    if (!normalized.StartsWith("VK_")) normalized = "VK_" + normalized;
    return Enum.TryParse(typeof(User32.VK), normalized, out _);
}

Try / catch

try
{
    postMessage.KeyPress("VK_SPACE");
}
catch (ArgumentException ex)
{
    // Log invalid key and skip or use default
    TaskControl.Logger.LogError("Invalid key pressed: {Message}", ex.Message);
}

Prevention

When it happens

Trigger: Calling PostMessage.KeyDown(key), KeyUp(key), or KeyPress(key) with a string that does not correspond to a valid User32.VK enum member (e.g. 'XYZ', 'F99', or a misspelled key name). Valid examples: 'A', 'VK_SPACE', 'F1', 'RETURN' (must match Vanara's User32.VK enum names).

Common situations: A script author passes a custom or localized key name that isn't in the Vanara VK enum. Typographical errors in key strings. Using lowercase-only or wrong casing (though ToVk uppercases, the enum name must still match).

Related errors


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