JavaScript
  Home arrow JavaScript arrow Page 5 - Handling events with the DOM - Part III
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Handling events with the DOM - Part III
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2005-05-03

    Table of Contents:
  • Handling events with the DOM - Part III
  • Another creature in an object-based world: the Event object
  • Determining the type of event: the "type" property
  • Targeting objects: determining the target of the event
  • Focusing on keys: determining which key has been pressed
  • The x-y game: determining mouse coordinates
  • Buttons from the bottom: detecting mouse buttons

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Handling events with the DOM - Part III - Focusing on keys: determining which key has been pressed


    (Page 5 of 7 )

     

    To determine which key the user has pressed, usually we use the "keyCode" property. However, Mozilla and Netscape expose the "which" property to find out the key pressed. First, we should get the ASCII code for the key, and then turn it into the corresponding character. Taking advantage of the "String.fromCharCode()" method, this is quite easy to do. The function to obtain the key pressed is as follows:

     

    function detectKey(e){

        var code;

        if(!e) var e=window.event;

        if(e.keyCode){code=e.keyCode;}

        else if(e.which){code=e.which;}

        alert(String.fromCharCode(code));

    }

     

    And we might call it with the statement:

     

    document.onkeypress=detectKey;

     

    Indeed, key detection is somewhat annoying sometimes, since “keypress”, "keyup" and "keydown" event handlers are triggered as long as the user keeps the key pressed. In Netscape, the ASCII value of the key is given by the "charCode" property on "keypress" events, and it’s present in the "keyCode" property for "keydown" and "keyup" events, while IE stores the Unicode value of the key in the event "keyCode" property for all three key events.

     

    Both the " keydown" and " keyup" events will fire when any modifier key is pressed, such as ALT, CTRL and SHIFT. The "keypress" event can be used instead to capture combinations such as SHIFT-A. For keeping things simple, it’s recommended to use only one event handler at once.

     

    It’s time to move forward and learn how to detect the mouse coordinates according to the event generated.

     

    More JavaScript Articles
    More By Alejandro Gervasio


       · This third part of the series shows several examples to tackle the most common...
       · Very helpful. You are an excelltent technical writer. Please submit more!
       · Hi, I'm glad to know that you've found this series helpful. I'm writing more...
       · It helped me finally get a popup menu working.Also made me aware of a completely...
       · Very glad to see that the article has helped you out in the developing...
     

    JAVASCRIPT ARTICLES

    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT