Setting up hotkeys using onKeyPress

H

Hasan Ammar

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? Does anybody have a tutorial/guide?
 
E

Evertjan.

Hasan Ammar wrote on 18 aug 2004 in comp.lang.javascript:
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? Does anybody have a tutorial/guide?

I use this code with onkeydown(!!!), also for F5/6/7/8.

[IE only, because the page is for my personal administrator use only]
[The alert()s are leftovers from the debugging fase]

onkeydown='druk()'

.....

function druk(){
x=event.keyCode
//alert(x)
if((x==8)||(x==32)){ // bs or space
doenv(ouderbnum)}
else if (x==27){ // esc
location.reload()}
else if (x==37){ // li
doenv(tvorig)}
else if (x==39){ // re
doenv(tvolgend)}
else if (x==40){ // dn
doenv(svorig)}
else if (x==38){ // up
doenv(svolgend)}
else if (x==36){ // back numb
doenv(bnumdef)}
else if (x==35){ // help
helpen()}
else if (x==45){ // toggle test
text()}
else if (x==116){ // F5
doen(onder[0])}
else if (x==117){ // F6
doen(onder[1])}
else if (x==118){ // F7
doen(onder[2])}
else if (x==119){ // F8
doen(onder[3])}
//else {alert(x)}
}
 
R

Robert

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top