I want to trigger f11 event of keyboard using javascript or jquery, can anyone tell me how to do this ?
You can't make people's computers type things through javascript. It violates the sandboxing environment of browsers. I assume that you are trying to make a window go full screen though. This SO question covers that.
How to make the window full screen with Javascript (stretching all over the screen)
To trigger event Keypress of F11 key like from keyboard, we need this..
Call this function on any event..
function mKeyF11(){
var e = new Event('keypress');
e.which = 122; // Character F11 equivalent.
e.altKey=false;
e.ctrlKey=false;
e.shiftKey=false;
e.metaKey=false;
e.bubbles=true;
document.dispatchEvent(e);
}
This is pure Javascript function and don't need jQuery to support it..
Update: Chrome prevents JavaScript key-press. Source
For chrome we can use the following.
if (document.documentElement.webkitRequestFullscreen) {
document.documentElement.webkitRequestFullscreen();
}
You can try using this one: http://andrew.hedges.name/experiments/fullscreen/
Working code sample:
document.addEventListener("keyup", keyUpTextField, false);
function keyUpTextField (e)
{
var keyCode = e.keyCode;
if(keyCode==122){/*ejecutaAlgo*/}
}
I'm use this code:
function go_full_screen(){
var elem = document.documentElement;
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.msRequestFullscreen) {
elem.msRequestFullscreen();
} else if (elem.mozRequestFullScreen) {
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullscreen) {
elem.webkitRequestFullscreen();
}
}
<a href="#" onClick="go_full_screen();">Full Screen / Compress Screen</a>