Sorry if this may seem stupid, but how would I make a JS file that will do something when the user clicks the letter w or some other letter? Again sorry if this is a stupid question...
Asked
Active
Viewed 140 times
-1
-
I think you need to be more specific – Mike Robinson Jan 29 '14 at 05:11
-
One approach would be to wrap every character in its own element (and then handle the click event on those elements). However, whether this is feasible or not depends on the context. What are you trying to achieve, overall? – Felix Kling Jan 29 '14 at 05:11
-
1http://stackoverflow.com/questions/12153357/how-to-register-document-onkeypress-event – dev7 Jan 29 '14 at 05:11
-
1Show some efforts. What have you tried so far? – Devraj Gadhavi Jan 29 '14 at 05:32
4 Answers
1
document.addEventListener("keydown", keyDownEvent, false);
function keyDownEvent(e) {
var keyCode = e.keyCode;
alert("You have hit some key :"+keycode);
}
you can refer the key codes on google. and accordingly perform some action.

Esha Jain
- 555
- 5
- 16
0
logic: read the ascii value
of the key stroke and store it in some variable in if condition compare the stored value to the actual ascii value and if it matches execute the code.This should work

Sajad Karuthedath
- 14,987
- 4
- 32
- 49

Naim FS
- 61
- 9
-
How do you "read the ascii value" and what does "execute the code" mean? – user2864740 Jan 29 '14 at 05:13
0
Do a search on "javascript keypress keydown keyup" on Google and you may start from here.

Johnny
- 481
- 4
- 13
0
Try out something like:
<html>
<script type="text/javascript">
function keyHandler(e){
if(!e)e=window.event;
if(!e.which)e.which=e.keyCode;
if(e.which==87){ /*87 is the keycode for 'w' */
//do whatever you want
}
}
if(document.attachEvent){
document.attachEvent("onkeyup",keyHandler);
}else{
document.addEventListener("keyup",keyHandler,false);
}
</script>
</html>

kcak11
- 832
- 7
- 19