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
- Verify the key string matches a Vanara User32.VK enum name (e.g. 'VK_SPACE', 'A', 'VK_F1').
- Check the Vanara.PInvoke User32.VK enumeration documentation for valid member names.
- 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
- Always prefix key names with 'VK_' or use single uppercase letter for letter keys.
- Reference the Vanara User32.VK enum for exact member names.
- Build a whitelist of keys your script uses and validate against it.
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
- ReadImageMatWithResizeSync: 不支持的插值算法 {interpolation}
- AutoSkip的配置参数需要为AutoSkipConfig类型
- retryInterval 必须大于 0
- 候选文本不能包含空字符串或纯空白字符串
- {paramName} 必须是集合或 JS Array
AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13).
Data as JSON: /api/errors/2cf1cb7e4ae2d80a.
Report an issue: GitHub.