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.

Question:
Do you have a quick technique you would like to see covered in DHTML Diner?
Answer by pressing
Y or N.

Statements used:

switch (whichKey) {
 case "y":
  mess =
"Great, send it along!"
  break;
 case "n":
  mess =
"Oh, come on. You must!"
  break;
 default:
  mess =
"You must press Y or N!"
}

alert(mess)
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 (4)

Step-by-Step

1. Include this script in your page:

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

if (NS4) document.captureEvents(Event.KEYPRESS);
document.onkeypress = doKey;

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

  switch (whichKey) {
    case "a":
      ...statements to execute if "a" is pressed...
      break;
    case "b":
      ...statements to execute if "b" is pressed...
      break;
    case "1":
      ...statements to execute if "1" is pressed...
      break;
    case "2":
      ...statements to execute if "2" is pressed...
      break;
    case "y":
      ...statements to execute if "y" is pressed...
      break;
    case "n":
      ...statements to execute if "n" is pressed...
      break;
    .
    . other alphanumeric "hot" keys
    .
    default:
    ...statements to execute if no "hot" key is pressed...
    or include the break statement:
    break;
  }
}

//-->
</SCRIPT>

2. Define your "hot" keys as strings in the switch statement's case labels, as in the example above.

3. In each case label, place the appropriate JavaScript statements to be executed when that key is pressed. End statement list with a break.

4. Optionally, define a default behavior in case the key pressed is not "hot", in the default: label.

5. If the hot keys are case-sensitive, omit the toLowerCase() method when declaring whichKey, and define different statements for upper and lower case characters.



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 >
Installing and Using Meeplace, the Business Review CMS · Sending an HTML and Plain Text E-newsletter with ASP.NET, Part 2 · Create Multilingual Web Sites with Windows Unicode Fonts
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MySql View Technique for Grouping Crosstab Column Data · Why You Need a Mobile Web Site · Tame Web Marketing with Social Media Management

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

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