0

i'm planning on activating a keypress after page load, can you help me where i'm doing wrong. thanks

here is my code

<script type="text/javascript">
window.onload = function(event) {
var e = $.Event('keypress');
e.which = 122; // Character 'F11'
$('item').trigger(e);
}

</script>
Álvaro Touzón
  • 1,247
  • 1
  • 8
  • 21
Anna Mae
  • 95
  • 1
  • 10
  • I'm pretty sure that browsers will block activation of full-screen if it's not a direct user input, as a mouse click or an actual keypress. Coding a keypress will be blocked, if I'm not mistaken. – Martin Johansson Oct 16 '17 at 07:49
  • Possible duplicate of [F11 key event fire on onload using javascript](https://stackoverflow.com/questions/24134194/f11-key-event-fire-on-onload-using-javascript) – David R Oct 16 '17 at 07:49

2 Answers2

0

Javascript Events keycodes

for(let elem of document.querySelectorAll('*')) {
    elem.addEventListener("keydown", KeyDownFun, true);
}

function KeyDownFun( KeyboardEvent ) {
    var keyDownCode = KeyboardEvent.keyCode || KeyboardEvent.which;
    console.log("Key Event Code : ", keyDownCode);
    if( keyDownCode == 122 ) { // F11 - 122, Ctrl - 17
        $('item').trigger( KeyboardEvent );
    }
}

You can stop event propagation for some key codes also

document.addEventListener("keydown", function(event) {
  var keyDownCode = event.keyCode || event.which;
  console.log("Key Event Code : ", keyDownCode);
  if( keyDownCode == 122 ) {
    event.stopImmediatePropagation();
    event.preventDefault();
    event.stopPropagation();

    //$('item').trigger( KeyboardEvent );
  }
});

@see

Yash
  • 9,250
  • 2
  • 69
  • 74
0

Note: Fullscreen requests need to be called from within an event handler or otherwise they will be denied. Firefox Fullscreen API

This means that you need an actual event like a mouse click or keypress in order for the browser to aceept your request for Fullscreen. Events triggered by code is to my knowledge also rejected for securityreasons. You didn't specify why you need this, if it's a site that people visit or if it's a site that is to be shown on a specific screen. In the second scenario you could use a browser plugin that will start the browser in fullscreen at startup. I've used mFull for Firefox on screens for display which has worked well, but there are many others.

Martin Johansson
  • 773
  • 1
  • 11
  • 27