UWP - Do not trigger the Click event when you press the spacebar (for example, in the "Movies and TV" application)

I am working on a custom media player and trying to reproduce the same behavior as in the Movies and TV application (Windows 10 CU).

Spaceused to play and pause the video no matter what. Space not used to press buttons when they are focused (but Enterthere are). This behavior violates some keyboard accessibility rules , but I think everything is fine. Spacefor Play and Pause, this is what the user expects.

Question: how did they do it?

I found a few half-solutions:

Decision 1 Window.Current.CoreWindow.KeyDown and ifinClick Event Handler

Page.xaml.cs:

protected override void OnNavigatedTo(NavigationEventArgs e)
 {
  Window.Current.CoreWindow.KeyDown += CoreWindowOnKeyDown;
  //...
 }

 bool isItSpace;

 private void CoreWindowOnKeyDown(CoreWindow sender, KeyEventArgs args)
 {  
    if (args.VirtualKey ==  VirtualKey.Space)
    isItSpace = true;
 }


 private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
 {
     if (isItSpace)
     {
       isItSpace = false;
       return;
     }
    //...
 }

Page.xaml:

<Button  Click="ButtonBase_OnClick"  >Button Text</Button>

Why not:

  • if Click
  • ...

2 Window.Current.CoreWindow.KeyDown KeyUp

- :

    if (FocusManager.GetFocusedElement() is Button)
    {
        var bu = (Button)FocusManager.GetFocusedElement();
        bu.IsEnabled = false;
    }

  • . . ..
  • .

3 button.ClickMode = ClickMode.Hover

ClickMode Hover, . KeyDown (, 2), Click . Hover, Enter ...

?

+4
1

, , , keybaord, Enter Space, .

, , .

Windows 10 build 14393 - , .

14393 AllowFocusOnInteraction, . , , , , .

, MediaTransportControls, .

AllowFocusOnInteraction="False" AppBarButton ( Flyout, AudioMuteButton!), PlayPauseButton.

, PlayPauseButton, . , Space, , , play/pause .

, keybaord. , Tab . , .


Update

, - Space, , -, .

, , ExtendedAppBarButton, .

2,

AddHandler(KeyDownEvent, new KeyEventHandler(OnKeyDown), true);
AddHandler(KeyUpEvent, new KeyEventHandler(OnKeyUp), true);

IsEnabled Space. , IsPlayPauseButton, KeyDown Window, , True.

ExtendedAppBarButton , :

<Setter Property="Width" Value="{ThemeResource MTCMediaButtonWidth}" />
<Setter Property="Height" Value="{ThemeResource MTCMediaButtonHeight}" />
<Setter Property="AllowFocusOnInteraction" Value="False" />
<Setter Property="AllowFocusWhenDisabled" Value="True" />
<Setter Property="UseSystemFocusVisuals" Value="False" />

, UseSystemFocusVisuals False ', , , .

<Rectangle x:Name="FocusVisual"
           Opacity="0"
           StrokeThickness="2"
           Stroke="{TemplateBinding BorderBrush}" />

, Storybaord Disabled, , IsEnabled.

, MediaTransportControls AppBarButton , .

, , , -.

, - . .

+5

Source: https://habr.com/ru/post/1680073/


All Articles