babalae/better-genshin-impact · error · ArgumentException
键盘编码必须是VirtualKeyCodes枚举中的值,当前传入的 {key} 不合法
Error message
键盘编码必须是VirtualKeyCodes枚举中的值,当前传入的 {key} 不合法 What it means
Thrown by GlobalMethod.ToVk when the provided key string does not correspond to a valid User32.VK (VirtualKeyCodes) enum member. Internally, User32Helper.ToVk uppercases the key, prefixes it with "VK_" if missing, and calls Enum.Parse — so the string must match a Vanara User32.VK enum name (e.g., "A", "VK_A", "F1", "VK_F1", "SPACE", "VK_SPACE"). The catch block swallows the original exception and rethrows as a generic ArgumentException.
Source
Thrown at BetterGenshinImpact/Core/Script/Dependence/GlobalMethod.cs:145
}
else
{
Simulation.SendInput.Keyboard.KeyPress(vk);
}
break;
}
}
private static User32.VK ToVk(string key)
{
try
{
return User32Helper.ToVk(key);
}
catch
{
throw new ArgumentException($"键盘编码必须是VirtualKeyCodes枚举中的值,当前传入的 {key} 不合法");
}
}
#endregion 键盘操作
#region 鼠标操作
private static int _gameWidth = 1920;
private static int _gameHeight = 1080;
private static double _dpi = 1;
public static void SetGameMetrics(int width, int height, double dpi = 1)
{
// 必须16:9 的分辨率
if (width * 9 != height * 16)
{
throw new ArgumentException("游戏分辨率必须是16:9的分辨率");
}View on GitHub (pinned to a7cb36712d)
Solutions
- Verify the key string against the Vanara User32.VK enum — valid examples: "A"-"Z", "0"-"9", "F1"-"F24", "SPACE", "RETURN", "ESCAPE", "TAB", "SHIFT", "CONTROL".
- Add a pre-check using Enum.TryParse<User32.VK>("VK_" + key.ToUpper(), out _) before calling.
- Provide a curated whitelist of accepted key names in your script's input validation.
- Note: the original exception from Enum.Parse is swallowed — debug by temporarily calling User32Helper.ToVk directly to see the real error.
Example fix
// before
GlobalMethod.KeyPress("RETRUN"); // typo
// after
var validKeys = new HashSet<string>(Enum.GetNames<User32.VK>());
var normalizedKey = key.ToUpper().StartsWith("VK_") ? key.ToUpper() : "VK_" + key.ToUpper();
if (!validKeys.Contains(normalizedKey))
throw new ArgumentException($"Unknown key: {key}. Use VK enum names like A, SPACE, RETURN.");
GlobalMethod.KeyPress(key); Defensive patterns
Strategy: validation
Validate before calling
// Validate key against User32.VK enum before calling key methods
using Vanara.PInvoke;
var normalizedKey = key.ToUpperInvariant();
if (!normalizedKey.StartsWith("VK_"))
normalizedKey = "VK_" + normalizedKey;
if (!Enum.TryParse<User32.VK>(normalizedKey, out _))
throw new ArgumentException($"Invalid key: {key}. Must be a valid VK enum name (e.g. A, SPACE, RETURN, F1).");
GlobalMethod.KeyPress(key); Type guard
// Type guard for valid virtual key names
static bool IsValidVk(string key)
{
var normalized = key.ToUpperInvariant();
if (!normalized.StartsWith("VK_"))
normalized = "VK_" + normalized;
return Enum.TryParse<User32.VK>(normalized, out _);
} Try / catch
try
{
GlobalMethod.KeyPress(key);
}
catch (ArgumentException ex) when (ex.Message.Contains("VirtualKeyCodes"))
{
_logger.LogError("Invalid key name: {Key}. Use VK enum names like A, SPACE, RETURN, ESCAPE.", key);
} Prevention
- Maintain a curated list of commonly used VK names for script input.
- Test key strings against the Vanara User32.VK enum before use.
- Note that User32Helper.ToVk uppercases and prefixes with VK_ automatically.
- Common valid names: A-Z, 0-9, F1-F24, SPACE, RETURN, ESCAPE, TAB, SHIFT, CONTROL, ALT.
When it happens
Trigger: Calling key press methods (KeyPress, KeyDown, KeyUp) via GlobalMethod with a key string that is not a valid Windows virtual-key enum name: typos like "RETURN" (correct: "RETURN" is valid but "RETRUN" is not), lowercase-only strings that don't match after uppercasing, or completely invalid names like "KEY_A" or "ESC" vs "ESCAPE".
Common situations: Script passes a key name from user config without validation. Confusion between different key-naming conventions (e.g., "ENTER" vs "RETURN", "ESC" vs "ESCAPE"). Using HTML key event names instead of Win32 VK enum names.
Related errors
- {paramName} 必须大于 0
- 动作 {snapshot.Description} 执行 {attempts} 次后,等待 {snapshot.Targ
- milliseconds 不能小于 0
- 未找到名称为 {objectName} 的 RecognitionObject 配置
- 对象 {objectName} 缺少 template 配置
AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13).
Data as JSON: /api/errors/60713be4cab33546.
Report an issue: GitHub.