How would I change these functions to work on keypresses rather than button clicks on Tetris? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How would I change these functions to work on keypresses rather than button clicks on Tetris?

https://code.sololearn.com/WcT7LGC7moHS/#html Above is a link to the code in which I wish to change the buttons to simple key presses for rotation, movement, and soft and hard drops. Any assistance is appreciated

9th Mar 2019, 4:50 PM
Fleet
Fleet - avatar
5 Answers
+ 3
Fleet yes you can do it without an input field. The listner function will be like, on("keypressed", (e) => { //TODO });
11th Mar 2019, 12:21 AM
Seniru
Seniru - avatar
+ 4
You can use keyboard event listeners for that purpose. Check this link for more details https://www.w3schools.com/jsref/obj_keyboardevent.asp Feel free to ask if you have any doubts regarding the listeners
9th Mar 2019, 5:15 PM
Seniru
Seniru - avatar
+ 2
Yes, you can use key codes. Here is an example of how your code will look like document.on("keypressed", (e)=> { //up arrow pressed. if(e.keycode==38) { //code for handling up arrow. } //you have to handle all the keys like this. Here is a reference for key codes. https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes });
12th Mar 2019, 12:30 PM
Seniru
Seniru - avatar
+ 1
Is it possible to use these keyboard event listeners without using an input field, and to call specific functions based on the key pressed?
10th Mar 2019, 4:57 PM
Fleet
Fleet - avatar
0
What about arrow key controls? Would I just use the keyCodes? Finally, how exactly would I use that segment of code and where?
11th Mar 2019, 8:01 PM
Fleet
Fleet - avatar