Why don't the keys work? JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why don't the keys work? JavaScript

When I ran this on browser while making the game the player would move around normally wthout a problem, after copy pasting the code in SoloLearn it wouldn't work however: https://code.sololearn.com/Wa1603A8A0a1

5th Apr 2021, 10:28 AM
Karak10
Karak10 - avatar
2 Answers
+ 1
Add tabindex="0" as attribute to the canvas tag. Add canvas.focus() to your script. And finally add preventDefault() method to the keyDown and keyUp event handlers. https://code.sololearn.com/Wa7A18a29A24/?ref=app
6th Apr 2021, 7:09 AM
ODLNT
ODLNT - avatar
+ 1
Karak10, Error Line 10-11 Was having minor syntax error canvas.width = window.innerWidthl canvas.height = window.innerHeight; It should be as canvas.width = window.innerWidth; canvas.height = window.innerHeight; It's works fine with this change. Any way I don't know how to play game this fix will keep it working. DHANANJAY
6th Apr 2021, 12:28 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar