Connecting Tech Pros Worldwide Forums | Help | Site Map

onkeydown

Roger Withnell
Guest
 
Posts: n/a
#1: Dec 18 '05
I'm using the following code to start a function on key down.

document.onkeydown = OnKeyDown;

function OnKeyDown()
{
vKeyCode = event.keyCode;
..code...
}

This works in IE but in NN8 or Firefox 1.5.

Is my code wrong? Or is there another way to achieve this?

Thanking you in anticipation.




Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Danny
Guest
 
Posts: n/a
#2: Dec 18 '05

re: onkeydown




For mozilla/geckos, use the function 1st argument, event object does
pass the 'event object' as the 1st argument to the handler, so:

function BOBO(ev) {

alert(ev.keyCode)

}

document.onkeydown=BOBO;
Danny
hansschmucker@gmail.com
Guest
 
Posts: n/a
#3: Dec 18 '05

re: onkeydown


it's not really wrong as far as I can tell... just terribly obsolete.
By now there are two new APIs for that: attachEvent for MSIE and
addEventListener for any standards compliant browser. You can use this
function to make sure it works everywhere, just replace "click" with
"keyUp". That'll work in any browser. Attention: for MSIE the event
type is onXYZ, for the rest it's just XYZ: onclick VS click

function addClickHandler(object,func){
if( object.addEventListener ) {
object.addEventListener("click",func,false);
return 0;
} else if ( object.attachEvent ) {
object.attachEvent("onclick",func);
return 0;
} else {
object.onclick = func;
return 0;
}
return (-1);
}

Roger Withnell
Guest
 
Posts: n/a
#4: Dec 18 '05

re: onkeydown


Thanks for this, but I'm struggling.

I would like to call a function on key down in the window to test if it is
F12 (123).

With your code, how is addClickHandler called?
What is the object? document?
What function is func? The function I want to call?
Can I replace click with keyDown?

I've tried combinations of the above without success.

Your further help would be appreciated.

<hansschmucker@gmail.com> wrote in message
news:1134876907.687941.309110@g43g2000cwa.googlegr oups.com...[color=blue]
> it's not really wrong as far as I can tell... just terribly obsolete.
> By now there are two new APIs for that: attachEvent for MSIE and
> addEventListener for any standards compliant browser. You can use this
> function to make sure it works everywhere, just replace "click" with
> "keyUp". That'll work in any browser. Attention: for MSIE the event
> type is onXYZ, for the rest it's just XYZ: onclick VS click
>
> function addClickHandler(object,func){
> if( object.addEventListener ) {
> object.addEventListener("click",func,false);
> return 0;
> } else if ( object.attachEvent ) {
> object.attachEvent("onclick",func);
> return 0;
> } else {
> object.onclick = func;
> return 0;
> }
> return (-1);
> }
>[/color]



Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Tony
Guest
 
Posts: n/a
#5: Dec 18 '05

re: onkeydown


Roger Withnell wrote:[color=blue]
> I'm using the following code to start a function on key down.
>
> document.onkeydown = OnKeyDown;
>
> function OnKeyDown()
> {
> vKeyCode = event.keyCode;
> ..code...
> }
>
> This works in IE but in NN8 or Firefox 1.5.
>
> Is my code wrong? Or is there another way to achieve this?
>
> Thanking you in anticipation.[/color]

event isn't defined in your function - try:

function OnKeyDown(event) { ...

No promises - but I THINK that may help.

I would also recommend changing the function name - yes, I know it's
case sensitive, but having it that close is just asking for errors.

RobG
Guest
 
Posts: n/a
#6: Dec 19 '05

re: onkeydown


Roger Withnell wrote:[color=blue]
> Thanks for this, but I'm struggling.[/color]

Please don't top-post in this news group.

[color=blue]
> I would like to call a function on key down in the window to test if it is
> F12 (123).[/color]

If you wish to detect F12 anywhere in the document, then add an
onkeydown event handler to the body element:

<body onkeydown="checkF12(event);">


Where checkF12 checks the key pressed.

function checkF12(e){
var e = e || window.event;

// e now refers to the onkeydown event

}

There are plenty of examples in the archives of cross-browser keycode
detection.

[color=blue]
> With your code, how is addClickHandler called?
> What is the object? document?
> What function is func? The function I want to call?
> Can I replace click with keyDown?[/color]

This is why top-posting is frowned upon. Your question is here, but the
thing you are talking about is down below somewhere. Put your comments
immediately below quoted text, and only quote what is necessary.

These are fairly basic JavaScript and Document Object Model (DOM)
questions. You are dealing with two different event models, W3 and
Microsoft. To help with understanding, have a browse of quirksmode:

<URL: http://www.quirksmode.org/js/introevents.html >

[...][color=blue]
>
> Posted Via XXXXXX Premium Usenet Newsgroup Services
> ----------------------------------------------------------
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----------------------------------------------------------[/color]

Get a real newsreader or use Google groups:

<URL: http://groups.google.com/group/comp....t?lnk=lr&hl=en >


--
Rob
Closed Thread