www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 10-12-2008, 03:24 AM
    jzimmerlin jzimmerlin is offline
    Registered User
     
    Join Date: Oct 2008
    Posts: 5
    How To Disable Scrolling with the Arrow Keys in Firefox

    Hi,
    I am creating a Javascript web-app that will (hopefully) use the arrow keys to control some of the actions. The problem I'm running into is that when the up or down arrow keys are pressed, the page scrolls.

    I tried using the code below to stop the scrolling...
    function cancelEvent(e)
    {
    e = e ? e : window.event;
    if(e.stopPropagation)
    e.stopPropagation();
    if(e.preventDefault)
    e.preventDefault();
    e.cancelBubble = true;
    e.cancel = true;
    e.returnValue = false;
    return false;
    }

    but it didn't work. Any ideas? Suggestions? I appreciate your time and help.
    Reply With Quote
      #2  
    Old 10-12-2008, 03:39 AM
    felgall's Avatar
    felgall felgall is offline
    Computer Consultant
     
    Join Date: Mar 2005
    Location: Sydney, Australia
    Posts: 7,982
    Web pages can only use key combinations that the browser passes to them. The arrow keys have meaning to the browser and are therefore never passed to a web page.
    Reply With Quote
      #3  
    Old 10-12-2008, 01:22 PM
    jzimmerlin jzimmerlin is offline
    Registered User
     
    Join Date: Oct 2008
    Posts: 5
    That's odd... because in Firefox 3, the arrow keys being pressed triggers an event, and that event has a keyCode for the arrow keys. So it would seem the arrow keys do get passed to the browser when they're pressed... but is there any way to stop them from scrolling the page?
    Reply With Quote
      #4  
    Old 10-12-2008, 02:49 PM
    felgall's Avatar
    felgall felgall is offline
    Computer Consultant
     
    Join Date: Mar 2005
    Location: Sydney, Australia
    Posts: 7,982
    The browser gets them before the page and so processes them first. Interfering with the normal functioning of someone's browser is not a good idea.
    Reply With Quote
      #5  
    Old 10-12-2008, 03:11 PM
    jzimmerlin jzimmerlin is offline
    Registered User
     
    Join Date: Oct 2008
    Posts: 5
    Ah gotcha. And I hear ya on the part about not interfering with standard browser functionality.
    Reply With Quote
      #6  
    Old 10-12-2008, 03:21 PM
    scragar's Avatar
    scragar scragar is offline
    Some Random Guy
     
    Join Date: Jun 2003
    Location: here
    Posts: 4,461
    [quote=felgall;940290]Web pages can only use key combinations that the browser passes to them. The arrow keys have meaning to the browser and are therefore never passed to a web page.[quote]

    actualy I want you to test that theory:

    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>demo</title>
    <script type="text/javascript">
    // <![CDATA[
    window.onload = function(){
      var temp = document.createElement('p');
      temp.appendChild(document.createTextNode('lorem...'));
      for(var i = 0; i < 100; i++){
        document.body.appendChild( temp.cloneNode(true)  );
      }
    
      addEvent(document.body, 'keydown', keyDown);
      addEvent(window, 'keydown', keyDown);
    
    }
    
    
    function addEvent(obj, evType, fn){
      if(obj.addEventListener){
        obj.addEventListener(evType, fn, false);
        return true;
      }else if(obj.attachEvent){
        var r = obj.attachEvent("on"+evType, fn);
        return r;
      }
    }
    
    
    function keyDown(e){
      var ev = e||event;
      var key = ev.which||ev.keyCode;
      var esc = 0;
    
      switch(key){
      case 37: // left
      case 38: // up
      case 39: // right
      case 40: // down
         esc = 1;
      break;
      }
      if(esc && ev.preventDefault){
        ev.preventDefault();
      }
      return esc;
    }
    
    // ]]>
    
    </script>
    
    </head>
    <body>
    :p
    </body>
    </html>
    __________________
    If you are using PHP please use the [php] and [/php] forum tags for highlighting...
    The same applies to HTML and the forums [html][/html] tags.
    Reply With Quote
      #7  
    Old 11-12-2008, 09:05 AM
    boxbuilder boxbuilder is offline
    Registered User
     
    Join Date: Nov 2008
    Posts: 1
    hi scragar! great solution! have u also some for IE????
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:52 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.