Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

KeyCodes and IE 2

Status
Not open for further replies.

bdichiara

Programmer
Oct 11, 2006
206
US
I'm using a script which works in Firefox, but not so much in IE that allows "keyboard shortcuts" on the page. So if the user presses "s" it takes them to the search box on the page to do a search. Only problem is i get a JS error in IE that says 'keyCode' is null or not an object.

Code:
function sendToSearch(e) {
	if (e.keyCode) {
		keycode=e.keyCode;
	} else {
		keycode=e.which;
	}
	character=String.fromCharCode(keycode);
	if(keycode == "83" || keycode == 83 || character == "s"){
		document.forms['search_box'].elements['search'].focus();
	}
}
document.onkeypress = sendToSearch;
Any ideas?

_______________
_brian.
 
This should work. In the event that the browser does not support some combination of keyCode or which, it will set the value of keycode to false:
Code:
<html>
<head>
<script language="JavaScript">

function alertKeycode(e) {
   e = (e) ? e : event;
   var keycode = (e.keyCode) ? e.keyCode : (e.which) ? e.which : false;
   alert(keycode)
}

document.onkeydown = alertKeycode;
</script>
</head>

<body>

</body>
</html>

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Why use JS for this at all? Investigate "access keys" - they're an accessible alternative to JS like this, and will probably work in a whole heap more browsers - even with JS disabled.

See our site here for an example:


Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Wow. BillyRay, that is awesome, i didn't know that existed. I may use that with some of my other applications, however with this one, pressing the "s" is easier for my users. Trying to use an ALT+Letter combination will only end up confusing them.

@ kaht, It looks like that did the trick, however now, in IE it actually puts an "s" into the search field after you type it.

I've tried to reset the value of the search field, but the "s" is still coming through. Any ideas about that?

_______________
_brian.
 
Well.... you can set the returnValue for the event equal to false, but that will not work in Firefox.... I'm not sure you can completely suppress the value from coming thru in all browsers - in which case you're probably better off using Dan's suggestion.

If a non-firefox solution works for you, then you can try setting the returnValue to false like so:
Code:
<html>
<head>
<script language="JavaScript">

function alertKeycode(e) {
   e = (e) ? e : event;
   var keycode = (e.keyCode) ? e.keyCode : (e.which) ? e.which : false;
   alert(keycode)
   e.returnValue = false;
}

document.onkeydown = alertKeycode;
</script>
</head>

<body>
<input type="text" />
</body>
</html>

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top