What are the different steps Rust and JS have to make reading user input (event handling), with WASM?

Event Handlers seem like a hassle in Rust, and I'm wondering how big, and what precisely the advantages would, or could be. What are the extra steps JS either has or doesn't have to make compared to Rust, using WASM?

An example I can find reading input in JS and executing the code in Rust: Click event read in JS. As far as I'm understanding here. JS directly uses the converted Rust function in WASM.

An example reading mouse input in Rust: Mouse move in Rust. In this example, Rust calls the JS eventhandler, which than all gets converted to WASM and executed?

AND -> Does the same apply for reading keyboard inputs?

Can someone ELI5? What happens step by step, and as I asked, what would be the advantages? Any specific reason why you'd want to use this in Rust?

(kind of a duplicate question, but I need some more detail)

1 Like

WASM, and therefore Rust, doesn't have access to browser APIs/DOM, and can't interact with keyboard or mouse itself. For WASM all interaction with the outside world has to go through JS.

All of the mouse and keyboard in Rust/WASM is done by JavaScript getting these events, translating them to a format that the Rust/WASM program expects and invoking WASM functions to let them see the events.

In terms of just these events, Rust/WASM has no advantage at all. It can't do anything more than JS already can.

The reason to handle events in Rust/WASM is if you have program written in Rust and want to invoke Rust functions (e.g. if you wrote a game in Bevy, you'd want to handle mouse the Bevy way). If the actions you perform are compute-heavy, they're likely to be faster in WASM than in JS.

1 Like

Thanks, I thought so, but they don't recommend just using JS either which is a bit odd I suppose.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.