javascripthtmldom-events

HTML <video> tag - key events not received without controls attribute | Event#preventDefault not cancelling controls


I have attempted to display a video with custom controls. It seems to be working fine, but I have one problem. The controls attribute seem to be causing problems when I try to listen for key events.

I currently have this piece of code for testing:

const video = document.getElementById("video");

video.addEventListener("keypress", (event) => {
    console.log("keypress");
    console.log(`  Key: ${event.which}`);
    
    event.preventDefault();
});

I have done the same for keydown as well, but it has the same problem. The problem being one of two things:

Q&A:

I have attempted to find a solution for the past day, but to no avail. If you have any suggestions, please do let me know.


Solution

  • I finally got it working, after typing back and forth through the comments.

    Listening for the keydown event on the document in capture phase allows me to use event.preventDefault(). I just have to add some conditions so it does not cancel all key presses in the document.

    const video = document.getElementById("video");
    
    document.addEventListener("keydown", (event) => {
        if (event.target !== video) return;
        
        // Space has keyCode 32
        if (event.which === 32) event.preventDefault();
    }, true);