Key and Character Codes vs. Event Types : Key Event « Event « JavaScript DHTML

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Dojo toolkit
7. Event
8. Event onMethod
9. Form Control
10. GUI Components
11. HTML
12. Javascript Collections
13. Javascript Objects
14. jQuery
15. Language Basics
16. Node Operation
17. Object Oriented
18. Page Components
19. Security
20. Style Layout
21. Table
22. Utilities
23. Window Browser
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorial
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
JavaScript DHTML » Event » Key Event 
Key and Character Codes vs. Event Types

 <!-- ***********************************************************
Example 6-1
"Dynamic HTML:The Definitive Reference"
2nd Edition
by Danny Goodman
Published by O'Reilly & Associates  ISBN 0-596-00316-1
http://www.oreilly.com
Copyright 2002 Danny Goodman.  All Rights Reserved.
************************************************************ -->
<html>
<head>
<title>Keyboard Events and Codes</title>
<style type="text/css">
body {font-family:Arial, sans-serif}
h1 {text-align:right}
td {text-align:center}
</style>
<script language="JavaScript" type="text/javascript">
// array of table cell ids
var tCells = ["downKey""pressKey""upKey""downChar""pressChar""upChar""keyTarget""character"];

// clear table cells for each key down event
function clearCells() {
    for (var i = 0; i < tCells.length; i++) {
      document.getElementById(tCells[i]).innerHTML = "&mdash;";
    }
}

// display target node's node name
function showTarget(evt) {
    var node = (evt.target? evt.target : ((evt.srcElement? evt.srcElement : null);
    if (node) {
        document.getElementById("keyTarget").innerHTML = node.nodeName;
    }
}

// decipher key down codes
function showDown(evt) {
    clearCells();
    evt = (evt? evt : ((event? event : null);
    if (evt) {
        document.getElementById("downKey").innerHTML = evt.keyCode;
        if (evt.charCode) {
            document.getElementById("downChar").innerHTML = evt.charCode;
        }
        showTarget(evt);
    }
}

// decipher key press codes
function showPress(evt) {
    evt = (evt? evt : ((event? event : null);
    if (evt) {
        document.getElementById("pressKey").innerHTML = evt.keyCode;
        if (evt.charCode) {
            document.getElementById("pressChar").innerHTML = evt.charCode;
        }
        showTarget(evt);
        var charCode = (evt.charCode? evt.charCode : evt.keyCode;
        // use String method to convert back to character
        document.getElementById("character").innerHTML = String.fromCharCode(charCode);
    }
}

// decipher key up codes
function showUp(evt) {
    evt = (evt? evt : ((event? event : null);
    if (evt) {
        document.getElementById("upKey").innerHTML = evt.keyCode;
        if (evt.charCode) {
            document.getElementById("upChar").innerHTML = evt.charCode;
        }
        showTarget(evt);
    }
}

// set page-wide event listeners
document.onkeydown = showDown;
document.onkeypress = showPress;
document.onkeyup = showUp;
</script>
</head>
<body>
<h1>Key and Character Codes vs. Event Types</h1> 
<hr>
<p>Enter some text with uppercase and lowercase letters:<br>
<form>
<input type="text" id="entry" size="60" 
       onkeydown="showDown(event)" 
       onkeypress="showPress(event)" 
       onkeyup="showUp(event)">
</textarea></p>
</form>
<table border="2" cellpadding="5" cellspacing="5">
<caption>Keyboard Event Properties</caption>
<tr><th>Data</th><th>keydown</th><th>keypress</th><th>keyup</th></tr>
<tr><td>keyCode</td>
    <td id="downKey">&mdash;</td>
    <td id="pressKey">&mdash;</td>
    <td id="upKey">&mdash;</td>
</tr>
<tr><td>charCode</td>
    <td id="downChar">&mdash;</td>
    <td id="pressChar">&mdash;</td>
    <td id="upChar">&mdash;</td>
</tr>
<tr><td>Target</td>
    <td id="keyTarget" colspan="3">&mdash;</td>
</tr>
<tr><td>Character</td>
    <td id="character" colspan="3">&mdash;</td>
</tr>
</table>
</body>
</html>


           
       
Related examples in the same category
1. 'ctrlLeft' Example
2. 'ctrlKey' Example
3. 'shiftKey' Example
4. 'shiftLeft' Example
5. 'keyCode' Example
6. Catches and manages the keyboard's events
7. Shift key pressed?
8. Unicode of the key pressed
9. Keyboard Event Handler Laboratory
10. Displaying keyCode Property Values
11. Displaying charCode and keyCode Property Values
12. Checking Events for Modifier Keys
13.  Checking Events for Key and Mouse Button Pressed
14. Handle arrow key, control, alt
w__w_w.ja_v__a__2s__._c__o_m | |Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.