# Controls in HTML5 for Mobile and Desktop simultaneously

**URL:** <https://forums.unrealengine.com/t/controls-in-html5-for-mobile-and-desktop-simultaneously/264026>\
**Category:** Platform & Builds\
**Tags:** html, unreal-engine, UE4-26\
**Created:** [December 3, 2021, 7:57pm UTC](https://forums.unrealengine.com/t/controls-in-html5-for-mobile-and-desktop-simultaneously/264026 "2021-12-03T19:57:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![v.rtx\_void](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/v.rtx_void/32/479735_2.png) [@v.rtx\_void](https://forums.unrealengine.com/u/v.rtx_void)\
**Post date:** [December 3, 2021, 7:57pm UTC](https://forums.unrealengine.com/t/controls-in-html5-for-mobile-and-desktop-simultaneously/264026/1 "2021-12-03T19:57:14Z")

</div>

Hey there,

I am fairly new to Unreal and starting to develop a small Gallery Exhibition of Photos in 3D.  
So far I could easily build the 3D assets, place them etc., but I cannot wrap my head around how to make the project controllable from Desktop AND on mobile devices (Android & iOS).

Moving through the space should be possible with Virtual Joysticks (and WASD Keys for Desktop)

My problem is that in:

→ Project Settings → Engine → Input

I have set up the Virtual Joysticks to be Alway visible and have set up some Mappings for moving forward, backwards, up, down etc.

 ![Untitled](https://d3kjluh73b9h9o.cloudfront.net/original/3X/0/4/04501bb5c20e51a93f48e2b27a329a635b15d678.jpeg)

This made it possible to navigate through the map on the Desktop Browser by clicking and dragging with the Mouse on the Virtual Joysticks, but on my Smartphone Browser I cannot click and drag on them.

I have tried to assign “Touch 1” to the Main Input Keys of the DefaultVirtualJoysticks Template, but that did also not work unfortunately.

Did I miss something here? Is it actually possible to use the Virtual Joysticks in a HTML5 packaged Project on a smartphone Browser?

---

<div class="post-metadata">

**Author:** ![DarknessFX](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/darknessfx/32/1194676_2.png) [@DarknessFX](https://forums.unrealengine.com/u/DarknessFX)\
**Post date:** [January 16, 2022, 4:17pm UTC](https://forums.unrealengine.com/t/controls-in-html5-for-mobile-and-desktop-simultaneously/264026/2 "2022-01-16T16:17:16Z")

</div>

There is nothing on the HTML5 template that hooks the webpage Touch events to your project wasm. As fair as I know, it won’t work without some hacks on the HTML5 page (like convert touch to fake keyboard inputs) or lots of customs changes and updates on your engine source code.

---

<div class="post-metadata">

**Author:** ![ThD\_ManiaC](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/thd_maniac/32/1194665_2.png) [@ThD\_ManiaC](https://forums.unrealengine.com/u/ThD_ManiaC)\
**Post date:** [September 5, 2023, 8:41pm UTC](https://forums.unrealengine.com/t/controls-in-html5-for-mobile-and-desktop-simultaneously/264026/3 "2023-09-05T20:41:25Z")

</div>

figured out that “clicking” with a finger works fine out-of-the-box, e.g. for UMG buttons but touch-movement is not working by default…

here is some hacky solution for this which gets Touch-Start, -Move and -End events working in Widget-Blueprints on Mobile (touch) and Desktop (mouse) HTML5 with UE4.23 (without engine-source modification, sorry no multi-touch):

in the `UE_4.23\Engine\Build\HTML5\project_template.js` add this, e.g. at line 103:

```auto
const touchableElement = document.getElementById('canvas');
touchableElement.addEventListener('touchstart', registerTouch);

function registerTouch(event)
{
	const mouseDownEvent = new MouseEvent('mousedown', {});
	event.target.dispatchEvent(mouseDownEvent);
	
	touchableElement.removeEventListener('touchstart', registerTouch);
	touchableElement.addEventListener('touchmove', handleTouchMove);
}

function handleTouchMove(event)
{
	// Handle touch move event
    event.preventDefault();
	
	// Create and dispatch corresponding mousemove event
	const mouseMoveEvent = new MouseEvent('mousemove', {
	  clientX: event.touches[0].clientX,
	  clientY: event.touches[0].clientY,
	});
	event.target.dispatchEvent(mouseMoveEvent);
	
	touchableElement.addEventListener('touchend', handleTouchEnd);
}

function handleTouchEnd(event)
{
	event.preventDefault();

	// Create and dispatch corresponding mouseup event
	const mouseUpEvent = new MouseEvent('mouseup', {});
	event.target.dispatchEvent(mouseUpEvent);
	
	touchableElement.removeEventListener('touchend', handleTouchEnd);
}

```

in the Widget-Blueprint, overwrite the OnMouseDown, -Move and -Up functions, with their code like this:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/3/8/838dd9d4e9f63d24f86e77393aa646e8c5250315.png)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/5/e/25ef36155d8c057cc51806c1912c55b3667c3348.png)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/4/e/34e36dc456e56bcf44a06ef9b64353be1af925a4.png)

The `handleTouchMove` and `handleTouchEnd` do overwrite the default click-behaviour and resets it when releasing the finger. In BP we though need to skip the first move-event (due to wrong move value), but we use i to manually process the MouseDown (since the default click behaviour is overwritten).

The method above does not work on Desktop (mouse), so we use a `registerTouch` event to enable the above Touch behaviour only when a TouchStart was detected in the Javascript. For some reason the first TouchStart event always returns the top-left-corner of the game-canvas for the MouseDown event (instead of the real touch-location…). since this seems always like x=7.163 (on android mobile-portrait and -landscape and windows edge) we can exploit this to enable the Touch-mode in Blueprint only when a TouchStart was triggered once (otherwise the default mouse-behaviour stays as is).

very weird, but so far it gets the job done…
