Skip to content

Instantly share code, notes, and snippets.

@DanAntFerrari
Created January 21, 2014 13:03
Show Gist options
  • Save DanAntFerrari/8539621 to your computer and use it in GitHub Desktop.
Save DanAntFerrari/8539621 to your computer and use it in GitHub Desktop.
Active fullscreen by key down
function keyboardShortcuts(){
// setup the keyboard shortcuts
console.log('keyboardShortcuts: listner ON');
document.addEventListener('keyup', function(e) {
console.log('keyboardShortcuts: e.keyCode',e.keyCode);
switch(e.keyCode) {
case 70:
// F
// Make the body toggle full screen.
var isInFullScreen = (document.fullScreenElement && document.fullScreenElement !== null) || (document.mozFullScreen || document.webkitIsFullScreen);
var el = document.documentElement; // Make the body go full screen.
// window.console.firebug
if (isInFullScreen) {
cancelFullScreen();
} else {
requestFullScreen(el);
}
break;
default:
break;
}
}, false);
} // keyboardShortcuts();
function requestFullScreen(el){
console.log('_requestFullScreen');
// Supports most browsers and their versions.
//https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Using_full_screen_mode?redirectlocale=en-US&redirectslug=Web%2FGuide%2FDOM%2FUsing_full_screen_mode
var rfs = // for newer Webkit and Firefox
el.requestFullScreen
|| el.webkitRequestFullScreen
|| el.mozRequestFullScreen
|| el.msRequestFullScreen
;
if(typeof rfs!="undefined" && rfs){
rfs.call(el);
} else if(typeof window.ActiveXObject!="undefined"){
// for Internet Explorer
var wscript = new ActiveXObject("WScript.Shell");
if (wscript!=null) {
wscript.SendKeys("{F11}");
}
}
} // requestFullScreen();
function cancelFullScreen(){
console.log('_cancelFullScreen');
// Exit full screen.
var el = document
,rfs = // for newer Webkit and Firefox
el.exitFullscreen
|| el.msExitFullscreen
|| el.mozCancelFullScreen
|| el.webkitExitFullscreen
;
if(typeof rfs!="undefined" && rfs){
rfs.call(el);
} else if(typeof window.ActiveXObject!="undefined"){
// for Internet Explorer
var wscript = new ActiveXObject("WScript.Shell");
if (wscript!=null) {
wscript.SendKeys("{F11}");
}
}
} // requestFullScreen(element);
// init keyboardShortcuts;
keyboardShortcuts();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment