Button and Key Codes
Internet Explorer 4.0x features a property that indicates what mouse button was pressed (for a mouse event), and another property that indicates what key was pressed (for a keyboard event). In Navigator 4.0x, one propery (
which) serves both cases. The following example demonstrates:
First try it our by entering characters in the text box and clicking the button. Watch the status bar for the results.
This example demonstrates many important issues. Notice the conditional statement in the
onclick event handler script. Since the
if statement makes sure the
codes() function isn't executed unless the user is running a fourth-generation browser (or above). The specification of the
If you have access to both Internet Explorer 4.0x and Navigator 4.0x, you probably noticed that the code for the primary mouse button differs. In Internet Explorer 4.0x the value is 0, whereas in Navigator 4.0x it is 1. Furthermore, browsers don't let you trap for this user event. Right clicks in Windows 95 or NT, for example, display a context-sensitive pop-up menu, without passing the event to the page. Therefore, you normally won't check what mouse button the user pressed.
There are several other minor differences between the browsers. For instance, the Backspace key generates a
click event on Navigator, but not on Internet Explorer. Also note that several key values differ with the browser. The
String.fromCharCode() method should fix this up because it returns the actual character pressed, but instead it introduces new problems. This method returns incorrect characters on the Macintosh version of Internet Explorer 4.0. Since the only key codes that differ are uncommon non-alphanumeric ones, it is best to evaluate the code rather than the character itself, as returned by the
Created: January 13, 1998
Revised: January 13, 1998