Internet Advertising - Bad Credit Loans - Sciences in 2007 - Kamala - Anime Downloads

PDA

View Full Version : How to run function onkeydown (anywhere on page)


bobby9101
Jun 11th 2007, 2:39 pm
HI, currently I am using onkeydown on an input field, but I want to run the function when someone types no matter where on the page.
I need to be able to pass the event keyword in the function

ajsa52
Jun 12th 2007, 4:17 am
Well, you can associate the event funtcion to the <body> element.
Example (works for Firefox and Explorer):

<html>
<head>
<script type = "text/javascript">

function f_kd(ev)
{
var keynum;
if (window.event) // IE
{
keynum = ev.keyCode;
}
else if (ev.which) // Netscape/Firefox/Opera
{
keynum = ev.which;
}
var keychar = String.fromCharCode(keynum);

alert( "keynum='+" + keynum + ", keychar='" + keychar + "'" );
}
</script>

</head>
<body onKeyDown="f_kd(event);">
Hello
</body>
</html>