I am having a system which recives data samples of sensors in WebSocket and translates it into touch events.
The data is irelevant to the question, Eventually I have a x,y
samples I need to simulate a touch event from.
I wrote a simple code to simulate the gestures using dispatchEvent
I wrote a code which simply stores the point detected in an object, and if it’s close to an existing point (within radius, TOW_POINT_DISTANCE_THRESHOLD
) it declares it as an initial point for the later click recognition (distance of start and end points is smaller than a constant).
end
point is determined if there are no other points for duration of MOUSE_INACTIVITY_THRESHOLD
(80ms
)
//simple code to simulate touch events from point in a loop
//point = {x:someX, y:someU}
let isFirst = true;
const startPointKey = Object.keys(activeTouchPoints.current).find((key) => {
if (Math.hypot(activeTouchPoints.current[key].x - point.x, activeTouchPoints.current[key].y - point.y) <= TOW_POINT_DISTANCE_THRESHOLD) {
isFirst = false;
return true;
}
}) || `${point.x},${point.y}`;
const theElement = (anyParent.ownerDocument || anyParent).elementFromPoint(point.x, point.y) as HTMLElement;
if (!theElement || !anyParent.contains(theElement)) return;
if (isFirst) {
theElement.focus();
activeTouchPoints.current[startPointKey] = {
x: point.x,
y: point.y,
downX: point.x,
downY: point.y,
};
const touch = new Touch({
identifier: Date.now(),
target: theElement,
clientX: point.x,
clientY: point.y,
});
const pointerOverEvent = new PointerEvent('pointerover', {
bubbles: true,
pointerId: touch.identifier,
pointerType: 'touch',
clientX: point.x,
clientY: point.y,
});
theElement.dispatchEvent(pointerOverEvent);
const pointerEnterEvent = new PointerEvent('pointerenter', {
bubbles: true,
pointerId: touch.identifier,
pointerType: 'touch',
clientX: point.x,
clientY: point.y,
});
theElement.dispatchEvent(pointerEnterEvent);
const pointerEvent = new PointerEvent('pointerdown', {
bubbles: true,
pointerId: touch.identifier,
pointerType: 'touch',
clientX: point.x,
clientY: point.y,
});
theElement.dispatchEvent(pointerEvent);
const touchEvent = new TouchEvent('touchstart', {
bubbles: true,
touches: [touch],
targetTouches: [touch],
changedTouches: [touch],
});
theElement.dispatchEvent(touchEvent);
const gotPointerCaptureEvent = new PointerEvent('gotpointercapture', {
bubbles: true,
pointerId: touch.identifier,
pointerType: 'touch',
clientX: point.x,
clientY: point.y,
});
theElement.dispatchEvent(gotPointerCaptureEvent);
const mouseDownEvent = new MouseEvent('mousedown', {
bubbles: true,
clientX: point.x,
clientY: point.y,
});
theElement.dispatchEvent(mouseDownEvent);
} else {
// Touch move
activeTouchPoints.current[startPointKey].x = point.x;
activeTouchPoints.current[startPointKey].y = point.y;
// Dispatch touchmove and mousemove events
const touch = new Touch({
identifier: Date.now(),
target: theElement,
clientX: point.x,
clientY: point.y,
});
const pointerEvent = new PointerEvent('pointermove', {
bubbles: true,
pointerId: touch.identifier,
pointerType: 'touch',
clientX: point.x,
clientY: point.y,
});
theElement.dispatchEvent(pointerEvent);
const touchEvent = new TouchEvent('touchmove', {
bubbles: true,
touches: [touch],
targetTouches: [touch],
changedTouches: [touch],
});
theElement.dispatchEvent(touchEvent);
// Reset inactivity timer
if (activeTouchPoints.current[startPointKey].timerId) {
clearTimeout(activeTouchPoints.current[startPointKey].timerId);
}
}
// Set up inactivity timer for mouseup
activeTouchPoints.current[startPointKey].timerId = setTimeout(() => {
const touch = new Touch({
identifier: Date.now(),
target: theElement,
clientX: activeTouchPoints.current[startPointKey].x,
clientY: activeTouchPoints.current[startPointKey].y,
});
const pointerEvent = new PointerEvent('pointerup', {
bubbles: true,
pointerId: touch.identifier,
pointerType: 'touch',
clientX: point.x,
clientY: point.y,
isPrimary: activeTouchPoints.current[startPointKey].isPrimary,
});
theElement.dispatchEvent(pointerEvent);
const touchEvent = new TouchEvent('touchend', {
bubbles: true,
touches: [touch], //?
targetTouches: [touch], //?
changedTouches: [touch],
});
theElement.dispatchEvent(touchEvent);
const mouseUpEvent = new MouseEvent('mouseup', {
bubbles: true,
clientX: point.x,
clientY: point.y,
});
theElement.dispatchEvent(mouseUpEvent);
// Check for click based on distance
const distance = Math.hypot(
activeTouchPoints.current[startPointKey].x - activeTouchPoints.current[startPointKey].downX,
activeTouchPoints.current[startPointKey].y - activeTouchPoints.current[startPointKey].downY
);
if (distance <= CLICK_DISTANCE_THRESHOLD) {
const clickEvent = new MouseEvent('click', {
bubbles: true,
clientX: activeTouchPoints.current[startPointKey].x,
clientY: activeTouchPoints.current[startPointKey].y,
});
theElement.dispatchEvent(clickEvent);
}
// Clean up
delete activeTouchPoints.current[startPointKey];
}, MOUSE_INACTIVITY_THRESHOLD);
}
I thought using it at the app level (aka using dispatchEvent
) and not in the browser/system level, and that’s where the problem start.
The problem is that it doesn’t work for simple native components like Sliders which requires change
event and don’t response to mouse/touch events, or advanced external components like SwipperJS for example which doesn’t response to those events also (even to clicks), and I am not even talking about scrolling a text on swipe if it’s long like in real device.
TL;DR: the problem is the simulating level, simulating directly at web level is problematic and not native (unless you have a clever general solution that will work everywhere).
I guess I have to simulate at the browser level (I originally didn’t mean to, because I wanted it to support also an external browser), luckily I can choose the main browser and I can use electron (which I currently use) or any other framework for running it.
I am looking for soluton to do this part the best way, is there any library (testing one maybe) that simulates those events at the user level? Or any general way to do it in electron (contolling the touch like it’s System’s)? / faking events at the system level for only this app?
My electron code eventally is very simple, it jsut open’s the app in a BrowserWindow
with some args, I don’t insists in using electorn, if there is a better and easier way of doing it in other platform like CefSharp
or anything else I would be glad to hear.
Any hint and help is appricated.