Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing the ESC key using Javascript only

I have a custom video play with it's own controls. I have a script to make changes to the controls when the user enters and exists fullscreen. The problem is that when I exit fullscreen using the ESC key instead of the button, the style changes to the controls are not reverted back. I need to ensure that exiting fullscreen using ESC or button will result in the same behaviour.

CSS:

function toggleFullScreen() {

  elem = document.getElementById("video_container");
  var db = document.getElementById("defaultBar"); 
  var ctrl = document.getElementById("controls");

  if (!document.fullscreenElement &&    // alternative standard method
      !document.mozFullScreenElement && !document.webkitFullscreenElement) {  // current working methods
    if (document.documentElement.requestFullscreen) {

          ctrl.style.width = '50%';
          ctrl.style.left = '25%';
          full.firstChild.src = ('images/icons/unfull.png');
          elem.requestFullscreen();          
        } else if (document.documentElement.mozRequestFullScreen) {
          full.firstChild.src = 'images/icons/unfull.png';
          ctrl.style.width = '50%';
          ctrl.style.left = '25%';
          elem.mozRequestFullScreen();
        } else if (document.documentElement.webkitRequestFullscreen) {

          ctrl.style.width = '50%';
          ctrl.style.left = '25%';
          full.firstChild.src = 'images/icons/unfull.png';
          elem.webkitRequestFullscreen();

        }
  } else if (document.exitFullscreen) {
        full.firstChild.src = 'images/icons/full.png';
        ctrl.style.width = '100%';
        ctrl.style.left = '0';        
        document.exitFullscreen();
        }
        else if (document.mozCancelFullScreen) {
        full.firstChild.src = 'images/icons/full.png';
        ctrl.style.width = '100%';
        ctrl.style.left = '0'; 
        document.mozCancelFullScreen();
        }
        else if (document.webkitCancelFullScreen) {

        ctrl.style.width = '100%';
        ctrl.style.left = '0';
        full.firstChild.src = 'images/icons/full.png';
        document.webkitCancelFullScreen();
        } 
}
like image 333
Batman Avatar asked Feb 17 '13 19:02

Batman


People also ask

What is code for Esc key?

On computer keyboards, the Esc key Esc (named Escape key in the international standard series ISO/IEC 9995) is a key used to generate the escape character (which can be represented as ASCII code 27 in decimal, Unicode U+001B, or Ctrl + [ ).

How can I tell if Esc key is pressed?

function keyPress (e) { if(e. key === "Escape") { // write your logic here. } }

What is keyPress event in Javascript?

The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys. Examples of keys that don't produce a character value are modifier keys such as Alt , Shift , Ctrl , or Meta .


1 Answers

event.key === "Escape"

More recent and much cleaner: use event.key. No more arbitrary number codes!

document.addEventListener("keydown", function(event) {
    const key = event.key; // Or const {key} = event; in ES6+
    if (key === "Escape") {
        // Do things
    }
});

Modern style, with lambda and destructuring

document.addEventListener("keydown", ({key}) => {
    if (key === "Escape") // Do things
})

Mozilla Docs

Supported Browsers

like image 101
Gibolt Avatar answered Oct 04 '22 22:10

Gibolt