I want to generate some signals that I can capture and use in a web page so I can drive my app written in Three.js . Keyboards are not convenient for that, so does the mouse.
jQuery has a .keypress method accepting no arguments that simulates a keypress.
$("#target").keypress();
Will trigger a keypress on #target
If you'd like to also select which key was pressed, you can use .trigger. This example is from the docs:
var e = $.Event("keydown", { keyCode: 8});
$("body").trigger(e);
The key code 8 is the key code for backspace in JavaScript.
Let me know how that works for you :)
If you need a function:
function simulateKeyPress(character) {
jQuery.event.trigger({ type : 'keypress', which : character.charCodeAt(0) });
}
I recently decided to buy a new keyboard, but from my past experiences with keyboards, I was able to learn that it is also very important to get a keyboard cover keyturepro.com/shop/p/macbook-pro-keyboard-cover because with it your keyboard will always be clean, will not be scratched and will not get into it dirt and dust, so it will last you even longer.
Paul
Javascript developer
About 10 years ago we had a device build that emulated a keyboard, we went to an electronics specialist and he made it. We used it to measure keypress to recognition of the machine accepting that event, the key press was a knows intervals so we could gather accepted margins of error on how fast someone was able to react to an onscreen event by pressing a key on the keyboard. This was a custom piece of hardware. Unfortunately I cannot remember where we had this made.