Muhammad Touseef
Muhammad Touseef

Reputation: 4465

Canceling the SpaceBar Click/Tap on uwp controls throughout the app

When a UI element ( control ) is focused in a uwp app it can be triggered with Spacebar or the Enter keys, this is not limited to Desktop but also helps in Xbox so user can navigate through the controls and press on any focused control to active its Command.

Use case

But in My use case I want only Enter key to trigger that behaviour and Spacebar should not do anything at all no matter which control is pressed on the screen.

The reason to this requirement is that I am building a MediaPlayer application and no matter which control or button is focused within the app when I press Spacebar I want to simply link it to the Play/Pause Behaviour of my media element.

Not a Duplicate

This question is not a duplicate of : UWP - Don't fire Click event when pressing space (like in Movies & TV app)

Because in the question linked above, the answer was only relevant if any of the AppBarButtons were focused so they will not do anything on pressing Space but only will be invoked with Enter. But in my use case I want to apply the same behavior even outside the MediaPlayerElement control. I have a NavigationView and MediaPlayerElement resides in one of the pages, so I want this behavior to work even when a NavigationViewItem is focused or any other control which can be focused and invoked should only be invoked with Enter and not Space.

Is there a app level solution where I can apply this behaviour at the very root control and it descends to all of its children i.e : whole app?

What I have tried

I have tried with the already answered question (linked above) and that works fine for its limited scenario. And I have also tried setting AllowFocusOnInteraction=false to every app bar button and also other extra controls I have in the style of my CustomMediaTransportControls. But this is also limited to MediaPlayerElement only and also it prevents tab navigation which is not good for accessibility.

Upvotes: 1

Views: 379

Answers (2)

Nico Zhu
Nico Zhu

Reputation: 32785

There are many ways could approach, You could listen the current Content PreviewKeyDown event to detect Space press.

public Scenario1()
{
    this.InitializeComponent();
    Window.Current.Content.PreviewKeyDown += Content_PreviewKeyDown;  
}

private void Content_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
    e.Handled = e.Key == VirtualKey.Space ? true : false;
}

You could also GLOBAL HOTKEY for your uwp app that could be used when your app's window not in foreground. For more derail you could check this blog .

Upvotes: 1

Martin Zikmund
Martin Zikmund

Reputation: 39092

You can do this by handling the PreviewKeyDown event higher in the visual tree hierarchy, for example in the Page.

Subscribe to the event in the Page constructor:

public MainPage()
{
    this.InitializeComponent();
    this.PreviewKeyDown += MainPage_PreviewKeyDown;            
}

Or in XAML:

<Page ... PreviewKeyDown="MainPage_PreviewKeyDown">

And in the event handler set the KeyRoutedEventArgs to handled when the Space key was pressed:

private void MainPage_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == VirtualKey.Space)
    {
        e.Handled = true;
    }
}

This way the key down event will never reach any control below in the hierarchy because the PreviewKeyDown event propagates the tree before the event takes place.

Upvotes: 2

Related Questions