Is it possible to set up hotkeys using onkeypress? I know it can be
done with the usual alphanumeric keys, but what about function keys?
or using ctrl/alt combinations?
PointedEars provied this link to a post titled "Re: mouseless
navigation" the link is:
<
http://www.w3.org/TR/html4/interact/forms.html#adef-accesskey>
I copied the following from the article:
In this example, we assign an access key to a link defined by the A
element. Typing this access key takes the user to another document, in
this case, a table of contents.
<P><A accesskey="C"
rel="contents"
href="
http://someplace.com/specification/contents.html">
Table of Contents</A>
The invocation of access keys depends on the underlying system. For
instance, on machines running MS Windows, one generally has to press
the "alt" key in addition to the access key. On Apple systems, one
generally has to press the "cmd" key in addition to the access key.
I wrote up a Javascript program for detecting key presses too. It has
a Netscape path.
The function is written so it could be invoked by setting the
document.onkeypress global variable.
Looks like you can either set the event handler via the
statement below or in the onkeypress event handler of the
html body statement. If you use the statement below, IE
will not be passing the event variable to the function
processKey and you will have to preference window to
the event variables for the IE portion of the code.
document.onkeypress = processKey;
Robert
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>onkeypress example</title>
<SCRIPT type="text/javascript">
function processKey(event)
{
if (typeof event == "object" &&
typeof event.which == "number")
{
// Netscape style event
alert("Netscape: event.which = " + typeof event.which +
" " + event.which + " letter is " +
String.fromCharCode(event.which));
alert("event.shiftKey = " + event.shiftKey +
" event.ctrlKey = " + event.ctrlKey +
" event.altKey = " + event.altKey +
" event.metaKey = " + event.metaKey);
//control-y goes to yahoo
if (event.which == 121 && event.ctrlKey == true)
{
location.replace("
http://www.yahoo.com");
return false;
}
}
else
{
// We assume IE
/* ***
// Doing an alert seems to confuse IE.
// The event will be changed after the alert.
alert("IE: window.event.keyCode = " +
window.event.keyCode +
" letter is " +
String.fromCharCode(window.event.keyCode) +
"\n" +
" window.event.shiftKey = " + window.event.shiftKey +
" window.event.ctrlKey = " + window.event.ctrlKey +
" window.event.altKey = " + window.event.altKey +
"\n" +
" ... Warning: " +
"IE event handle is confused after this alert");
*** */
//control-y goes to yahoo
if (window.event.keyCode == 25 )
{
location.replace("
http://www.yahoo.com");
window.event.returnValue = false;
return;
}
}
return true;
}
</SCRIPT>
</head>
<body onkeypress='return processKey(event);'>
<p>Lets look for a key press.</p>
<form>
<input type=text size=20>
</form>
</body>