lpcmanager

Create new widget - HMI:TouchDetect that can be added as page sibling to detect screen touches independently of screen saver and set PLC variable accordingly
// widget_touchdetect.ysl2 — global overlay; place the group as a direct child of the root
// svg alongside HMI:Page groups so it is not removed on page changes.
widget_desc("TouchDetect") {
longdesc
||
TouchDetect publishes whether any finger is on the screen via one HMI variable (first path).
Place the group under the document svg as a sibling of page groups (it is not tied to any page's
widget list). init() sets relativeness/offset so get_variable_index works without calling sub()
(sub() registers with subscribers and runs apply_cache; not needed for outbound-only touch writes).
Label example: HMI:TouchDetect@/YourBoolOrInt — while at least one touch pointer is active the
variable is 1, otherwise 0. Listeners are on document.body (pointerdown/pointerup/pointercancel).
||
shortdesc > Global touch active flag (body pointer events, touch pointers only)
path name="touch_active" count="1" accepts="HMI_BOOL,HMI_INT"
}
widget_class("TouchDetect") {
||
init() {
const n = this.indexes ? this.indexes.length : 0;
if (n > 0) {
this.offset = 0;
this.relativeness = Array(n).fill(false);
this.container_id = "";
}
this._touchPointerIds = new Set();
this._onPointerDown = (e) => {
if (this._touchPointerIds.size === 0) {
this.apply_hmi_value(0, 1);
}
this._touchPointerIds.add(e.pointerId);
};
this._onPointerUp = (e) => {
this._touchPointerIds.delete(e.pointerId);
if (this._touchPointerIds.size === 0) {
this.apply_hmi_value(0, 0);
}
};
document.body.addEventListener("pointerdown", this._onPointerDown, { passive: true });
document.body.addEventListener("pointerup", this._onPointerUp, { passive: true });
document.body.addEventListener("pointercancel", this._onPointerUp, { passive: true });
}
||
}
widget_defs("TouchDetect") {
if "count(path) != 1"
error > TouchDetect id="«@id»": exactly one HMI path is required (e.g. HMI:TouchDetect@/PLC_TOUCH_ACTIVE)
}