Wanted: good Javascript for handling key events

04 October 2005   0 comments   Web development

Mind That Age!

This blog post is 13 years old! Most likely, its content is outdated. Especially if it's technical.

Powered by Fusion×

I've searched and searched but can't find anything that is good enough. What I'm after is some Javascript/ECMAScript code that let's me control what keys the user presses on a particular page. One page that does this well is Gmail but their code is so damn hard to debug because of all the "obfuscation".

So, do you know any pages where they've really got keyevents going with javascript? I mean for the whole page, not just a simple onkeydown on a form input. The only stuff I've found either doesn't work in IE or doesn't work at all in Mozilla.

I know that it's considered bad usability to fiddle with the keys because it can break something native to the browser but I know what I'm doing and Gmail as an example that it is possible.


I've found something that works now. At least in Firefox and MS IE 6. It uses the onkeypress event like this:

function body_onkeypress(evt){
   if (!keyboard_shortcuts_enabled) return;
   function S(k) { return String.fromCharCode(k); }
   if (window.event) key=window.event.keyCode;
   else key=evt.which;
   var s = S(key);
   // then do something with 's'
document.onkeypress = body_onkeypress


Thank you for posting a comment

Your email will never ever be published

Related posts

Toggle Zope's debug mode 01 October 2005
Gmail catching up with the IssueTrackerProduct 04 October 2005
Related by Text:
How to "onchange" in ReactJS 21 October 2015
parentElementsByTagName(doc, tagname, classname) 06 March 2006
Going real simple on HTML5 audio 14 October 2011
What stumped me about AngularJS 12 May 2013
Fastest way to download a file from S3 29 March 2017