spacer

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

home / experts / javascript / column9


New Keyboard Events

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

JavaScript 1.2 introduces several new keyboard events:

  • keydown
  • keyup
  • keypress

Rather than having a single event for a key press, there are three different events to choose from. A keypress event occurs when you press or hold down a key. This event is actually a combination of two separate actions: the key reaching the bottom of its physical travel (keydown), and the instant at which the key starts its travel upward (keyup).

Thanks to the event.which property, you can even find out what key was pressed:

<SCRIPT LANGUAGE="JavaScript">
<!--

function processKeypresses(e) {
  var whichASC = e.which; // key's ASCII code
  var whichChar = String.fromCharCode(whichASC); // key's character
  if (whichChar == 'A' || whichChar == 'a')
    window.scrollBy(0, 10); // scroll down
  else if (whichChar == 'Z' || whichChar == 'z')
    window.scrollBy(0, -10); // scroll up
  return false; // cancel event otherwise
}

if (document.captureEvents)
  document.captureEvents(Event.KEYPRESS);
document.onkeypress = processKeypresses;

// -->
</SCRIPT>

Notice the use of the String.fromCharCode() method to convert an integer to a character with that ASCII value. This is required because the which property holds the ASCII value of the key that was pressed.

To experiment with this script, click the "a" and "z" keys. The window should scroll up and down, respectively. Now hold down one of these keys and the window will scroll smoothly. The function always returns false. This cancels all keypress events, so you can't use the keyboard to enter text in a form field, for example.

Remember that this script works only with Netscape Navigator 4.0x and above. It will not work on older versions of Netscape Navigator, and will generate an error on Microsoft Internet Explorer 4.0x.

For a good demonstration of keyboard events, check out Peter Belesis' column on "Using the Keypress Event."

http://www.internet.com


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


Created: December 16, 1997
Revised: December 16, 1997

URL: http://www.webreference.com/js/column9/newkeyboardevents.html