{"record":{"id":"fecfc2b25a308a49","repo":"dotnet/wpf","slug":"invalidenumargumentexception-key-int-key-typeof-key-fecfc2","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/KeyEventArgs.cs","lineNumber":32,"sourceCode":"        /// </summary>\n        /// <param name=\"keyboard\">\n        ///     The logical keyboard device associated with this event.\n        /// </param>\n        /// <param name=\"inputSource\">\n        ///     The input source that provided this input.\n        /// </param>\n        /// <param name=\"timestamp\">\n        ///     The time when the input occurred.\n        /// </param>\n        /// <param name=\"key\">\n        ///     The key referenced by the event.\n        /// </param>\n        public KeyEventArgs(KeyboardDevice keyboard, PresentationSource inputSource, int timestamp, Key key) : base(keyboard, timestamp)\n        {\n            ArgumentNullException.ThrowIfNull(inputSource);\n\n            if (!Keyboard.IsValidKey(key))\n                throw new System.ComponentModel.InvalidEnumArgumentException(\"key\", (int)key, typeof(Key));\n\n            _inputSource = inputSource;\n\n            _realKey = key;\n            _isRepeat = false;\n\n            // Start out assuming that this is just a normal key.\n            MarkNormal();\n        }\n\n        /// <summary>\n        ///     The input source that provided this input.\n        /// </summary>\n        public PresentationSource InputSource\n        {\n            get \n            {\n                ","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/KeyEventArgs.cs#L14-L50","documentation":"The KeyEventArgs constructor validates that the supplied Key is a keyboard-supported key (Keyboard.IsValidKey). Passing a key outside the valid range or an undefined Key member throws InvalidEnumArgumentException naming parameter 'key'.","triggerScenarios":"Constructing new KeyEventArgs(keyboard, inputSource, timestamp, key) with a Key value not in Keyboard's valid key range (e.g. cast from arbitrary int, Key.None misuse, or synthetic key from external input).","commonSituations":"Raising simulated keyboard events in tests or automation; translating raw scan codes/virtual keys from Win32 interop into Key; corrupt persisted key data.","solutions":["Pass only valid Key values; check Keyboard.IsValidKey(key) before constructing","Fix the int-to-Key conversion that produced the invalid value (e.g. off-by-one or wrong virtual-key mapping)","For synthetic input, use valid keys from the Input/KeyboardSupport keys tables"],"exampleFix":"// before\nvar args = new KeyEventArgs(kb, src, timestamp, (Key)5000);\n// after\nKey k = (Key)5000;\nif (Keyboard.IsValidKey(k))\n    var args = new KeyEventArgs(kb, src, timestamp, k);","handlingStrategy":"validation","validationCode":"bool keyOk = Keyboard.IsValidKey(key);","typeGuard":"bool IsValidKey(Key k) => Keyboard.IsValidKey(k);","tryCatchPattern":"try { var e = new KeyEventArgs(kb, src, ts, key); } catch (InvalidEnumArgumentException) { /* invalid key from interop */ }","preventionTips":["Validate all interop-derived keys with Keyboard.IsValidKey before use","Never cast arbitrary integers to Key without range checks"],"tags":["wpf","keyboard","invalid-enum"],"backgroundTag":"invalid-enum-argument","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"}