Overriding the backspace key using JavaScript, in Firefox, IE, Opera and Safari
Posted by jimblackler on Sep 21, 2007
In my web wordgame Qindar.net I wanted to allow the players to use the keyboard to place words on the game board. This included use of the arrow keys to navigate, and the backspace key to ‘delete’ wrongly placed letters.
The problem is that even when this is handled in JavaScript, most browsers still catch the backspace key and interpret it as a user request to go back to the previous page. Boom! There goes your game page, and one annoyed user.
I did find a way of masking the backspace key that works in all the browsers I have tested it against. The trick for most browsers is to override the onkeydown event, check for event number “8” and return ‘false’ from that event. This signals to the browser not to process that key.
As often happens one particular browser is troublesome, in this case it was Opera, that needed “onkeypress” overriding rather than “onkeydown”.
Yesterday I had an email query recently asking how this was done so I’ve detailed it here.
There’s a demo here. Select ‘View Source’ in your browser to see how it’s done.
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head>
- <title>Backspace Browser Trap Demo</title>
- </head>
- <body>
- <p>Try pressing backspace on this page.</p>
- <p>Note you can still go 'back' with Alt + Left</p>
- <p id="keypressed"></p>
- <script type="text/javascript" language="javascript">
- // function to possibly override keypress
- trapfunction = function(event)
- {
- var keynum;
- if (window.event) // eg. IE
- {
- keynum = window.event.keyCode;
- }
- else if (event.which) // eg. Firefox
- {
- keynum = event.which;
- }
- if (keynum == 8) // backspace has code 8
- {
- document.getElementById("keypressed").innerHTML = "Backspace pressed";
- // display a message
- return false;
- // nullifies the backspace
- }
- return true;
- }
- document.onkeyup = function(event)
- {
- document.getElementById("keypressed").innerHTML = ""; // clear the message
- return true;
- }
- document.onkeydown = trapfunction; // IE, Firefox, Safari
- document.onkeypress = trapfunction; // only Opera needs the backspace nullifying in onkeypress
- </script>
- </body>
- </html>
It seems that Firefox (at least current versions) also respects document.onkeypress, so if you register trapfunction as a handler for that then it gets called twice for every keypress. You can get around this either by detecting Opera specifically (and registering document.onkeypress for that browser only), or by catching onkeyup as well and making ignoring any future calls to onkeydown until onkeyup has been seen. Both of these solutions are hacky, but I can’t come up with anything more robust.
In the demo I had a single handler for simplicity, in the Qindar game control I had two handlers. I’d advise having a separate handler for each event, make sure both return false if the key char is 8. Only carry out your application’s key actions in onkeydown (or onkeypress if you want the key repeat behaviour).
Thanx. it is very usefull
Unfortunately JavaScript’s keyboard event handling is a big mess. There are some keys which can be only captured as keydown events, some of as keypress events, and it even varies that what kind of event type is used by different browsers for a given key. I am tried to develop a little ajax based console as a hobby (and learn some new things), it’s quite complicated to work on every (or at least the most known) browsers … Some standard would be wonderful …
Just try to follow this document: http://unixpapa.com/js/key.html
But anyway thanks for your post, I’ve missed the return value of the event handlers it seems and this post helped me to find my mistake!