Click to See Complete Forum and Search --> : Capturing a key press event
DJRobThaMan
08-12-2003, 12:23 PM
I was just wondering if anyone knew where I could find a list of all the assignments for the buttons on a keyboard. I don't know if I'm really making myself all that clear but I came across a script that displayed a menu when the user pressed a certain button and in order to do that it said something like if(event.keypress == "220"){etc...}. So if anybody knows where I can find all the assignments for the keyboard that would be great.
Thanks
It's easier to use JavaScript to tell you...
<script type="text/javascript">
function daKIE() {
var str = "You pressed key #"+event.keyCode+"\n";
alert(String.fromCharCode(event.keyCode));
oDiv.innerHTML=event.keyCode +" = "+ String.fromCharCode(event.keyCode);
alert(str);
}
</script>
</head>
<body onKeyPress="daKIE()" onMouseDown="daKIE()">
<div id="oDiv"></div>
</body></html>
[J]ona
This version will also work in Mozilla/Netscape:
<script type="text/javascript">
function whichKey(e) {
if (window.event) {
code = event.keyCode
}
else {
code = e.which;
}
document.getElementById("charcode").innerHTML = String.fromCharCode(code);
}
document.onkeypress = whichKey;
</script>
</head>
<body>
<p>You pressed this key:</p>
<div id="charcode" style="font-family: verdana, arial, sans-serif; font-weight: bold; font-size: 12px;"></div>
Originally posted by pyro
This version will also work in Mozilla/Netscape:
So I got lazy... Gonna do somethin' about it--punk? :D
[J]ona
And this version works in all the browsers that I have to test it (IE6, NN4.7 and 7.0, Mozilla 1.2 and Opera 7):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>KeyCode Generator</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function whichKey(e) {
if (window.event) {
code = event.keyCode
}
else {
code = e.which;
}
if (document.getElementById) {
document.getElementById("charcode").innerHTML = String.fromCharCode(code);
document.getElementById("keycode").innerHTML = code;
}
else {
alert ("You pressed this key:"+String.fromCharCode(code)+"\n\nIt's KeyCode is:"+code);
}
}
document.onkeypress = whichKey;
</script>
</head>
<body>
<p>You pressed this key:
<span id="charcode" style="font-family: verdana, arial, sans-serif; font-weight: bold; font-size: 12px;"></span></p>
<p>It's KeyCode is:
<span id="keycode" style="font-family: verdana, arial, sans-serif; font-weight: bold; font-size: 12px;"></span></p>
</body>
</html>
DJRobThaMan
08-12-2003, 02:47 PM
lol @ jona
Thanks a lot guys
now I can be a LOT more idle than usual.
DJRobThaMan
08-12-2003, 03:02 PM
Hold up...
I was looking at pyro's code and it seems that instead of using keycode == some number that I'm never going to remember unless I keep something like what's posted above on me at all times i could use something like this
if(event.charCode == "w"){
do whatever I say to do;
}
Is that possible??????????
Certainly, just a slight modification to my earlier script:
<script type="text/javascript">
function whichKey(e) {
if (window.event) {
code = event.keyCode
}
else {
code = e.which;
}
if (String.fromCharCode(code) == "w") {
alert ("You pressed the 'w' key");
}
}
document.onkeypress = whichKey;
</script>
DJRobThaMan
08-12-2003, 04:36 PM
NICE
Thanks a lot
You are very welcome... :)