I am working on custom media player and trying to reproduce same behavior as in Movies & TV app (Windows 10 CU).
Space is used to Play & Pause video no matter what. Space is not used to click buttons when they are focused (but Enter is). This behavior breaks some rules about keyboard accessibility, but I think it is OK. Space for Play & Pause is something that user expect.
Question is: How they did it?
I found some half-solutions:
Solution 1 Window.Current.CoreWindow.KeyDown
and if
in Click 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:
- Need to add
if
in every Click Handler - There is button click animation...
Solution 2 Disable focused Button in Window.Current.CoreWindow.KeyDown
and enable it in KeyUp
Something like:
if (FocusManager.GetFocusedElement() is Button)
{
var bu = (Button)FocusManager.GetFocusedElement();
bu.IsEnabled = false;
}
Why not
- It change focus from disabled button. Need to change it back after enable. Focus is jumping..
- Disable and enable animation appear
- it is too hackie
Solution 3 button.ClickMode = ClickMode.Hover
When ClickMode
is set to Hover
it is not possible to Click it with keyboard. When I change it in KeyDown
handler of Window (like solution 2) it still bubble to Click event for the first time. Solution could be set all buttons to Hover
, but in that case Enter will not cause the Click and I need to change all buttons...
Are there any better solutions on your mind?