SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Evangelist
    Join Date
    Sep 2003
    Location
    San Diego, CA
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Internet Button on Keyboard causing trouble!

    I have an internet key on my keyboard, and I'm sure others do too. When I push it, it opens Internet Explorer to my homepage. If I am already in internet explorer, and the page is focused, and I push the internet button, then it changes the location of the page to my homepage.

    I want to prevent it from doing this for my users whom visit my site. I have a popup, and while in that popup, it is likely they push that button, or start internet explorer. Is there anyway to detect this, and either block it, or blur the page before it takes effect in Javascript?

    It DOES NOT happen if I push START>Internet Explorer since when I click Start, it blurs the webpage.


    Thanks to all that can give me any help!
    Last edited by kgh; Dec 1, 2004 at 14:09.

  2. #2
    SitePoint Evangelist
    Join Date
    Sep 2003
    Location
    San Diego, CA
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can anyone help me?

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I played around with this for a while and the short answer is no, it doesn't look like the special keys can be disabled.

    I made a script that sort of works though, if the user presses the internet key a dialog asks them if they want to reload the current page in a new window, and then closes the existing window, leaving them where they were. It would probably wreak havoc with stuff like session variables and user state and so on, but here it is:

    Code:
    <html>
    <head>
    <script language="JavaScript">
    function KillIt()
    {
    	if( !confirm( "You pressed the INTERNET key on your fancy keyboard, our site will disappear.  Press CANCEL to reload it." ) ) {
    		window.open("kbthing.html");
    		window.close();
    	}
    }
    </script>
    </head>
    <body onkeydown = 'if(event.keyCode == 172)KillIt();'>
    </body>
    </html>
    Keep in mind that keycode 172 works for MY COMPAQ keyboard, heaven knows what the code is for generic keyboard X.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I played around with this for a while and the short answer is no, it doesn't look like the special keys can be disabled.
    Why is that? If it's got a keycode, that's all you should need.

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
    window.onload=function()
    {
    	document.onkeypress = check_keyCode;
    };
    
    function check_keyCode(e)
    {
    	if(!e) var e = window.event; //for IE
    	var code;
    	if(e.which) code = e.which; //FF1.0 and NN4
    	if(e.keyCode) code = e.keyCode //IE
    
    	//alert(code)  //you can uncomment this line to see what code fires when you press your special key
    	if (code==172)  //or whatever keycode fires when that internet key is pressed
    	{
    		return false;
    	}
    }
    
    // End hiding -->
    </script>
    **guaranteed 100% untested**

  6. #6
    SitePoint Evangelist
    Join Date
    Sep 2003
    Location
    San Diego, CA
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that I'll test your suggestions and see what happens!

    Thanks a ton!

  7. #7
    SitePoint Evangelist
    Join Date
    Sep 2003
    Location
    San Diego, CA
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Blocking them like that doesnt work, I tryed your example, and made my own. The first one does work, but if I could have it change the target of the opening window... because since it would open it in a popup window, their internet page would be very small. If only I could BLUR the page BEFORE they press it...

    ...I need a psychic page!....

    I have tried onkeydown=window.blur(); but that doesnt do it before since the button fires as soon as you push it down (not a press)...

  8. #8
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK this seems to work:

    Code:
    <html>
    <head>
    <script language="JavaScript">
    function Check()
    {
    	if(event.keyCode == 172) {
    		window.onbeforeunload = function() { return "You pressed the INTERNET key"; };
    	} else {
    		window.onbeforeunload = null;
    	}
    }
    </script>
    </head>
    <body onkeydown='Check();'>
    </body>
    </html>
    Any ideas on how to make that better?

  9. #9
    SitePoint Evangelist
    Join Date
    Sep 2003
    Location
    San Diego, CA
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all of the help guys! What I did was when they press it, it opens a window with the URL of about:home so they do get their window open ;-)

    Then it opens the same page with all of the variables in the querystring. The page then converts the querystring.

    It then closes the original page.

    I went of the first idea of jimfraser, thanks!

  10. #10
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried onkeydown=window.blur(); but that doesnt do it before since the button fires as soon as you push it down (not a press)...
    Did you try onkeypress? onkeydown and onkeypress work differently.

    What's the keycode when you push your internet button?

  11. #11
    SitePoint Evangelist
    Join Date
    Sep 2003
    Location
    San Diego, CA
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The keycode is 172, but nothing happened when I used onkeypress. I tried to have it alert the keycode onkeypress, and it alerted it for other buttons on my keyboard, but nothing for my internet key - no alert, and it took the page to my homepage.

  12. #12
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do you know the keycode is 172 then?

  13. #13
    SitePoint Evangelist
    Join Date
    Sep 2003
    Location
    San Diego, CA
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used onkeydown, and that works, but I can't block it by using onkeypress...


Bookmarks

Posting Permissions

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