How to capture mouse input in html5

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Luckytu

I want to export my first person game for html5. All my keyboard inputs work in a webbrowser, however the mouse inputs don’t. I want to rotate the player / camera using the mouse. What would be the proper way to get mouse inputs to work?

My code is:

func _input(event: InputEvent) -> void:
     if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
		head.rotate_x(deg2rad(event.relative.y * mouse_sensitivity * -1))
		self.rotate_y(deg2rad(event.relative.x * mouse_sensitivity * -1))

		var camera_rot = head.rotation_degrees
		camera_rot.x = clamp(camera_rot.x, -89.9, 89.9)
		head.rotation_degrees = camera_rot

head being the node the camera is a child of.

Mouse mode is set to


The script is attached to a KinematicBody (the player).

It works for windows export but not in html5.

:bust_in_silhouette: Reply From: James Hackett

Try something like this

var keyInput = document.getElementById(“keyInput”);

function inputFunc(e) {
If (e.keyCode == 82) {// 82 = R
//Do Something
keyInput.addEventListener(“keypress”, inputFunc);