spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / dhtml / diner / keypress
DHTML Diner Logo

This is a DHTML cross-browser technique.
The in-page examples will only work in Navigator 4 and Explorer 4, all platforms.

This technique was developed in response to a query from Ian Feldman.

In longer script listings, cross-browser code is blue, Navigator-specific code is red, and Explorer code is green.

"Hot" keys for this page:

N - goto Next article page
2 - goto 2nd article page
3 - goto 3rd article page
4 - goto 4th article page

Statements used:

switch (whichKey) {
 case "n":
   location.href =
    "keypress2.html";
   break;
 case "2":
   location.href =
    "keypress2.html";
   break;
 case "3":
   location.href =
    "keypress3.html";
   break;
 case "4":
   location.href =
    "keypress4.html";
   break;
 default:
   break;
}
Developer News
Linux 2.6.26 Opens Up to Debugging
Borland Launches ALM Management Tools
Nominations Open for CEO Vision Awards

Using the Keypress Event

Both version 4 browsers have exposed many new client events to scripting. In DHTML Lab, we have discussed and used these new events in relation to the techniques we have developed. For example, in Column 3, Dynamic Images, we discussed the click and mousedown events; in Columns 6 and 7, Drag & Drop, we based our routines on the mousedown, mousemove and mouseup events, and looked at the IE4-specific onselectstart event.

In Column 3, we also provided a table of event properties for the two browsers. It is reproduced as an addendum to the present discussion as well.

For a complete overview of events exposed by version 4 browsers, see Doc JavaScript's event columns.

In the present article, we will concentrate on capturing and using the keypress event.

Live Examples: All pages in this article have live examples for version 4 browsers. The keys that are assigned tasks are listed in the left column, as is the relevant part of the script (switch) that contains the statements executed upon keypress.

Capturing the keypress

Navigator 4 documents an onkeypress handler for the document, Image, Link and Textarea objects. Although not documented, the window object can also be given an onkeypress handler.

Explorer 4 exposes the keypress event for all elements and the document object. The window object cannot process keypresses, although the BODY tag can.

More often than not, our page will be scripted to process a keypress regardless of the element-in-focus. For this, we need to use the cross-browser document object. In Navigator, we need to capture the keypress event for the document object, before defining the event handler.

Therefore, if we want all keypresses to be handled by a function called doKey(), we need the following code:

<SCRIPT LANGUAGE="JavaScript1.2">
<!--
IE4 = (document.all);
NS4 = (document.layers);

if (NS4) document.captureEvents(Event.KEYPRESS);
document.onkeypress = doKey;
//-->
</SCRIPT>

Now, let's look at how doKey() processes the keypress.



Produced by Peter Belesis and



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

webref The latest from WebReference.com Browse >
How to Create an Ajax Autocomplete Text Field: Part 10 · Adding Client Capabilities to Server Controls Using the ASP.NET AJAX Control Toolkit · JavaScript and HTML Tricks
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Palm's Treo Presses On (iPhone Who?) · Toshiba Extends Notebook Line with 5400 and 7200-RPM Drives · Epson Kicks Color Cost to the Curb

All Rights Reserved. Legal Notices.
Created: June 05, 1998
Revised: June 05, 1998

URL: http://www.webreference.com/dhtml/diner/keypress/