{"record":{"id":"8da481c8c06996d9","repo":"dotnet/wpf","slug":"invalidenumargumentexception-actions-int-actions-typeof","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"actions\", (int)actions, typeof(RawKeyboardActions))","messagePattern":"InvalidEnumArgumentException\\(\"actions\", \\(int\\)actions, typeof\\(RawKeyboardActions\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/RawKeyboardInputReport.cs","lineNumber":59,"sourceCode":"        /// <param name=\"virtualKey\">\n        ///     The Win32 virtual key code if a key is being reported.\n        /// </param>\n        /// <param name=\"extraInformation\">\n        ///     Any extra information being provided along with the input.\n        /// </param>\n        public RawKeyboardInputReport(\n            PresentationSource inputSource,\n            InputMode mode,\n            int timestamp, \n            RawKeyboardActions actions, \n            int scanCode, \n            bool isExtendedKey,\n            bool isSystemKey,\n            int virtualKey, \n            IntPtr extraInformation) : base(inputSource, InputType.Keyboard, mode, timestamp)\n        {\n            if (!IsValidRawKeyboardActions(actions))\n                throw new System.ComponentModel.InvalidEnumArgumentException(\"actions\", (int)actions, typeof(RawKeyboardActions));\n\n            _actions = actions;\n            _scanCode = scanCode;\n            _isExtendedKey = isExtendedKey;\n            _isSystemKey = isSystemKey;\n            _virtualKey = virtualKey;\n            _extraInformation = extraInformation;\n        }\n\n        /// <summary>\n        ///     Read-only access to the set of actions that were reported.\n        /// </summary>\n        public RawKeyboardActions Actions {get {return _actions;}}\n\n        /// <summary>\n        ///     Read-only access to the scan code that was reported.\n        /// </summary>\n        public int ScanCode {get {return _scanCode;}}","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/RawKeyboardInputReport.cs#L41-L77","documentation":"The RawKeyboardInputReport constructor validates the actions parameter with IsValidRawKeyboardActions and throws InvalidEnumArgumentException when the value is not a defined member of RawKeyboardActions. RawKeyboardActions flags (ReportUp/ReportDown/ReportMultipleActions...) must describe a coherent keyboard report; arbitrary integer values are rejected.","triggerScenarios":"Constructing RawKeyboardInputReport with an out-of-range or undefined RawKeyboardActions value, e.g. casting a raw int from platform data, combining flags into an invalid combination, or passing 0/undefined bits.","commonSituations":"Interop/P-Invoke code building keyboard input reports from Win32 messages where the raw integer was not mapped to the enum; tests synthesizing input with hand-picked flag values; version drift after the enum gains or loses members.","solutions":["Use only named RawKeyboardActions values (ReportUp, ReportDown, ReportMultipleActions) instead of casting ints.","Validate the value with Enum.IsDefined(typeof(RawKeyboardActions), actions) before constructing the report.","Map platform-specific constants to the WPF enum explicitly in your interop layer.","Re-derive actions from the message (WM_KEYUP vs WM_KEYDOWN) rather than passing raw flag data."],"exampleFix":"// before\nvar report = new RawKeyboardInputReport(inputSource, mode, timestamp,\n    (RawKeyboardActions)rawInt, scanCode, ext, sys, vk, extraInfo); // may throw\n\n// after\nvar actions = (RawKeyboardActions)rawInt;\nif (!IsValidRawKeyboardActions(actions))\n    actions = up ? RawKeyboardActions.ReportUp : RawKeyboardActions.ReportDown;\nvar report = new RawKeyboardInputReport(inputSource, mode, timestamp,\n    actions, scanCode, ext, sys, vk, extraInfo);","handlingStrategy":"validation","validationCode":"static bool IsValidRawKeyboardActionsValue(RawKeyboardActions a) =>\n    Enum.IsDefined(typeof(RawKeyboardActions), a) ||\n    a == (RawKeyboardActions.ReportUp | RawKeyboardActions.ReportMultipleActions);","typeGuard":"bool TryGetKeyboardActions(int raw, out RawKeyboardActions actions)\n{\n    actions = (RawKeyboardActions)raw;\n    return Enum.IsDefined(typeof(RawKeyboardActions), actions);\n}","tryCatchPattern":"try\n{\n    var report = new RawKeyboardInputReport(src, mode, ts, actions, scan, ext, sys, vk, extra);\n}\ncatch (InvalidEnumArgumentException ex)\n{\n    Log($\"Invalid RawKeyboardActions: {ex}\");\n}","preventionTips":["Only assign named RawKeyboardActions members; avoid int casts from interop data.","Validate with Enum.IsDefined before constructing input reports.","Map Win32 keyboard messages explicitly (WM_KEYDOWN -> ReportDown, WM_KEYUP -> ReportUp).","Add unit tests covering every action value you synthesize."],"tags":["wpf","input","enum","argument-validation"],"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-21T21:30:21.729Z"}