faqts : Computers : Programming : Languages : JavaScript : Event handling

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

200 of 267 people (75%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

you can capture all the keys !!

Apr 21st, 2003 03:18
Shashank Tripathi, david koenig, http://sniptools.com/jskeys


Please try the following code. It works for capturing key presses on 
IE, Netscape, Opera 7, and somewhat quirkily with Opera 6 (numbers and 
uppercase letters should work). For this to work with Mozilla etc, 
perhaps a more DOM compliant code would be better (for some working 
code check out this site: http://sniptools.com/jskeys) 
CODE SAMPLE: 
<html><head>
<script language="JavaScript">
<!--
function keyHandler(e)
{
    var pressedKey;
    if (document.all)    { e = window.event; }
    if (document.layers) { pressedKey = e.which; }
    if (document.all)    { pressedKey = e.keyCode; }
    pressedCharacter = String.fromCharCode(pressedKey);
    alert(' Character = ' + pressedCharacter +
          ' [Decimal value = ' + pressedKey + ']');
}
document.onkeypress = keyHandler;
//-->
</script></head>
<body>Press any key on your computer now.</body></html>