Cocos2d-JS, Unable to receive keyboard events until we click inside the game

Hi,

In my cocos2d-JS based game, user has to use an arrow key to control the character. The problem is when the game starts, keyboard events are not received by the gameplay layer until the player clicks anywhere inside the game.

I thought the issue might get solved by putting ‘focus’ on the Game-Canvas element. So I tried to use the following code:

var canvas=document.getElementById(‘gameCanvas’);
// set focus to the canvas when the video ends
canvas.focus()

But it doesn’t make any difference. What could be the solution to this problem? (I cannot make the user to click the game screen)

Hope someone helps me with this ASAP… Thanks in advance…

1 Like

Hi,
Please use window.focus(), and try it again.

Best regards
David

Hello there, I tried your solution but did not get any success.
Please help regarding this focus issue.