{"record":{"id":"e89b5d37089b8866","repo":"dotnet/wpf","slug":"invalidenumargumentexception-key-int-key-typeof-key-e89b5d","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/KeyboardDevice.cs","lineNumber":205,"sourceCode":"                    modifiers |= ModifierKeys.Control;\n                }\n                if(IsKeyDown_private(Key.LeftShift) || IsKeyDown_private(Key.RightShift))\n                {\n                    modifiers |= ModifierKeys.Shift;\n                }\n\n                return modifiers;\n            }\n        }\n\n        /// <summary>\n        /// There is a proscription against using Enum.IsDefined().  (it is slow)\n        /// so we write these PRIVATE validate routines instead.\n        /// </summary>\n        private void Validate_Key(Key key)\n        {\n            if( 256 <= (int)key || (int)key <= 0)\n                throw new  System.ComponentModel.InvalidEnumArgumentException(\"key\", (int)key, typeof(Key));\n        }\n\n        /// <summary>\n        /// This is the core private method that returns whether or not the specified key\n        ///  is down.  It does it without the extra argument validation and context checks.\n        /// </summary>\n        private bool IsKeyDown_private(Key key)\n        {\n            return ( ( GetKeyStatesFromSystem(key) & KeyStates.Down ) == KeyStates.Down );\n        }\n\n        /// <summary>\n        ///     Returns whether or not the specified key is down.\n        /// </summary>\n        public bool IsKeyDown(Key key)\n        {\n//             VerifyAccess();\n            Validate_Key(key);","sourceCodeStart":187,"sourceCodeEnd":223,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/KeyboardDevice.cs#L187-L223","documentation":"InvalidEnumArgumentException from KeyboardDevice.Validate_Key: because Enum.IsDefined is proscribed for performance, the private validator manually checks the Key value and rejects any key whose integer value is >= 256 or negative, i.e. not a member of the Key enumeration.","triggerScenarios":"Calling Keyboard.IsKeyDown/IsKeyUp/IsKeyToggled/GetKeyStates with Key.None (0) or a key >= 256 (cast from raw int or undefined enum member).","commonSituations":"Interop code mapping virtual-key codes directly into Key without adjusting for the Key enum's 1-based layout; iterating a range that includes Key.None; deserialized/binary data containing 0.","solutions":["Skip or special-case Key.None (0) before querying key state","Clamp/validate: if ((int)key > 0 && (int)key < 256) before calling, or use Keyboard.IsValidKey","Fix the virtual-key-to-Key conversion so it never yields 0 or >255"],"exampleFix":"// before\nbool down = Keyboard.IsKeyDown((Key)vk);\n// after\nKey k = (Key)vk;\nbool down = vk != 0 && Keyboard.IsKeyDown(k);","handlingStrategy":"validation","validationCode":"bool keyInRange = (int)key > 0 && (int)key < 256;","typeGuard":"bool IsQueryableKey(Key k) => (int)k > 0 && (int)k < 256;","tryCatchPattern":"try { var st = Keyboard.GetKeyStates(key); } catch (InvalidEnumArgumentException) { /* Key.None or out-of-range */ }","preventionTips":["Skip Key.None (0) when iterating or converting virtual-key codes","Clamp interop key codes to 1..255 before querying key state"],"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"}