Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    button click should trigger key-event

    Hi,

    why does this code not work?

    Code:
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    
    <input id="input" value="Toggle Fullscreen" type="button">
    
    <script>
    
    $(function() {
    
    var e = jQuery.Event("keypress");
    e.which = 122;
    
    $("#input").trigger(e);}
    );
    
    </script>
    Key "F11" should be triggered when clicking on the button.

    Thanks in advance

    Ben
    Last edited by nebben; 10-16-2013 at 01:31 PM.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    If you read your script code (like line by line) it says

    - Execute the following code only after the DOM has been created
    - Create a new "keypress" event and assign it with the keycode 122
    - Trigger this event on the button with id "input"

    It doesn't say

    - Execute the following code only after the DOM has been created
    - Assign a click handler to the button with id "input", so that the following code will only be executed on button click
    - Create a new "keypress" event and assign it with the keycode 122
    - Trigger this event on the window or document element (this might(!) work, I have no experience with it)

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,968
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Why are you using a very very old jQuery version, 1.3.2? A lot of things have changed and been fixed since then. The latest is 1.10.2.

  • #4
    New Coder
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Code:
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    
    <input id="target" value="Toggle Fullscreen" type="button">
    
    <script>
    
    $(function() {
    
    $( "#target" ).click(function() {
    trigger(e); 
    });
    
    var e = jQuery.Event( "keypress", { keyCode: 122 } );
    
    });
    
    </script>

    The click handler works. Not sure about the rest.

    How to make it a document or window event, please?

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Try
    Code:
    $(document).trigger(customevent);
    // or
    $(window).trigger(customevent);
    
    var customevent = jQuery.Event("keypress", {keyCode: 122} );

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    By the way ... for security reasons (sandbox principle) this might not work because you are trying to "control" the user's browser. But if you just want to go fullscreen in the first place, you could try this
    Code:
    function requestFullScreen() {
        var element = document.body;
        // Supports most browsers and their versions.
        var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
    
        if (requestMethod) { // Native full screen.
            requestMethod.call(element);
        } else if (typeof window.ActiveXObject !== "undefined") { // Older IE.
            var wscript = new ActiveXObject("WScript.Shell");
            if (wscript !== null) {
                wscript.SendKeys("{F11}");
            }
        }
    }
    This will ask the user if it is OK for him to go fullscreen, though. Only older IE will simulate F11 through ActiveX. There will also be a notification for the user in that case.

  • #7
    New Coder
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    thank you, it works but it does not help me.

    Because any clicks in fullscreen mode on other pages on the site, where it is implemented, breaks the fullscreen.

    The manual F11 Fullscreen is more stable in this respect.

    I think this cant be changed easily.

    Also this method is only an enter-into and not an exit-from.
    Last edited by nebben; 10-19-2013 at 04:19 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •