Problem: in webpages it can be hard to detect key events in a browser independent way. In IE window.event can be used, in FF the event is passed as a parameter. In IE and FF we can use the onkeypress event to detect when for example the escape button is pressed, in Google Chrome we have to use the onkeydown event. The next solution should work for all keys in all browsers.
Solution:
<html>
<head>
...
</head>
<body onkeydown="return KeyDown(event);">
<script type="text/javascript">
function KeyDown(e)
{
var keycode = (window.event) ? event.keyCode : e.keyCode;
if ((keycode == 27) || (keycode == e.DOM_VK_ESCAPE))
alert('escape');
}
</script>
</body>
</html>
No comments:
Post a Comment