You have seen in some websites, there will be some light box, popup windows and some tool tip . This can be closed by pressing ESC key. You may wondered about how the webpage detect my ESC key pressing. Here is the javascript to detect whether ESC key is pressed or not.
[code type=html]
<html>
<head>
<script type=”text/javascript”>
window.document.onkeydown = function (e)
{
if (!e) e = event;
if (e.keyCode == 27)
{
alert(‘you have pressed ESC key’);
}
}
</script>
</head>
<body>
</body>
</html>
[/code]
We can detect the presence of other key is pressed or not by using the event code below.
if (e.keyCode == 27) – Just replace 27 with the code below
Backspace | 8 |
Tab | 9 |
Enter | 13 |
Shift | 16 |
Ctrl | 17 |
Alt | 18 |
Pause, Break | 19 |
CapsLock | 20 |
Esc | 27 |
Space | 32 |
Page Up | 33 |
Page Down | 34 |
End | 35 |
Home | 36 |
Left arrow | 37 |
Up arrow | 38 |
Right arrow | 39 |
Down arrow | 40 |
PrntScrn (see below†) | 44 |
Insert | 45 |
Delete | 46 |
0 to 9 | 48-57 |
A to Z | 65-90 |
F1 to F12 | 112-123 |
NumLock | 144 |
ScrollLock | 145 |
, < | 188 |
. > | 190 |
/ ? | 191 |
` ~ | 192 |
[ { (see below‡) | 219 |
\ | | 220 |
] } | 221 |
‘ “ | 222 |