Sams Teach Yourself JavaScript in 24 Hours Free Open Book

Sams Teach Yourself JavaScript in 24 Hours

Previous Page
Next Page

Using Keyboard Events

JavaScript can also detect keyboard actions. The main event handler for this purpose is onKeyPress, which occurs when a key is pressed and released, or held down. As with mouse buttons, you can detect the down and up parts of the keypress with the onKeyDown and onKeyUp event handlers.

Of course, you might find it useful to know which key the user pressed. You can find this out with the event object, which is sent to your event handler when the event occurs. In Netscape and Firefox, the event.which property stores the ASCII character code for the key that was pressed. In Internet Explorer, event.keyCode serves the same purpose.

By the Way

ASCII (American Standard Code for Information Interchange) is the standard numeric code used by most computers to represent characters. It assigns the numbers 0128 to various charactersfor example, the capital letters A through Z are ASCII values 65 to 90.


Displaying Typed Characters

If you'd rather deal with actual characters than key codes, you can use the fromCharCode string method to convert them. This method converts a numeric ASCII code to its corresponding string character. For example, the following statement converts the event.which property to a character and stores it in the key variable:

Key = String.fromCharCode(event.which);

Because different browsers have different ways of returning the key code, displaying keys browser independently is a bit harder. However, you can create a script that displays keys for either browser. The following function will display each key as it is typed:

function DisplayKey(e) {
   // which key was pressed?
   if (e.keyCode) keycode=e.keyCode;
      else keycode=e.which;
   character=String.fromCharCode(keycode);
   // find the object for the destination paragraph
   k = document.getElementById("keys");
   // add the character to the paragraph
   k.innerHTML += character;
}

The DisplayKey() function receives the event object from the event handler and stores it in the variable e. It checks whether the e.keyCode property exists, and stores it in the keycode variable if present. Otherwise, it assumes the browser is Netscape or Firefox and assigns keycode to the e.which property.

The remaining lines of the function convert the key code to a character and add it to the paragraph in the document with the id attribute keys. Listing 9.3 shows a complete example using this function.

By the Way

The final lines in the DisplayKey() function use the getElementById() function and the innerHTML attribute to display the keys you type within a paragraph on the page. This technique is explained in Hour 13, "Using the W3C DOM."


Listing 9.3. Displaying Typed Characters

<html>
<head>
<title>Displaying Keypresses</title>
<script language="javascript" type="text/javascript">
   function DisplayKey(e) {
      // which key was pressed?
      if (e.keyCode) keycode=e.keyCode;
         else keycode=e.which;
      character=String.fromCharCode(keycode);
      // find the object for the destination paragraph
      k = document.getElementById("keys");
      // add the character to the paragraph
      k.innerHTML += character;
   }
</script>
</head>
<body onKeyPress="DisplayKey(event);">
<h1>Displaying Typed Characters</h1>
<p>This document includes a simple script that displays the keys
you type in the paragraph below. Type a few keys and try it. </p>
<p id="keys">
</p>
</body>
</html>

When you load this example into either Netscape or Internet Explorer, you can type and see the characters you've typed appear in a paragraph of the document. Figure 9.2 shows this example in action in Firefox.

Figure 9.2. Firefox displays the keypress example.


Previous Page
Next Page
Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y]


     Main Menu
Sams Teach Yourself JavaScript in 24 Hours
Table of Contents
Copyright
About the Author
Acknowledgments
Part I: Introducing the Concept of Web scripting and the JavaScript Language
Part II: Learning JavaScript Basics
Part III: Learning More About the DOM
Hour 9. Responding to Events
Understanding Event Handlers
Using Mouse Events
Using Keyboard Events
Using the onLoad and onUnload Events
Summary
Q&A
Quiz Questions
Quiz Answers
Exercises
Hour 10. Using Windows and Frames
Hour 11. Getting Data with Forms
Hour 12. Working with Style Sheets
Hour 13. Using the W3C DOM
Hour 14. Using Advanced DOM Features
Part IV: Working with Advanced JavaScript Features
Part V: Building Multimedia Applications with JavaScript
Part VI: Creating Complex Scripts
Part VII: Appendixes
Index


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele