I am building a program in JavaScript. While I tried to add keyevents, but when I was done and tried to move my object it didn't move. I tried to figure out what the problem was while I tried to move a object (with x and y coordinate system for example(y[1] += 5;
)). Therefore I did try to see so the keycode was right. I made a function to see the keyCode
:
window.addEventListener( "keypress", doKeyDown, false );
function doKeyDown(e) {
alert( e.keyCode )
}
Then I tried to press any button to see the keycode but every button is saying "0", unless arrow up, down, right, left. They are telling me the same value as on this website: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes but as I said for example "W" "A" "S" "D" just tell me "0" as the keycode and I wounder why.
Use charCode
instead of keyCode
for characters.
The charCode
property is holding the ascii of the character you can use to retrieve the pressed letter with String.fromCharCode
.
Keycodes and charcodes are different things and you can use this difference to determine if the user typed a character or accomplished an action such as when using arrows.