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
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

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


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger

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

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