whenever i press on either up or down button i can check if those key were pressed for example an alert will be fired here :
if ((38 in keysDown) ) { // Player holding up
window.alert("up button was pressed");
}
if ((40 in keysDown) ) { // Player holding down
window.alert("down button was pressed");
}
but here even tho i press on enter the alert doesnt play
if ((13 in keysDown) ) { // player pushed on enter
window.alert("enter key was pressed"); // here nothing happen
}
i am using those 3 lines right at the start to add a event listner:
keysDown = {};
addEventListener("keydown", function (e) {keysDown[e.keyCode] = true;}, false);
addEventListener("keyup", function (e) {delete keysDown[e.keyCode];}, false);
but for some reason which i dont know of the enter key doesnt work
the problem occurs on firefox , working fine on Chrome and on IE
thanks in advance.
EDIT i figure out the problem, the canvas which i am using doesnt have focus,
so the enter button doesnt work, can any1 tell me how to switch focus ?
sure.
How do you set focus to the HTML5 canvas element?
or even better:
1- Tabindex:
2- setting contentEditable to true:
source: How do I give an HTML canvas the keyboard focus using jquery?