How do I make a container (Canvas/Panel) receive key events when it is clicked? (Windows.UI.Xaml / XAML Islands)

Harshithraj1871 1,826 Reputation points
2026-12-02T10:31:41.98+00:00

I'm using system XAML (Windows.UI.Xaml) in a XAML Island, with C++/WinRT.

I have a container (a Canvas with a background). When I click on it and type something, I expect the container to receive the key events. Instead, focus goes to the root container, which is the ScrollViewer that XAML puts above the island's content, and that is where the key events go.

I found that a Panel/Canvas can't take focus, so I wrapped the Canvas in a ContentControl with IsTabStop = true. Clicking still didn't focus it, so I also handle PointerPressed and call Focus() myself:

ContentControl container;
container.IsTabStop(true);
container.Content(canvas);

container.PointerPressed([](IInspectable const& sender, PointerRoutedEventArgs const& e)
{
    sender.as<ContentControl>().Focus(FocusState::Pointer);
    e.Handled(true);
});



Now the container gets focus when clicked, and the key events go to it directly.

Is wrapping the container in a ContentControl like this the right approach? Or is there a better or recommended way to make a container take focus on click and receive key events?

Windows development | WinUI
0 comments No comments

Answer accepted by question author
Gatlin Le (WICLOUD CORPORATION) 1,840 Reputation points Microsoft External Staff Moderator
2026-12-02T11:02:52.3733333+00:00

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.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Oldest
  1. AI answer

    2026-12-02T10:32:34.8566667+00:00

    Wrapping the Canvas in a focusable ContentControl, setting IsTabStop(true), and explicitly calling Focus(FocusState::Pointer) from the pointer handler is an appropriate Windows.UI.Xaml solution.

    A Canvas is a layout element rather than a focusable Control; keyboard events are raised at the element holding input focus and then routed through its element tree. Microsoft’s guidance for non-focusable elements is to compose them inside a focusable control or implement a custom control with explicit focus behavior.^1^ ^2^

    Keep the wrapper as the keyboard-focus owner and attach the key handlers to it:

    ContentControl container;
    container.IsTabStop(true);
    container.Content(canvas);
    
    container.PointerPressed(
        [](IInspectable const& sender, PointerRoutedEventArgs const& e)
        {
            auto control = sender.as<ContentControl>();
            bool focused = control.Focus(FocusState::Pointer);
    
            if (focused)
            {
                e.Handled(true);
            }
        });
    
    container.KeyDown([](IInspectable const&, KeyRoutedEventArgs const& e)
    {
        // Process e.Key().
    });
    

    Focus(FocusState::Pointer) is the correct state when focus is assigned as the direct consequence of pointer input. Focus returns false when the control cannot receive focus, so checking its return value is useful during diagnostics.^3^

    For focus to succeed, the wrapper must remain:

    • enabled;
    • visible;
    • hit-test visible;
    • a tab stop.

    Setting IsTabStop(false) prevents both tab focus and programmatic Focus() from succeeding.^4^

    There is no documented contract in the supplied Windows.UI.Xaml material that a bare ContentControl automatically takes pointer focus whenever its content is clicked. Therefore, retaining the explicit pointer-to-focus behavior is safer than depending on incidental template or host behavior, particularly in a XAML Island.

    PointerPressed versus Tapped

    Use PointerPressed when focus must move immediately on press or when later pointer processing depends on it. If this is simply click activation and pointer details are irrelevant, Tapped is another possible interaction point; Microsoft notes that pointer events are primarily useful when examining pointer-specific details.^5^

    Only set e.Handled(true) if the press must not continue bubbling to ancestors. Focus does not inherently require suppressing the routed pointer event. If the island’s surrounding content needs the event—for selection, scrolling, or other behavior—leave it unhandled after setting focus.

    When to use a custom control instead

    For one container, ContentControl composition is the simpler option. If this behavior is reused or represents a distinct interactive component, derive a custom control from Control or ContentControl and put these responsibilities into it:

    • pointer-to-focus behavior;
    • IsTabStop;
    • keyboard handling;
    • a visible keyboard focus indicator;
    • appropriate automation name and semantics.

    Microsoft specifically recommends a focusable control type or a custom control with explicit focus behavior for an element that is not focus-capable by default, and requires a visible focus indicator.^1^

    If the region performs a standard command rather than acting as a general keyboard-input surface, prefer a standard Button containing the visual content. That provides pointer activation, Enter/Space activation, focus handling, and automation semantics without implementing them manually. Be aware that controls such as Button can class-handle Enter and Space and convert them into Click, so those keys might not reach an ordinary KeyDown handler.^2^


    References

    1. Keyboard accessibility
    2. Keyboard events
    3. Control.Focus(FocusState) Method-Definition
    4. Control.IsTabStop Property-Definition
    5. UIElement.PointerPressed Event-Definition
    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.