{"record":{"id":"706be74ff71afe5c","repo":"dotnet/wpf","slug":"invalidenumargumentexception-key-int-key-typeof-key","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"key\", (int)key, typeof(Key))","messagePattern":"InvalidEnumArgumentException\\(\"key\", \\(int\\)key, typeof\\(Key\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/KeyGesture.cs","lineNumber":94,"sourceCode":"        internal KeyGesture(Key key, ModifierKeys modifiers, bool validateGesture)\n            : this(key, modifiers, String.Empty, validateGesture)\n        {\n        }\n\n        /// <summary>\n        /// Private constructor that does the real work.\n        /// </summary>\n        /// <param name=\"key\">Key</param>\n        /// <param name=\"modifiers\">Modifiers</param>\n        /// <param name=\"displayString\">display string</param>\n        /// <param name=\"validateGesture\">If true, throws an exception if the key and modifier are not valid</param>\n        private KeyGesture(Key key, ModifierKeys modifiers, string displayString, bool validateGesture)\n        {\n            if(!ModifierKeysConverter.IsDefinedModifierKeys(modifiers))\n                throw new InvalidEnumArgumentException(\"modifiers\", (int)modifiers, typeof(ModifierKeys));\n\n            if(!IsDefinedKey(key))\n                throw new InvalidEnumArgumentException(\"key\", (int)key, typeof(Key));\n\n            ArgumentNullException.ThrowIfNull(displayString);\n\n            if (validateGesture && !IsValid(key, modifiers))\n            {\n                throw new NotSupportedException(SR.Format(SR.KeyGesture_Invalid, modifiers, key));\n            }\n\n            _modifiers = modifiers;\n            _key = key;\n            _displayString = displayString;\n        }\n#endregion Constructors\n\n        //------------------------------------------------------\n        //\n        //  Public Methods\n        //","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/KeyGesture.cs#L76-L112","documentation":"The private KeyGesture constructor validates the Key argument with IsDefinedKey and throws InvalidEnumArgumentException for 'key' when the value is not a defined Key enum member. This rejects out-of-range or undefined key codes.","triggerScenarios":"Calling a public KeyGesture constructor with a Key cast from an arbitrary int (e.g. (Key)1000) or a special/deprecated member not accepted by IsDefinedKey.","commonSituations":"Building gestures from persisted numeric key codes in config or from interop virtual-key values without mapping them to WPF Key values.","solutions":["Pass only Key enum members accepted by IsDefinedKey (real keys, not None or internal values)","Validate with IsDefinedKey before constructing the gesture","Map interop VK codes to Key via KeyInterop.KeyFromVirtualKey and validate"],"exampleFix":"// before\nvar gesture = new KeyGesture((Key)vkCode, ModifierKeys.Control);\n// after\nvar key = KeyInterop.KeyFromVirtualKey(vkCode);\nif (IsDefinedKey(key)) // or wrap in try/catch\n{\n    var gesture = new KeyGesture(key, ModifierKeys.Control);\n}","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(Key), key) || key == Key.None) throw new ArgumentOutOfRangeException(nameof(key));","typeGuard":"static bool IsUsableKey(Key k) => k != Key.None && Enum.IsDefined(typeof(Key), k);","tryCatchPattern":"try { var g = new KeyGesture(key, modifiers); } catch (InvalidEnumArgumentException ex) { /* undefined key value */ }","preventionTips":["Map interop virtual-key codes via KeyInterop.KeyFromVirtualKey before use","Validate Key values parsed from persisted data"],"tags":["wpf","input","enum","keygesture"],"backgroundTag":"invalid-enum-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}