Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Send a keyboard button press through a js function #144

Open
ericoporto opened this issue Apr 26, 2021 · 3 comments
Open

Send a keyboard button press through a js function #144

ericoporto opened this issue Apr 26, 2021 · 3 comments

Comments

@ericoporto
Copy link

Hi,

I would like to send a key like f12 or alt+enter to my Emscripten built engine from a JS function. The reason is these are buttons that do special things in the engine, but they aren't easily available on the phones, so I would like to place a little overlay with some screen buttons in the html that would send these button presses to the engine.

Is there an easy way to leverage the SDL2 port to do this?

@Daft-Freak
Copy link
Member

Should be able to use dispatchEvent from JS, I think:

document.getElementById("canvas").dispatchEvent(new KeyboardEvent("keydown", {keyCode: 0x7B/*F12*/}));
//... and/or "keyup"

(Currently still relying on the old keyCode property)

Might need to manually keydown/up Alt, instead of using altKey though...

@ericoporto
Copy link
Author

Thanks, that solves for me!

@ericoporto
Copy link
Author

ericoporto commented May 26, 2021

Turns out this dispatchEvent didn't work. :/

The below works for some keys, but I can't find a way to pass the alt key.

window.dispatchEvent(new KeyboardEvent("keydown", {keyCode: 0x7B/*F12*/}));

What should be the alt keyCode?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants