Here is a solution based on Yahoo User Interface JavaScript framework which illustrates the trapping of F1 key both in IE and Firefox. Tested in IE 7 and Firefox 2.0.0.3
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://yui.yahooapis.com/2.2.0/build/yahoo/yahoo-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.2.0/build/event/event-min.js"></script>
<script type="text/javascript">
document.focus; //Focusing on the document is important as the F1 key trapped is based on the document object.
function isexplorer(){
if(navigator.appName.indexOf('Microsoft')!=-1)
return true;
else
return false;
}
YAHOO.util.Event.addListener(document, "keydown", processKeyDown); //This listener listens for keydown event.
document.onhelp = new Function("return false;"); // To disable default help in IE
function processKeyDown(evnt) {
if ( evnt == null )
evnt = event;
if ( evnt.keyCode == 112 ) //F1
{
help(); // Insert the help function calling or script statement here
if(isexplorer()== false)
{
evnt.preventDefault(); // disable default help in Firefox
}
return false;
}
}
function help()
{
//Handles the help part
alert('This is the help function');
}
</script>
</head >
<body>
<h2 style="color:blue;">F1 Key Press Trapping Demo</h2>
</body>
</html>
In the above code the required JS files is being accessed directly from Yahoo's UI server.
Bookmarks