Hi @Harshithraj1871 ,
Losing key input to the island's root ScrollViewer after clicking your own surface is understandably confusing, and your workaround is on the right track. Wrapping the Canvas in a focusable Control and calling Focus(FocusState::Pointer) in PointerPressed is a reasonable approach for system XAML, and in my test, keeping e.Handled(true) was the detail that made it work reliably.
In Windows.UI.Xaml, focus belongs to Control, and Panel/Canvas do not derive from it, so a wrapper is needed. The Control.IsTabStop reference notes that a control with IsTabStop set to false cannot receive focus, and Focus returns false. For elements that cannot take focus, the Keyboard accessibility guidance suggests using a focusable control or a custom control with explicit focus behavior, with IsTabStop set to true and a visible focus indicator. The Control.Focus reference recommends FocusState::Pointer when focus comes from a pointer interaction.
I tested this in a Win32 C++/WinRT app that hosts system XAML through DesktopWindowXamlSource and calls PreTranslateMessage in the message loop. The test ran on Windows build 26300.9457 with SDK 10.0.26100.0, using real clicks and key presses. With your code, the ContentControl kept focus and received KeyDown. When I removed only e.Handled(true), Focus still returned true, but focus moved to the internal ScrollViewer once pointer processing finished. This happened again in a fresh process. A true return value does not guarantee the wrapper keeps focus. This is what I observed in my host, not a documented guarantee.
I would set Handled only after focus actually moves:
ContentControl container;
container.IsTabStop(true);
container.Content(canvas);
container.PointerPressed(IInspectable const& sender, PointerRoutedEventArgs const& e
{
if (sender.as<ContentControl>().Focus(FocusState::Pointer))
{
// Without this, the internal ScrollViewer took focus back in my test.
e.Handled(true);
}
});
container.KeyDown(IInspectable const&, KeyRoutedEventArgs const& e
{
// Handle e.Key(); set e.Handled(true) only for keys the canvas consumes.
});
If you reuse this in several places, you can put the same logic in a custom control that overrides OnPointerPressed and OnKeyDown.
Keep two side effects in mind. Ancestors will no longer get ordinary PointerPressed. The wrapper also becomes a tab stop, so it needs a focus visual. In my test, a TextBox inside the canvas still took focus and accepted text when clicked, and Tab and Shift+Tab moved between it and the wrapper inside the island. Moving focus between the island and Win32 controls is a separate step that needs host handling, as described in Advanced scenarios for UWP XAML Islands.
I could not reproduce every detail you described. A bare Canvas set as island content had no ScrollViewer ancestor, and the wrapper still received KeyDown while focus was on the ScrollViewer. If your app behaves differently, please share your Windows build, whether you call PreTranslateMessage, and what FocusManager::GetFocusedElement returns right after the click.
If anything is still unclear or you run into other focus issues in the island, feel free to reply here and I will be glad to help further. If this answer resolves your question, you may consider following this guide, so it would help others.
Thank you.