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.

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

Press any underlined letter to toggle related checkbox value:

Favorite Reading:
 Mystery
 Science Fiction
 Romance
 Adventure
 Comics
 Cereal Boxes

Statements used:

fK = document.fmKeys;
switch (whichKey) {
 case "m":
   fK.mys.checked =
    !fK.mys.checked;
   break;
 case "s":
   fK.sci.checked =
    !fK.sci.checked;
   break;
 case "r":
   fK.rom.checked =
    !fK.rom.checked;
   break;
 case "a":
   fK.adv.checked =
    !fK.adv.checked;
   break;
 case "c":
   fK.com.checked =
    !fK.com.checked;
   break;
 case "b":
   fK.cer.checked =
    !fK.cer.checked;
   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 (3)

We introduced the JavaScript 1.2 switch statement in column 19. It is particularly suited to keypress handling, since we can simply list our "hot" keys in a lookup-table-like fashion, and use the default: label for any non-"hot" key processing.

function doKey(e) {
  whichASC = (NS4) ? e.which : event.keyCode;
  whichKey = String.fromCharCode(whichASC).toLowerCase();

  switch (whichKey) {
    case "a":
      doA();
      break;
    case "b":
      doB();
      break;
    case "1":
      location.href = "page1.html";
      break;
    case "2":
      location.href = "page2.html";
      break;
    case "y":
      alert('You answered "Yes"')
      break;
    case "n":
      alert('You answered "No"')
      break;
    default:
      break;
  }
}

In the example above, we have defined statements to be executed whenever a, b, y, and n (regardless of case) and 1 and 2 are pressed.

Notice that upon keypress, we may choose to execute a function, navigate to a new page, or display an alert window. Any normal JavaScript statements may be included. In this page's live example (left column), keypresses toggle the state of form checkboxes.

On the next page, a quick review of the code necessary for using the keypress event.



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 >
Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS · Sending an HTML and Plain Text E-newsletter with ASP.NET, Part 2
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Workers Say Telework Is More Productive, Bosses Not So Sure · Kingston Debuts Power-Saving Memory Upgrades · Social Networking is King: Facebook Edges Google

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

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