As of Chrome 73, there’s explicit support for media keys, see https://developers.google.com/web/updates/2019/02/chrome-73-media-updates
In summary, you can install an event handler with
navigator.mediaSession.setActionHandler('previoustrack', function() {
// User hit "Previous Track" key.
});
The document above gives a good overview.
https://googlechrome.github.io/samples/media-session/ has example code and a demo.
https://developer.mozilla.org/en-US/docs/Web/API/Media_Session_API has more detailed documentation.