I am working on a UWP application and I want to navigate to a new/previous page when the next/previous media buttons are pressed. These keys, which are pretty standard on multimedia keyboards and laptops. I also have a remote bluetooth device that has these same keys/buttons on it.
Subscribing to KeyDown or CharacterReceived does not work for the media buttons either, they seem to never raise these events.
First attempt to just verify if I can receive them:
<Page
x:Class="MyApp.Views.MyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:canvas="using:Microsoft.Graphics.Canvas.UI.Xaml"
mc:Ignorable="d"
CharacterReceived="Page_CharacterReceived"
KeyDown="Page_KeyDown">
And the codebehind
private void Page_CharacterReceived(UIElement sender, CharacterReceivedRoutedEventArgs args)
{
Console.WriteLine(args.Character);
}
private void Page_KeyDown(object sender, KeyRoutedEventArgs e)
{
Console.WriteLine(e.Key);
}
Nope, these are never triggered.
What about Keyboard Accelerators?
<Grid.KeyboardAccelerators >
<KeyboardAccelerator Key="GoForward" Invoked="KeyboardAccelerator_Invoked" />
</Grid.KeyboardAccelerators>
Nope. But I'm not sure if that's really the right key, its the closest I can find in the VirtualKey enum.
Next, I tried using the manual SystemMediaTransportControls as described here. That does allow me to receive the button presses, but it also displays a popup with the media controls. I don't want to play media, I just want to receive the button presses. Certainly don't want the pop-up to be displayed for 5 seconds. I can't find a way to suppress the popup. Setting the CommandManager.IsEnabled = false seems to not fully sever the connection like the article says it should, or maybe I'm misunderstanding what that does.
The popup is this screen, although mine didn't have this album loaded.

Finally I found a suggestion on another site to use CoreWindow.KeyDown
public myPage(){
Windows.UI.Xaml.Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
this.InitializeComponent();
}
private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
Console.WriteLine(args.VirtualKey.ToString());
}
Now I can get the keypress! It wasn't key 167 GoForward, but key 176 VK_MEDIA_NEXT_TRACK which isn't in the UWP VirtualKey enum. I had to go to the legacy documentation to find it. But this feels like a sledgehammer and not aligned with how development is supposed to be done in UWP.

Can I catch this with the Keyboard Accelerator?
<KeyboardAccelerator Key="176" Invoked="KeyboardAccelerator_Invoked" />
That compiles, but then throws this exception, which isn't a total surprise.
