Search code examples
javascriptgoogle-chromefullscreenonkeydown

(Javascript) onkeydown doesn't work when press F11 to exit full screen?


I use this code below to count how many times does the user press F11 (on Chrome). When I press F11 to go full screen, the variable countF11 increases 1, but it doesn't change when I press F11 to exit full screen. What is the reason and is there a solution for this?

var countF11 = 0;

document.onkeydown = function (e) {
    if (e.keyCode == 122) {
        countF11++;
        document.getElementById("status").innerHTML = countF11;
    }
}

Solution

  • Chrome intentionally prevents any keyboard event listeners from firing when F11 is pressed when the browser is in "full screen mode." This is to prevent malicious JavaScript from preventing the client from leaving "full screen mode."

    Firefox does not have the same limitation; it will work just fine there.