Let’s say that I have a block of code like this:
keysPressed = new Object();
document.onkeydown = function(e) {
keysPressed[e.charCode] = true;
}
document.onkeyup = function(e) {
keysPressed[e.charCode] = false;
}
This should theoretically leave me with an object where I can go keysPressed.w to check if ‘w’ is currently pressed.
I can see in my JavaScript console that all of the attributes are defined like normal, but whenever I try to access them, it says that they’re undefined. I suspect that the attributes have a local scope inside the functions where they’re defined, and can’t be accessed elsewhere. Is there any way to solve this problem without creating a whole bunch of global variables?
Thanks in advance!
The problem here is that
charCodereturns a numeric value. So instead of adding a property forwit’s adding an property for the number87. You want to useString.fromCharCodeto get the string representation hereNow code like the following will work (note the use of upper case W instead of lower)