glenmac
03-15-2004, 03:37 PM
A little page for finding keycodes.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>test bed</title>
<script language="JavaScript">
function checkKeycode(e) {
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
alert("keycode: [" + keycode + "]");
}
</script>
</head>
<body>
<p>Type in here:
<input type="text" size="10" value="" onKeyPress="checkKeycode(event)">
</p>
</body>
</html>
glenmac
03-16-2004, 08:12 AM
sorry if this was inaprpriate!!!!!!!!!!!
No, I don't think it is inappropriate, although it is a bit simple.
Sometimes I write such a thing to remember which keycode some key has.
But now I can copy your code :)
Willy Duitt
03-17-2004, 05:26 PM
FWIW: Here's a Bookmarklet which displays the keycode in the status bar.
javascript:void(document.body.onkeypress=function(){window.status=event.keyCode;});
.....Willy
Choopernickel
03-17-2004, 07:19 PM
Here's a static method to add to String that will return the value of the key pressed on a given event, or something. I wrote this a while ago, and I can see that it needs a bit of work.
String.fromKeyCode = function(keyCode,evtType) {
if (evtType && evtType.length && evtType.toLowerCase() == "keypress")
return String.fromCharCode(keyCode);
var keyDownChars = new Array(16);
keyDownChars[8] = '[Bksp]';
keyDownChars[9] = '[Tab]';
keyDownChars[12] = '[N5+shift]';
keyDownChars[13] = '[Enter]';
keyDownChars.push('[Shift]','[Ctrl]','[Alt]','[Pause]','[CapsLock]');
for (i=11;i;--i) keyDownChars.push('undefined');
keyDownChars[27] = '[Esc]';
keyDownChars.push(' ','[PgUp]','[PgDn]','[End]','[Home]','[Left]','[Up]','[Right]','[Down]');
for (i=7;i;--i) keyDownChars.push('undefined');
keyDownChars[45] = '[Ins]';
keyDownChars[46] = '[Del]';
keyDownChars.push(['0',')'],['1','!'],['2','@'],['3','#'],['4','$'],['5','%'],['6','^'],['7','&'],['8','*'],['9','(']);
for (i=7;i;--i) keyDownChars.push('undefined');
keyDownChars.push('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U ','V','W','X','Y','Z','[WinKey]');
for (i=4;i;--i) keyDownChars.push('undefined');
keyDownChars.push('0','1','2','3','4','5','6','7','8','9','*','+','undefined','-','.','/','[F1]','[F2]','[F3]','[F4]','[F5]','[F6]','[F7]','[F8]','[F9]','[F10]','[F11]','[F12]');
for (i=62;i;--i) keyDownChars.push('undefined');
keyDownChars[144] = '[NumLock]';
keyDownChars[145] = '[ScrollLock]';
keyDownChars.push([';',':'],['=','+'],[',','<'],['-','_'],['.','>'],['/','?'],['`','~']);
for (i=26;i;--i) keyDownChars.push('undefined');
keyDownChars.push(['[','{'],['\\','|'],[']','}'],["'",'"']);
return keyDownChars[keyCode];
}
Here's an example of use:
function eventHandler(evt) {
var key = String.fromKeyCode((evt? evt.which : window.event.keyCode), (evt? evt.type; window.event.type));
alert(key);
}
glenngv
03-18-2004, 04:29 AM
The 'Key Events' link in my sig leads to a page that has complete display of key events and keycodes.