10

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/

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
wong2
  • 34,358
  • 48
  • 134
  • 179
  • 7
    I don't think that it's possible to execute key commands by just triggering events programmatically. That would be insane. – Šime Vidas Jun 21 '11 at 15:54
  • 9
    let's trigger `CMD` + `Q` or `ALT` + `F4` ? All serious browsers will not execute that due to Javascript. – jAndy Jun 21 '11 at 16:01

4 Answers4

11

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.

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
Val
  • 17,336
  • 23
  • 95
  • 144
  • I do want to let the browser do what they assigned to it by default..that is turn to fullscreen mode – wong2 Jun 21 '11 at 16:11
  • Like I said you are asking to take control of some persons keyboard which is stupid, lets say i visit FB, fb then triggers, f11,and then f, and then u, n then triggers the comment button, basically all I had to do is login on fb, then fb would type in what ever they wanted on my profile ... do you understand ? – Val Jun 21 '11 at 16:18
10

You can not do this. The linked answer in that question provides a way with jQuery to simulate key-presses, within the jQuery event framework.

You simply can not trigger or fake keypresses. So the answer of this question is:

No, this is impossible

Evert
  • 93,428
  • 18
  • 118
  • 189
3

You won't be able to override the browser's built-in hotkeys from within a web page.

You might be able to do it in a browser extension, but that's would surely be serious overkill just to change the application's hotkeys.

In any case, why would you even want to override the standard keyboard shortcuts? I don't get that. They've been standard for a long time; most users will be familiar with them, and will find it very odd if they've been changed to something else.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • 1
    **why would you even want to override the standard keyboard shortcuts**: I think it's nicer to let the user click a button wrote "fullscreen" than alert to tell them: "Please press F11 key now" – wong2 Jun 21 '11 at 16:28
2

Don't look at is as a question of "How do I trigger F11?" - look at is as "How do I trigger or simulate full-screen?"

With older versions of IE you can open a new window straight into full-screen:

window.open(someURLorOther, '', 'fullscreen=yes, scrollbars=auto');

Or you can use window.open to open a new window of a specific size.

Or you can try to resize the current window to fill the screen:

moveTo(0,0);
resizeTo(screen.availWidth,screen.availHeight);

However just because you can doesn't mean you should. You should never resize the current window - this annoys practically everyone. Opening a new window to a size you choose is more reasonable, though if it's too big it can be annoying, and on a normal web page (where by "normal" I probably mean not some kind of browser-based data-entry app) it is nicer not to open new windows.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241