How to bind Next/Previous media buttons

Galuvian 20 Reputation points
2023-03-30T02:17:59.0166667+00:00

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.
Windows 10 Volume Control Popup for groove music

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.

Debugger Screenshot

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.

User's image

Developer technologies | Universal Windows Platform (UWP)
{count} votes

Answer accepted by question author
  1. Anonymous
    2023-04-03T08:55:05.4633333+00:00

    Hello,

    Welcome to Microsoft Q&A!

    It's not possbile to use the KeyboardAccelerator Class if the key is not listed in the VirtualKey Enum. The key property in the KeyboardAccelerator object only receive VirtualKey Enum. Since you've found the CoreWindow.KeyDown works and you could detect the value of the pressed key, I'd suggest you do the navigation in the CoreWindow.KeyDown event. The way you found should be the only possible solution about this.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.