Not catch the keypress event for mediakey on safari video

I am recently testing media keypress event for LIVE/VOD videos using Safari. My Mac version is 10.13.6 and Safari Version is 13.1.

While the video is playing, I pressed the PLAY/PAUSE button from keyboard. The video is being paused and again clicking on the same button the video is playing. My requirement is to check some internal business rule and accordingly allow and disable the PLAY/PAUSE from the keyboard using Javascript.

For this I need to capture the keypress event and add the business logic.

We can capture this by using the following on Chrome:

navigator.mediaSession.setActionHandler('pause', function() {});

But in safari navigator.mediaSession is NULL as there is no mediaSession in navigator object for Safari.


Tried with the following code


var video = document.getElementById('vid')


video.addEventListener('keydown', (e) => {

console.log(e);

});


video.addEventListener('play', () => {

console.log("test")

document.getElementById("vid").focus();

});


keydown/keyup or keypress is working for all other keys but for media keys it is not working.


Is there any nice way of detecting and handling the media keys on the user’s keyboard from Safari using Javascript?

Did you find a solution? I am looking for exactly same whole day already...
Thank you!
Same.
Not catch the keypress event for mediakey on safari video
 
 
Q