Back Button click event is firing when we press "space" and "enter" key in the keyboard

Sunil A M 171 Reputation points
2024-04-24T11:53:52.3266667+00:00

Hi Team,

I have a back button in my application to navigate back to the previous page.

Currently when i press "Enter" or "Space" from the keyboard, The back button click event is firing.(which we are not expected)

We need to back button click event(In code behind) should fire only when i mouse left click.

Please help me here for this.

Best Regards,

Sunil

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.
733 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 82,206 Reputation points
    2024-04-26T06:24:58.6566667+00:00

    You can handle ProcessKeyboardAccelerators

    For example, If I intercept [Space] on a Button which has the focus :

      myButton.ProcessKeyboardAccelerators += MyButton_ProcessKeyboardAccelerators;
    

    with :

    private void MyButton_ProcessKeyboardAccelerators(UIElement sender, ProcessKeyboardAcceleratorEventArgs args)       
    {                       
    	if (args.Key == VirtualKey.Space)            
    	{
               Console.Beep(8000, 10);   
               args.Handled = true;    
        }                     
    }
    
    
    1 person found this answer helpful.

  2. Xiaopo Yang - MSFT 11,661 Reputation points Microsoft Vendor
    2024-04-25T01:46:34.2566667+00:00

    I tested this WinUI-Gallery sample. When the back button of a navigation view has focus, Space key and Enter key do perform a variety of common user interactions but it is by design. According to Space key and Enter key, they will invoke the action or command associated with the focused control.

    Space key When not in text entry mode, the Space key invokes the action or command associated with the focused control (just like a tap with touch or a click with a mouse).

    Enter key The Enter key can perform a variety of common user interactions, depending on the control with focus:

    • Activates command controls such as a Button or Hyperlink. To avoid end user confusion, the Enter key also activates controls that look like command controls such as ToggleButton or AppBarToggleButton.
    • Displays the picker UI for controls such as ComboBox and DatePicker. The Enter key also commits and closes the picker UI.
    • Activates list controls such as ListView, GridView, and ComboBox.
      • The Enter key performs the selection action as the Space key for list and grid items, unless there is an additional action associated with these items (opening a new window).
      • If an additional action is associated with the control, the Enter key performs the additional action and the Space key performs the selection action. NOTE The Enter key and Space key do not always perform the same action, but often do.