As I work on my Three.js demo (though I aim for the same functionality across any library), I find myself overwhelmed by the abundance of answers that involve jQuery or other libraries when it comes to user input and events.
Being new to this, I prefer sticking with Vanilla JS only.
My goal is simple - detecting key presses like W and logging a message such as
console.log("Key: 'W' was pressed");
. Similarly, I want to track mouse position using something like console.log(mouseX + ", " + mouseY);
.
The challenge lies in finding basic vanilla JS examples. I'm looking for a straightforward solution that I can later build upon, such as:
- Detecting if a key or mouse button is pressed
- Getting the X and Y coordinates of the mouse
- Implementing an event queue for synchronization with the frame rate/loop
I've heard that handling events and input can be tough, so I just need a little push in the right direction.
P.S. I'm open to using a lightweight library that simplifies this process and maybe even supports touch screens, mobile devices, and controller input - especially for gaming purposes.