/    Sign up×
Community /Pin to ProfileBookmark

Capturing a key press event

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

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@JonaAug 12.2003 — [font=arial][color=maroon]It's easier to use JavaScript to tell you...[/color][/font]

[font=monospace]

<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>

[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@pyroAug 12.2003 — This version will also work in Mozilla/Netscape:

&lt;script type="text/javascript"&gt;
function whichKey(e) {
if (window.event) {
code = event.keyCode
}
else {
code = e.which;
}
document.getElementById("charcode").innerHTML = String.fromCharCode(code);
}
document.onkeypress = whichKey;
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;You pressed this key:&lt;/p&gt;
&lt;div id="charcode" style="font-family: verdana, arial, sans-serif; font-weight: bold; font-size: 12px;"&gt;&lt;/div&gt;
Copy linkTweet thisAlerts:
@JonaAug 12.2003 — [i]Originally posted by pyro [/i]

[B]This version will also work in Mozilla/Netscape:[/B][/QUOTE]


[font=arial][color=maroon]So I got lazy... Gonna do somethin' about it--punk? ?[/color][/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@pyroAug 12.2003 — 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):

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;KeyCode Generator&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;script type="text/javascript"&gt;
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)+"nnIt's KeyCode is:"+code);
}
}
document.onkeypress = whichKey;
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;You pressed this key:
&lt;span id="charcode" style="font-family: verdana, arial, sans-serif; font-weight: bold; font-size: 12px;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;It's KeyCode is:
&lt;span id="keycode" style="font-family: verdana, arial, sans-serif; font-weight: bold; font-size: 12px;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@DJRobThaManauthorAug 12.2003 — lol @ jona


Thanks a lot guys


now I can be a LOT more idle than usual.
Copy linkTweet thisAlerts:
@DJRobThaManauthorAug 12.2003 — 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??????????
Copy linkTweet thisAlerts:
@pyroAug 12.2003 — Certainly, just a slight modification to my earlier script:

&lt;script type="text/javascript"&gt;
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;
&lt;/script&gt;
Copy linkTweet thisAlerts:
@DJRobThaManauthorAug 12.2003 — NICE



Thanks a lot
Copy linkTweet thisAlerts:
@pyroAug 12.2003 — You are very welcome... ?
×

Success!

Help @DJRobThaMan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...