By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,996 Members | 892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,996 IT Pros & Developers. It's quick & easy.

Setting up hotkeys using onKeyPress

P: n/a
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?
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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)}
}

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #2

P: n/a
am****@gmail.com (Hasan Ammar) wrote in message news:<e9**************************@posting.google. com>...
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>
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.