{"record":{"id":"2cf1cb7e4ae2d80a","repo":"babalae/better-genshin-impact","slug":"virtualkeycodes-key-2cf1cb","errorCode":null,"errorMessage":"键盘编码必须是VirtualKeyCodes枚举中的值，当前传入的 {key} 不合法","messagePattern":"键盘编码必须是VirtualKeyCodes枚举中的值，当前传入的 (.+?) 不合法","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/Core/Script/Dependence/Simulator/PostMessage.cs","lineNumber":41,"sourceCode":"    public void KeyPress(string key)\n    {\n        _postMessageSimulator.KeyPressBackground(ToVk(key));\n    }\n\n    public void Click()\n    {\n        _postMessageSimulator.LeftButtonClick();\n    }\n\n    private static User32.VK ToVk(string key)\n    {\n        try\n        {\n            return User32Helper.ToVk(key);\n        }\n        catch\n        {\n            throw new ArgumentException($\"键盘编码必须是VirtualKeyCodes枚举中的值，当前传入的 {key} 不合法\");\n        }\n    }\n}\n","sourceCodeStart":23,"sourceCodeEnd":45,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Core/Script/Dependence/Simulator/PostMessage.cs#L23-L45","documentation":"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.","triggerScenarios":"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).","commonSituations":"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).","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."],"exampleFix":"// before\npostMessage.keyDown('spacebar');\n// after\npostMessage.keyDown('VK_SPACE');","handlingStrategy":"validation","validationCode":"static readonly HashSet<string> ValidKeys = new() { \"VK_RETURN\", \"VK_SPACE\", \"VK_ESCAPE\", /* ... */ };\nstring normalizedKey = key.ToUpper();\nif (!normalizedKey.StartsWith(\"VK_\")) normalizedKey = \"VK_\" + normalizedKey;\nif (!Enum.TryParse<User32.VK>(normalizedKey, out _))\n{\n    throw new ArgumentException($\"Invalid key: {key}\");\n}","typeGuard":"static bool IsValidKey(string key)\n{\n    var normalized = key.ToUpper();\n    if (!normalized.StartsWith(\"VK_\")) normalized = \"VK_\" + normalized;\n    return Enum.TryParse(typeof(User32.VK), normalized, out _);\n}","tryCatchPattern":"try\n{\n    postMessage.KeyPress(\"VK_SPACE\");\n}\ncatch (ArgumentException ex)\n{\n    // Log invalid key and skip or use default\n    TaskControl.Logger.LogError(\"Invalid key pressed: {Message}\", ex.Message);\n}","preventionTips":["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."],"tags":["input-simulation","postmessage","argument-validation","script-api"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}