WinUI3 : How to catch CTRL + A key press in WinUI3 TextBox

Harshithraj1871 1,621 Reputation points
2025-02-25T12:23:50.6666667+00:00

Hi,

Im working on WinUI3 desktop application in CPP. I wanted to Catch CTRL + A press on WinUI3 TextBox. I checked online and found CoreWindow.GetKeyState(VirtualKey), but this will not work on Desktop applciation.

I used TextBox.KeyDown with GetKeyState(VK_CONTROL) to get CTRL + A, but it did not work. but TextBox.KeyUp with GetKeyState(VK_CONTROL) worked. Why is that so? Is this the right way to get CTRL + A key event?

Edit : Code snippet

    `textBox.KeyDown([textBox](auto const& sender, winrt::Microsoft::UI::Xaml::Input::KeyRoutedEventArgs const& args)`
            {

                // Get the virtual key

                if (args.Key() == winrt::Windows::System::VirtualKey::A )

                {

                    if (GetKeyState(VK_CONTROL) & 0x8000) // 0x8000 indicates the key is down

                    {

                        int check = 0; // This Line was not hit, When CTRL + A was pressed

                    }

                    // Mark event as handled to prevent default behavior

                    args.Handled(true);

                }

            });

         textBox.KeyUp([textBox](auto const& sender, winrt::Microsoft::UI::Xaml::Input::KeyRoutedEventArgs const& args)

             {

                 // Get the virtual key

                 if (args.Key() == winrt::Windows::System::VirtualKey::A)

                 {

                     if (GetKeyState(VK_CONTROL) & 0x8000) // 0x8000 indicates the key is down

                     {

                         int a = 0; //  This Line was hit, When CTRL + A was pressed 

                     }

                     // Mark event as handled to prevent default behavior

                     args.Handled(true);

                 }

             });
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.
830 questions
{count} votes

Accepted answer
  1. Jeanine Zhang-MSFT 10,856 Reputation points Microsoft External Staff
    2025-02-26T06:17:34.9166667+00:00

    Hi

    Welcome to Microsoft Q&A!

    I used TextBox.KeyDown with GetKeyState(VK_CONTROL), but it did not work. but TextBox.KeyUp with GetKeyState(VK_CONTROL) worked. Why is that so?

    The shortcut of the system already contains CTRL + A, so when CTRL + A is pressed, it will be intercepted by the system.

    If you want to catch CTRL + A key press, you need to override this shortcut manually via KeyboardAccelerators

                <TextBox.KeyboardAccelerators>
                    <KeyboardAccelerator Key="A" Modifiers="Control" Invoked="OnCtrlCInvoked"/>
                </TextBox.KeyboardAccelerators>
    
        private void OnCtrlCInvoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
        {
            if (args.KeyboardAccelerator.Key == VirtualKey.A && args.KeyboardAccelerator.Modifiers == VirtualKeyModifiers.Control)
            {
                System.Diagnostics.Debug.WriteLine("Ctrl+A Pressed");
                args.Handled = true;
            }
        }
    

    For more details you could refer to the Doc: WinUI 3 APIs (Windows App SDK)

    Thank you

    Jeanine


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.