Key Press Event Handling

Relates to Accessibility and DOM Scripting, Firefox and Co

On a current project, where accessibility is essential, I have integrated a stylesheet switcher on two pages where a mouseover affect on an image montage displays an introduction to each page in a separate box below the montage. The montage is structurally defined as a definition list, since it is a visual representation of links to pages on the site, with a brief description of each page. The stylesheet switcher allows users to switch the view to an actual list representation, if, for example, they are using only the keyboard. For CSS-enabled browsers without Javascript, the switcher loads a fresh page with the list display, and for non-display browsers the montage is just represented as a list from the outset.

While, I hope this will be effective under most conditions, I found a frustrating problem with supplementing the onclick event with an onkeypress event for keyboard users in Mozilla and co. When tabbing through the links on the page, as soon as the user tabs off the switcher (containing the event handler) the key press event is triggered. Well, of course, this is the correct behaviour, but Inte$net Exploder actually only triggers the event when the Return key is pressed, which is misleading to the developer, and incorrect interpretation.

The solution was to integrate a filter function that is called by the onkeypress event and this in turn only calls the style switcher function if the key pressed was Return

Here is the code:


function checkKeyPressed(evt, func, params)
{
  evt = (evt) ? evt : (window.event) ? event : null;
  if (evt)
  {
    var charCode = (evt.charCode) ? evt.charCode :
                   ((evt.keyCode) ? evt.keyCode :
                   ((evt.which) ? evt.which : 0));
    if (charCode == 13) func(params);
  }    
}

Where 13 is the ASCII value for the Return key. As well as passing the event as argument, I passed the function to be called and its parameters, to make the function generic. The scenario is just as relevant if using the event handlers to open a new window from a link (although this is strongly discouraged). The resulting anchor is as follows:


<a href="#" onclick="setStyleSheet('sheet'); return false;"
onkeypress="checkKeyPressed(event, setStyleSheet, 'sheet');"
>Switch to List View</a>

It is important that a false value is not returned in the key press event since this would prevent the user from tabbing beyond the style switcher link. The DHTML cookbook suggests the key detection in the filter should work in Netscape and Exploder back to v.4. It resolved the issue for this project.

Posted on Sunday, Sep 14, 2003 at 16:44:31.

Comments on Key Press Event Handling (7)

α comment

how to write program to handling events in XHTML.If we mouse cursor on some text it chage color.how to do this by using mouse events.

Posted by radha
Saturday, Jul 30, 2005 at 06:28:11

β comment

how to write program to handling events in XHTML.If we mouse cursor on some text it chage color.how to do this by using mouse events.

Posted by radha
Saturday, Jul 30, 2005 at 06:29:00

γ comment

how to do programming in XHTML.how to do mouse events.If we keep mouse on some it became other color how to do that.

Posted by radha
Saturday, Jul 30, 2005 at 06:30:52

δ comment

Why does:

document.forms[0].field.onkeypress=OnlyNumbers;

works and

<input name='field' type='text' onKeyPress="OnlyNumbers">

don't ?

the function is:

function OnlyNumbers(e)
{
	var code;

	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	else return true;
	alert(code);

}

Posted by Igor Feghali
Saturday, Nov 05, 2005 at 03:42:01

ε comment

The above information is useful but i think you had missed the events caused by system keys like backspace, f1,..etc. it. will be more helpful to the readers if that has got the feature to capture system key events. and personally i request you to send me an e-mail as i am needful of it.

Posted by Almannan
Sunday, Dec 11, 2005 at 13:45:14

ζ comment

Try this:

Posted by ducksauce
Monday, Feb 06, 2006 at 22:51:00

η comment

Sorry… though the form would automatically escape code:

try this:

<input name='field' type='text' onKeyPress="OnlyNumbers()">

Posted by Ben
Monday, Feb 06, 2006 at 22:52:23

Breadcrumbs Trail

[ Home ] -> TW Blog -> Sep 03 -> Key Press Event Handling
Site Map

The Severn Solutions website achieves the following standards:

[ XHTML 1.0 ] [ CSS 2 ] [ WAI AA ] [ Bobby AA ]

Page compiled in 0.119 seconds