JavaScript
  Home arrow JavaScript arrow Page 7 - Understanding The JavaScript Event Mod...
FaxWave - Free Trial.
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 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

Understanding The JavaScript Event Model (part 2)
By: Team Melonfire, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 29
    2002-07-10

    Table of Contents:
  • Understanding The JavaScript Event Model (part 2)
  • Back To Basics
  • How's The Weather Up There?
  • X Marks The Spot
  • Enter The Stalker
  • Of Keys And Clicks
  • A Few Modifications
  • Tonight's Menu
  • Reducing The Crime Rate
  • Endgame

  • 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
     
     
    Iron Speed
     
    ADVERTISEMENT

    Dell PowerEdge Servers

    Understanding The JavaScript Event Model (part 2) - A Few Modifications
    (Page 7 of 10 )

    You can also track which of the so-called modifier keys are pressed in combination with a regular key on the keyboard (the modifier keys are the Alt, Ctrl and Shift keys). The "modifiers" property returns an integer or constant indicating which modifier key(s) were held down during the event.

    Here's an example:
    <html>
    <head>
    <script language="JavaScript">
    function detectKey(mykey) 
    {
    if (mykey.modifiers == Event.ALT_MASK)
    {
    alert ("You just pressed the Alt key");
    }
    else if (mykey.modifiers == Event.CONTROL_MASK)
    {
    alert ("You just pressed the Ctrl key");
    }
    else if (mykey.modifiers == Event.SHIFT_MASK)
    {
    alert ("You just pressed the Shift key");
    }
    else 
    {
    alert ("Nope, that wasn't a modifier key");
    }
    }
    document.onkeydown = detectKey;
    </script>
    </head>
    <body>
    </body>
    </html>
    In this case, the detectKey() function checks to see which modifier key was pressed, and displays a message appropriately. Note, however, that the script above only works in Netscape Navigator 4.x - later versions of the browser seem to have a problem with it.

    Here's another example, this one demonstrating how the same thing works in Internet Explorer.
    <html>
    <head>
    <script language="JavaScript">
    function detectKey() 
    {
    if (event.altKey)
    {
    alert ("You just pressed the Alt key");
    }
    else if (event.ctrlKey)
    {
    alert ("You just pressed the Ctrl key");
    }
    else if (event.shiftKey)
    {
    alert ("You just pressed the Shift key");
    }
    else 
    {
    alert ("Nope, that wasn't a modifier key");
    }
    }
    document.onkeydown = detectKey;
    </script>
    </head>
    <body>
    </body>
    </html>

    More JavaScript Articles
    More By Team Melonfire, (c) Melonfire


     

       

    JAVASCRIPT ARTICLES

    - Getting Attention with Interactive Effects
    - Interacting with Tooltips and Previews
    - Just-in-Time Information and Ajax
    - Interactive Effects
    - Using Cookies With JavaScript
    - Understanding the JavaScript RegExp Object
    - Controlling Browser Properties with JavaScri...
    - Using Timers in JavaScript
    - Form Validation with JavaScript
    - JavaScript Exception Handling
    - Stringing Things Along
    - Understanding The JavaScript Event Model (pa...
    - Understanding The JavaScript Event Model (pa...
    - An Object Lesson In JavaScript

    Iron Speed



    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway