Why trigger F11 press event doesn't work?

wong2 picture wong2 · Jun 21, 2011 · Viewed 18.5k times · Source

I just read this question: Full Screen Page by pressing button instead of F11
The op asked to replace F11 with other hot keys, so I'm wondering that maybe I can simulate press F11 to get things work.
I learned that I can use trigger in JQuery to simulate key press event, so I do something like this:

$("body").keyup(function (e) {
    alert(e.which);
});
var e = $.Event("keyup");
e.which = 122; // # Key code of F11
$("body").trigger(e);  

When I run this, I got the alert says 122, but it seems that it doesn't give the hoped result. Is there a restriction there?

I made a fiddle here: http://jsfiddle.net/ap295/5/

Answer

Val picture Val · Jun 21, 2011

I think this is the one :) to detect it ...

$(document).keyup(function(e){
   if(e.which==122){
       e.preventDefault();//kill anything that browser may have assigned to it by default
       //do what ever you wish here :) 
       alert('F11 pressed');
       return false;
   }
});

but triggering it (NOT POSSIBLE)

But you will not prevent the browser from full screen :) ... Reson given is that , lets say I have full screened it somehow, and wish to toggle out of it using F11 but u are preventing me, I would have to restart PC, [computer illiterates] which poses security risk as you are preventing a user from doing something he is expecting to do, and they may think PC is broken or something :) so ...there you are.