November 24, 1999 - Hot Keys | WebReference

November 24, 1999 - Hot Keys

Yehuda Shiran November 24, 1999
Hot Keys
Tips: November 1999

Yehuda Shiran, Ph.D.
Doc JavaScript

We'll use the term hot key to describe a key that executes a specific action on a Web page. For example, we could write a script that takes the user to Microsoft's front page whenever the user presses the key "M" on the page. Take a look at the following script::

<SCRIPT LANGUAGE="JavaScript1.2">
<!--
var NS = (window.Event) ? 1 : 0;
function checkKey(e) {
  var code = (NS) ? e.which : event.keyCode;
  var key = String.fromCharCode(code);
  for (var i = 0; i < ar.length; i++) {
    if (key == ar[i].key) location.href = ar[i].url;
  }
}
function hotKey(key, url) {
  this.key = key;
  this.url = url;
}
if (NS) document.captureEvents(Event.KEYPRESS)
document.onkeypress = checkKey;
var ar = new Array();
ar[ar.length] = new hotKey("h", "https://www.docjs.com/");
ar[ar.length] = new hotKey("m", "https://www.microsoft.com/");
ar[ar.length] = new hotKey("n", "https://www.netscape.com/");
// -->
</SCRIPT>

As you can see, it is possible to assign as many hot keys as you wish. Each key can be associated with a different Web page. Note that your hot keys will operate on Internet Explorer 4.0x, Navigator 4.0x, and above. Older browsers will ignore them. For more information about keyboard events, refer to Column 9, The Navigator Event Model, Column 10, The Internet Explorer Event Model, and Column 11, The Cross-Browser Event Model.