I've tried to do the same. This might not be quite what you asked for and it's a bit hacky but it might work for you. I am also interested in other solutions btw. Basically Javascript.eval() can return certain datatypes(check docs for full list). Here's how I use this. I set a certain js variable for example when a html button is pressed and then in godot poll that variable during the process step. I then convert what was stored in that variable to a godot InputAction.
This is the part in my gdscripts that sets up the js environment with the necessary things:
JavaScript.eval("""
function Input(action){
this.action=action
this.processed=false
this.data=""
}
function Input(action, processed, data){
this.action=action
this.processed=processed
this.data=data
}
//starting input, doesn't need to be processed, so processed=true
var input=new Input("default")
input.processed=true
function setInput(newInput){
input=newInput
}
var godotInputActions=new Object()
""",true)
# creates a js object containing strings for all godot actions
# dont try to create inputs with other actions in js
for action in InputMap.get_actions():
JavaScript.eval("""
godotInputActions.%s="%s"
""" % [action,action],true)
Here's the js code for what my html button does(had to add that to godot.html from the webassembly export templates):
document.getElementById('output-button').addEventListener('click', () => {
setInput(new Input(godotInputActions.ui_button))
});
This is the function that grabs new input(called during each process step):
func _fetch_js_input():
var input_action=JavaScript.eval("""input.action""")
var input_processed=JavaScript.eval("""input.processed""")
var input_data=JavaScript.eval("""input.data""")
return {"action": input_action, "processed": input_processed, "data": input_data}
This marks the js variable as processed(called e.g. after fetchjs_input):
func _set_js_input_as_processed():
JavaScript.eval("""input.processed=true""",true)
Then I can do whatever is needed where these inputActions are processed:
func _input(event):
if (event is InputEventAction and event.action=="ui_button"):
print("Button pressed");