How to intercept Keyboard event before its sent to widget in WinUI3

Harshithraj1871 1,681 Reputation points
2025-03-08T11:09:49.0433333+00:00

Hi,

I'm working on WinUI3 desktop application inn cpp. I want to intercept keyboard event before it reaches the widget and do custom handling and not trigger the default handling. I tried KeyDown event, but with that we cant stop the default handling, even when i did args.handled(true), it did not stop default handling.

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
838 questions
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 20,771 Reputation points Microsoft External Staff
    2025-03-10T08:37:41.8266667+00:00

    Hello @Harshithraj1871

    To summaries this question to help anyone with the same problem.

    It is recommended to use PreviewKeyDown Event to intercept keyboard event before it reaches the UI element.

     <TextBox Text="test" KeyDown="TextBox_KeyDown"  PreviewKeyDown="TextBox_PreviewKeyDown"/>
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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