{"record":{"id":"5693e144caa27594","repo":"dotnet/wpf","slug":"sr-onlyacceptskeymessages","errorCode":null,"errorMessage":"SR.OnlyAcceptsKeyMessages","messagePattern":"SR\\.OnlyAcceptsKeyMessages","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndKeyboardInputProvider.cs","lineNumber":694,"sourceCode":"                modifierKeys |= ModifierKeys.Control;\n            }\n\n            keyState = UnsafeNativeMethods.GetKeyState(NativeMethods.VK_MENU);\n            if((keyState & 0x8000) == 0x8000)\n            {\n                modifierKeys |= ModifierKeys.Alt;\n            }\n\n            return modifierKeys;\n        }\n\n        private RawKeyboardActions GetKeyUpKeyDown(WindowMessage msg)\n        {\n            if(  msg == WindowMessage.WM_KEYDOWN || msg == WindowMessage.WM_SYSKEYDOWN )\n                return RawKeyboardActions.KeyDown;\n            if(  msg == WindowMessage.WM_KEYUP || msg == WindowMessage.WM_SYSKEYUP )\n                return RawKeyboardActions.KeyUp;\n            throw new ArgumentException(SR.OnlyAcceptsKeyMessages);\n        }\n\n        private void PossiblyDeactivate(IntPtr hwndFocus)\n        {\n            Debug.Assert( null != _source );\n\n            // We are now longer active ourselves, but it is possible that the\n            // window the keyboard is going to intereact with is in the same\n            // Dispatcher as ourselves.  If so, we don't want to deactivate the\n            // keyboard input stream because the other window hasn't activated\n            // it yet, and it may result in the input stream \"flickering\" between\n            // active/inactive/active.  This is ugly, so we try to supress the\n            // uneccesary transitions.\n            //\n            bool deactivate = !IsOurWindow(hwndFocus);\n\n            // This window itself should not be active anymore.\n            _active = false;","sourceCodeStart":676,"sourceCodeEnd":712,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndKeyboardInputProvider.cs#L676-L712","documentation":"HwndKeyboardInputProvider.GetKeyUpKeyDown translates only keyboard key messages (WM_KEYDOWN/WM_SYSKEYDOWN/WM_KEYUP/WM_SYSKEYUP) into RawKeyboardActions. Any other WindowMessage passed to it is a programming error, so it throws ArgumentException with SR.OnlyAcceptsKeyMessages. The library uses this as an internal contract check on the message-filtering path.","triggerScenarios":"Calling GetKeyUpKeyDown (directly or through the keyboard input provider pipeline) with a WindowMessage that is not one of WM_KEYDOWN, WM_SYSKEYDOWN, WM_KEYUP, or WM_SYSKEYUP — typically a custom message filter or subclassing WndProc forwarding non-key messages.","commonSituations":"Custom HwndSource hooks/addhooks that forward arbitrary messages (e.g. WM_CHAR, WM_KEYUP variants like WM_UNICHAR) into the keyboard processing path; automation or testing frameworks calling the internal translation with synthetic messages.","solutions":["Ensure only WM_KEYDOWN/WM_SYSKEYDOWN/WM_KEYUP/WM_SYSKEYUP messages are routed to this code path; filter other messages before calling.","If you control the call site, branch on msg first and handle non-key messages separately instead of passing them through.","Update any custom AddHook handler so it does not return RawKeyboardActions for messages it does not recognize."],"exampleFix":"// before\nvar action = GetKeyUpKeyDown(msg); // throws for WM_CHAR etc.\n// after\nif (msg == WindowMessage.WM_KEYDOWN || msg == WindowMessage.WM_KEYUP ||\n    msg == WindowMessage.WM_SYSKEYDOWN || msg == WindowMessage.WM_SYSKEYUP)\n{\n    var action = GetKeyUpKeyDown(msg);\n}\n","handlingStrategy":"validation","validationCode":"static bool IsKeyMessage(WindowMessage msg) =>\n    msg == WindowMessage.WM_KEYDOWN || msg == WindowMessage.WM_KEYUP ||\n    msg == WindowMessage.WM_SYSKEYDOWN || msg == WindowMessage.WM_SYSKEYUP;","typeGuard":null,"tryCatchPattern":"try\n{\n    var action = GetKeyUpKeyDown(msg);\n}\ncatch (ArgumentException)\n{\n    // msg was not a key message; ignore or log\n}","preventionTips":["Filter WindowMessages to the key-message set before keyboard translation.","Don't forward raw WndProc messages unfiltered into keyboard processing.","Centralize the message whitelist in one helper used by all hooks."],"tags":["argument","keyboard","window-messages","wpf"],"backgroundTag":"invalid-argument-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"}