Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add onkeydown to body?

Tags:

javascript

In my index.html I have this code. I would need to add onkeydown event to that body in Main.onLoad().

Any idea how to do it?

<body id="body" 
    onload="Main.onLoad();" 
    onunload="Main.unload();" 
    onmousedown="Main.mouseDown();"
>

<body id="body" 
    onload="Main.onLoad();" 
    onkeydown="TVA.keyDown(event);" 
    onunload="Main.unload();" 
    onmousedown="Main.mouseDown();"
>
like image 615
GibboK Avatar asked Jul 11 '13 12:07

GibboK


1 Answers

In a Javascript block, try to use window.onkeydown (MDN).

You can also use document.onkeydown and document.body.onkeydown.

Here is an example for you:

JavaScript

document.body.onkeydown = function(e){
    alert(String.fromCharCode(e.keyCode)+" --> "+e.keyCode);
};

Live Demo

The code above can be put in any valid JavaScript block (such as Main.onLoad() function).

like image 140
Jeff Noel Avatar answered Oct 22 '22 23:10

Jeff Noel