{"record":{"id":"c901644c24227ea5","repo":"dotnet/wpf","slug":"sr-hwndhostdoesnotsupportchildkeyboardsinks","errorCode":null,"errorMessage":"SR.HwndHostDoesNotSupportChildKeyboardSinks","messagePattern":"SR\\.HwndHostDoesNotSupportChildKeyboardSinks","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Interop/HwndHost.cs","lineNumber":209,"sourceCode":"        // explicit interface implementations (that do have the property of being hidden from the public\n        // contract, which limits IntelliSense on derived types like WebBrowser) while sticking protected\n        // virtuals next to them. Those virtuals contain our base implementation, while the explicit\n        // interface implementation methods do call trivially into the virtuals.\n        //\n        // This comment outlines the security rationale applied to those methods.\n        //\n        // <SecurityNote Name=\"IKeyboardInputSink_Implementation\">\n        //     The security attributes on the virtual methods within this region mirror the corresponding\n        //     IKeyboardInputSink methods; customers can override those methods, so we insert a LinkDemand\n        //     to encourage them to have a LinkDemand too (via FxCop).\n\n        /// <summary>\n        ///     Registers a IKeyboardInputSink with the HwndSource in order\n        ///     to retreive a unique IKeyboardInputSite for it.\n        /// </summary>\n        protected virtual IKeyboardInputSite RegisterKeyboardInputSinkCore(IKeyboardInputSink sink)\n        {\n            throw new InvalidOperationException(SR.HwndHostDoesNotSupportChildKeyboardSinks);\n        }\n\n        IKeyboardInputSite IKeyboardInputSink.RegisterKeyboardInputSink(IKeyboardInputSink sink)\n        {\n            return RegisterKeyboardInputSinkCore(sink);\n        }\n\n        /// <summary>\n        ///     Gives the component a chance to process keyboard input.\n        ///     Return value is true if handled, false if not.  Components\n        ///     will generally call a child component's TranslateAccelerator\n        ///     if they can't handle the input themselves.  The message must\n        ///     either be WM_KEYDOWN or WM_SYSKEYDOWN.  It is illegal to\n        ///     modify the MSG structure, it's passed by reference only as\n        ///     a performance optimization.\n        /// </summary>\n        protected virtual bool TranslateAcceleratorCore(ref MSG msg, ModifierKeys modifiers)\n        {","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Interop/HwndHost.cs#L191-L227","documentation":"HwndHost does not support child keyboard input sinks: its virtual RegisterKeyboardInputSinkCore deliberately throws InvalidOperationException with SR.HwndHostDoesNotSupportChildKeyboardSinks. Unlike HwndSource, an HwndHost cannot register nested IKeyboardInputSink components for Tab/keyboard routing.","triggerScenarios":"Calling IKeyboardInputSink.RegisterKeyboardInputSink (or the protected RegisterKeyboardInputSinkCore) with a child sink on an HwndHost instance, e.g. hosting a component that tries to register itself as a keyboard sink with an HwndHost-based control.","commonSituations":"Embedding WindowsFormsHost or other sink-registering children inside an HwndHost-derived control and relying on cross-boundary keyboard sink registration; migrating code from HwndSource to HwndHost.","solutions":["Do not register child keyboard sinks with HwndHost; remove the RegisterKeyboardInputSink call.","Restructure so the child is hosted in an HwndSource if sink registration is required.","Override RegisterKeyboardInputSinkCore in a derived HwndHost class to implement sink support yourself.","Handle keyboard interop via standard focus/Tab routing (e.g. IKeyboardInputSink on the top-level HwndSource) instead."],"exampleFix":"// before\nvar site = ((IKeyboardInputSink)hwndHost).RegisterKeyboardInputSink(childSink);\n// after\n// HwndHost does not support child sinks; rely on the owning HwndSource\nvar site = ((IKeyboardInputSink)hwndSource).RegisterKeyboardInputSink(childSink);","handlingStrategy":"type-guard","validationCode":"if (host is HwndHost)\n    throw new NotSupportedException(\"HwndHost does not support child keyboard sinks\");","typeGuard":"bool SupportsChildSinks(IKeyboardInputSink sink) => sink is HwndSource;","tryCatchPattern":"try\n{\n    site = ((IKeyboardInputSink)sinkContainer).RegisterKeyboardInputSink(childSink);\n}\ncatch (InvalidOperationException)\n{\n    // fall back to top-level HwndSource sink registration\n}","preventionTips":["Only register child keyboard sinks with HwndSource, never HwndHost.","Review third-party components that self-register as sinks when embedded.","Handle keyboard interop through focus and Tab routing instead of sink registration."],"tags":["wpf","hwndhost","keyboard-input"],"backgroundTag":"unsupported-operation","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"}