2022-11-20 20:03:07 -06:00
|
|
|
const inputConfig = {
|
2022-11-20 21:02:25 -06:00
|
|
|
clickRadius: 10, // Radius to be considered a click (pixels). If farther, turns into a drag
|
|
|
|
clickTiming: 500, // Timing window to be considered a click (ms). If longer, turns into a drag
|
|
|
|
dClickTiming: 500, // Timing window to be considered a double click (ms).
|
2022-11-21 20:19:41 -06:00
|
|
|
|
2022-11-23 22:53:14 -06:00
|
|
|
keyboardHoldTiming: 1000, // Timing window after which to consider holding a key (ms)
|
2022-11-20 20:03:07 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mouse input processing
|
|
|
|
*/
|
|
|
|
// Base object generator functions
|
2022-11-27 06:49:02 -06:00
|
|
|
function _mouse_observers(name = "generic_mouse_observer_array") {
|
|
|
|
return makeReadOnly(
|
|
|
|
{
|
|
|
|
// Simple click handler
|
|
|
|
onclick: new Observer(),
|
|
|
|
// Double click handler (will still trigger simple click handler as well)
|
|
|
|
ondclick: new Observer(),
|
|
|
|
// Drag handler
|
|
|
|
ondragstart: new Observer(),
|
|
|
|
ondrag: new Observer(),
|
|
|
|
ondragend: new Observer(),
|
|
|
|
// Paint handler (like drag handler, but with no delay); will trigger during clicks too
|
|
|
|
onpaintstart: new Observer(),
|
|
|
|
onpaint: new Observer(),
|
|
|
|
onpaintend: new Observer(),
|
|
|
|
},
|
|
|
|
name
|
|
|
|
);
|
2022-11-20 20:03:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const mouse = {
|
2022-11-27 06:49:02 -06:00
|
|
|
_contexts: [],
|
2022-11-23 22:53:14 -06:00
|
|
|
buttons: {},
|
2022-11-27 06:49:02 -06:00
|
|
|
coords: makeWriteOnce({}, "mouse.coords"),
|
2022-11-23 22:53:14 -06:00
|
|
|
|
2022-11-27 06:49:02 -06:00
|
|
|
listen: makeWriteOnce({}, "mouse.listen"),
|
2022-11-23 22:53:14 -06:00
|
|
|
|
|
|
|
// Register Context
|
|
|
|
registerContext: (name, onmove, options = {}) => {
|
|
|
|
// Options
|
|
|
|
defaultOpt(options, {
|
|
|
|
target: null,
|
|
|
|
buttons: {0: "left", 1: "middle", 2: "right"},
|
|
|
|
});
|
2022-11-20 21:02:25 -06:00
|
|
|
|
2022-11-23 22:53:14 -06:00
|
|
|
// Context information
|
|
|
|
const context = {
|
|
|
|
id: guid(),
|
|
|
|
name,
|
|
|
|
onmove,
|
|
|
|
target: options.target,
|
|
|
|
buttons: options.buttons,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Coordinate information
|
|
|
|
mouse.coords[name] = {
|
|
|
|
dragging: {},
|
|
|
|
|
|
|
|
prev: {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
},
|
|
|
|
|
|
|
|
pos: {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// Listeners
|
|
|
|
mouse.listen[name] = {
|
|
|
|
onwheel: new Observer(),
|
|
|
|
onmousemove: new Observer(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Button specific items
|
|
|
|
Object.keys(options.buttons).forEach((index) => {
|
|
|
|
const button = options.buttons[index];
|
|
|
|
mouse.coords[name].dragging[button] = null;
|
2022-11-27 06:49:02 -06:00
|
|
|
mouse.listen[name][button] = _mouse_observers(
|
|
|
|
`mouse.listen[${name}][${button}]`
|
|
|
|
);
|
2022-11-23 22:53:14 -06:00
|
|
|
});
|
2022-11-20 21:02:25 -06:00
|
|
|
|
2022-11-23 22:53:14 -06:00
|
|
|
// Add to context
|
|
|
|
context.coords = mouse.coords[name];
|
|
|
|
context.listen = mouse.listen[name];
|
2022-11-20 21:02:25 -06:00
|
|
|
|
2022-11-23 22:53:14 -06:00
|
|
|
// Add to list
|
2022-11-27 06:49:02 -06:00
|
|
|
mouse._contexts.push(context);
|
2022-11-20 21:02:25 -06:00
|
|
|
|
2022-11-23 22:53:14 -06:00
|
|
|
return context;
|
2022-11-20 21:02:25 -06:00
|
|
|
},
|
2022-11-20 20:03:07 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
const _double_click_timeout = {};
|
|
|
|
const _drag_start_timeout = {};
|
|
|
|
|
|
|
|
window.onmousedown = (evn) => {
|
2022-11-23 22:53:14 -06:00
|
|
|
const time = performance.now();
|
|
|
|
|
|
|
|
if (_double_click_timeout[evn.button]) {
|
|
|
|
// ondclick event
|
2022-11-27 06:49:02 -06:00
|
|
|
mouse._contexts.forEach(({target, name, buttons}) => {
|
2022-11-23 22:53:14 -06:00
|
|
|
if ((!target || target === evn.target) && buttons[evn.button])
|
|
|
|
mouse.listen[name][buttons[evn.button]].ondclick.emit({
|
2022-11-20 21:02:25 -06:00
|
|
|
target: evn.target,
|
|
|
|
buttonId: evn.button,
|
2022-11-23 22:53:14 -06:00
|
|
|
x: mouse.coords[name].pos.x,
|
|
|
|
y: mouse.coords[name].pos.y,
|
|
|
|
evn,
|
|
|
|
timestamp: time,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Start timer
|
|
|
|
_double_click_timeout[evn.button] = setTimeout(
|
|
|
|
() => delete _double_click_timeout[evn.button],
|
|
|
|
inputConfig.dClickTiming
|
|
|
|
);
|
|
|
|
}
|
2022-11-20 21:02:25 -06:00
|
|
|
|
2022-11-23 22:53:14 -06:00
|
|
|
// Set drag start timeout
|
|
|
|
_drag_start_timeout[evn.button] = setTimeout(() => {
|
2022-11-27 06:49:02 -06:00
|
|
|
mouse._contexts.forEach(({target, name, buttons}) => {
|
2022-11-23 22:53:14 -06:00
|
|
|
const key = buttons[evn.button];
|
|
|
|
if (
|
|
|
|
(!target || target === evn.target) &&
|
|
|
|
!mouse.coords[name].dragging[key].drag &&
|
|
|
|
key
|
|
|
|
) {
|
|
|
|
mouse.listen[name][key].ondragstart.emit({
|
2022-11-20 21:02:25 -06:00
|
|
|
target: evn.target,
|
|
|
|
buttonId: evn.button,
|
2022-11-23 22:53:14 -06:00
|
|
|
x: mouse.coords[name].pos.x,
|
|
|
|
y: mouse.coords[name].pos.y,
|
|
|
|
evn,
|
|
|
|
timestamp: time,
|
2022-11-20 21:02:25 -06:00
|
|
|
});
|
|
|
|
|
2022-11-23 22:53:14 -06:00
|
|
|
mouse.coords[name].dragging[key].drag = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
delete _drag_start_timeout[evn.button];
|
|
|
|
}, inputConfig.clickTiming);
|
2022-11-20 21:02:25 -06:00
|
|
|
|
2022-11-23 22:53:14 -06:00
|
|
|
mouse.buttons[evn.button] = time;
|
|
|
|
|
2022-11-27 06:49:02 -06:00
|
|
|
mouse._contexts.forEach(({target, name, buttons}) => {
|
2022-11-23 22:53:14 -06:00
|
|
|
const key = buttons[evn.button];
|
|
|
|
if ((!target || target === evn.target) && key) {
|
|
|
|
mouse.coords[name].dragging[key] = {};
|
|
|
|
mouse.coords[name].dragging[key].target = evn.target;
|
|
|
|
Object.assign(mouse.coords[name].dragging[key], mouse.coords[name].pos);
|
2022-11-20 21:02:25 -06:00
|
|
|
|
|
|
|
// onpaintstart event
|
2022-11-23 22:53:14 -06:00
|
|
|
mouse.listen[name][key].onpaintstart.emit({
|
2022-11-20 21:02:25 -06:00
|
|
|
target: evn.target,
|
|
|
|
buttonId: evn.button,
|
2022-11-23 22:53:14 -06:00
|
|
|
x: mouse.coords[name].pos.x,
|
|
|
|
y: mouse.coords[name].pos.y,
|
|
|
|
evn,
|
|
|
|
timestamp: performance.now(),
|
2022-11-20 21:02:25 -06:00
|
|
|
});
|
2022-11-23 22:53:14 -06:00
|
|
|
}
|
|
|
|
});
|
2022-11-20 20:03:07 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
window.onmouseup = (evn) => {
|
2022-11-23 22:53:14 -06:00
|
|
|
const time = performance.now();
|
|
|
|
|
2022-11-27 06:49:02 -06:00
|
|
|
mouse._contexts.forEach(({target, name, buttons}) => {
|
2022-11-23 22:53:14 -06:00
|
|
|
const key = buttons[evn.button];
|
|
|
|
if (
|
|
|
|
(!target || target === evn.target) &&
|
|
|
|
key &&
|
|
|
|
mouse.coords[name].dragging[key]
|
|
|
|
) {
|
2022-11-20 21:02:25 -06:00
|
|
|
const start = {
|
2022-11-23 22:53:14 -06:00
|
|
|
x: mouse.coords[name].dragging[key].x,
|
|
|
|
y: mouse.coords[name].dragging[key].y,
|
2022-11-20 21:02:25 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// onclick event
|
2022-11-23 22:53:14 -06:00
|
|
|
const dx = mouse.coords[name].pos.x - start.x;
|
|
|
|
const dy = mouse.coords[name].pos.y - start.y;
|
2022-11-20 21:02:25 -06:00
|
|
|
|
|
|
|
if (
|
2022-11-23 22:53:14 -06:00
|
|
|
mouse.buttons[evn.button] &&
|
|
|
|
time - mouse.buttons[evn.button] < inputConfig.clickTiming &&
|
2022-11-20 21:02:25 -06:00
|
|
|
dx * dx + dy * dy < inputConfig.clickRadius * inputConfig.clickRadius
|
|
|
|
)
|
2022-11-23 22:53:14 -06:00
|
|
|
mouse.listen[name][key].onclick.emit({
|
2022-11-20 21:02:25 -06:00
|
|
|
target: evn.target,
|
|
|
|
buttonId: evn.button,
|
2022-11-23 22:53:14 -06:00
|
|
|
x: mouse.coords[name].pos.x,
|
|
|
|
y: mouse.coords[name].pos.y,
|
|
|
|
evn,
|
|
|
|
timestamp: performance.now(),
|
2022-11-20 21:02:25 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
// onpaintend event
|
2022-11-23 22:53:14 -06:00
|
|
|
mouse.listen[name][key].onpaintend.emit({
|
2022-11-20 21:02:25 -06:00
|
|
|
target: evn.target,
|
2022-11-23 22:53:14 -06:00
|
|
|
initialTarget: mouse.coords[name].dragging[key].target,
|
2022-11-20 21:02:25 -06:00
|
|
|
buttonId: evn.button,
|
2022-11-24 09:30:13 -06:00
|
|
|
ix: mouse.coords[name].dragging[key].x,
|
|
|
|
iy: mouse.coords[name].dragging[key].y,
|
2022-11-23 22:53:14 -06:00
|
|
|
x: mouse.coords[name].pos.x,
|
|
|
|
y: mouse.coords[name].pos.y,
|
|
|
|
evn,
|
|
|
|
timestamp: performance.now(),
|
2022-11-20 21:02:25 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
// ondragend event
|
2022-11-23 22:53:14 -06:00
|
|
|
if (mouse.coords[name].dragging[key].drag)
|
|
|
|
mouse.listen[name][key].ondragend.emit({
|
2022-11-20 21:02:25 -06:00
|
|
|
target: evn.target,
|
2022-11-23 22:53:14 -06:00
|
|
|
initialTarget: mouse.coords[name].dragging[key].target,
|
2022-11-20 21:02:25 -06:00
|
|
|
buttonId: evn.button,
|
2022-11-24 09:30:13 -06:00
|
|
|
ix: mouse.coords[name].dragging[key].x,
|
|
|
|
iy: mouse.coords[name].dragging[key].y,
|
2022-11-23 22:53:14 -06:00
|
|
|
x: mouse.coords[name].pos.x,
|
|
|
|
y: mouse.coords[name].pos.y,
|
|
|
|
evn,
|
|
|
|
timestamp: performance.now(),
|
2022-11-20 21:02:25 -06:00
|
|
|
});
|
|
|
|
|
2022-11-23 22:53:14 -06:00
|
|
|
mouse.coords[name].dragging[key] = null;
|
2022-11-20 21:02:25 -06:00
|
|
|
}
|
2022-11-23 22:53:14 -06:00
|
|
|
});
|
2022-11-20 21:02:25 -06:00
|
|
|
|
2022-11-23 22:53:14 -06:00
|
|
|
if (_drag_start_timeout[evn.button] !== undefined) {
|
|
|
|
clearTimeout(_drag_start_timeout[evn.button]);
|
|
|
|
delete _drag_start_timeout[evn.button];
|
|
|
|
}
|
|
|
|
mouse.buttons[evn.button] = null;
|
2022-11-20 20:03:07 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
window.onmousemove = (evn) => {
|
2022-11-27 06:49:02 -06:00
|
|
|
mouse._contexts.forEach((context) => {
|
2022-11-23 22:53:14 -06:00
|
|
|
const target = context.target;
|
|
|
|
const name = context.name;
|
2022-11-20 21:02:25 -06:00
|
|
|
|
2022-11-23 22:53:14 -06:00
|
|
|
if (!target || target === evn.target) {
|
|
|
|
context.onmove(evn, context);
|
2022-11-20 21:02:25 -06:00
|
|
|
|
2022-11-23 22:53:14 -06:00
|
|
|
mouse.listen[name].onmousemove.emit({
|
|
|
|
target: evn.target,
|
|
|
|
px: mouse.coords[name].prev.x,
|
|
|
|
py: mouse.coords[name].prev.y,
|
|
|
|
x: mouse.coords[name].pos.x,
|
|
|
|
y: mouse.coords[name].pos.y,
|
|
|
|
evn,
|
|
|
|
timestamp: performance.now(),
|
|
|
|
});
|
2022-11-20 21:02:25 -06:00
|
|
|
|
2022-11-23 22:53:14 -06:00
|
|
|
Object.keys(context.buttons).forEach((index) => {
|
|
|
|
const key = context.buttons[index];
|
|
|
|
// ondragstart event (2)
|
|
|
|
if (mouse.coords[name].dragging[key]) {
|
|
|
|
const dx =
|
|
|
|
mouse.coords[name].pos.x - mouse.coords[name].dragging[key].x;
|
|
|
|
const dy =
|
|
|
|
mouse.coords[name].pos.y - mouse.coords[name].dragging[key].y;
|
|
|
|
if (
|
|
|
|
!mouse.coords[name].dragging[key].drag &&
|
|
|
|
dx * dx + dy * dy >=
|
|
|
|
inputConfig.clickRadius * inputConfig.clickRadius
|
|
|
|
) {
|
|
|
|
mouse.listen[name][key].ondragstart.emit({
|
|
|
|
target: evn.target,
|
|
|
|
buttonId: evn.button,
|
2022-11-24 09:30:13 -06:00
|
|
|
ix: mouse.coords[name].dragging[key].x,
|
|
|
|
iy: mouse.coords[name].dragging[key].y,
|
2022-11-23 22:53:14 -06:00
|
|
|
x: mouse.coords[name].pos.x,
|
|
|
|
y: mouse.coords[name].pos.y,
|
|
|
|
evn,
|
|
|
|
timestamp: performance.now(),
|
|
|
|
});
|
|
|
|
|
|
|
|
mouse.coords[name].dragging[key].drag = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ondrag event
|
|
|
|
if (
|
|
|
|
mouse.coords[name].dragging[key] &&
|
|
|
|
mouse.coords[name].dragging[key].drag
|
|
|
|
)
|
|
|
|
mouse.listen[name][key].ondrag.emit({
|
|
|
|
target: evn.target,
|
|
|
|
initialTarget: mouse.coords[name].dragging[key].target,
|
|
|
|
button: index,
|
2022-11-24 09:30:13 -06:00
|
|
|
ix: mouse.coords[name].dragging[key].x,
|
|
|
|
iy: mouse.coords[name].dragging[key].y,
|
2022-11-23 22:53:14 -06:00
|
|
|
px: mouse.coords[name].prev.x,
|
|
|
|
py: mouse.coords[name].prev.y,
|
|
|
|
x: mouse.coords[name].pos.x,
|
|
|
|
y: mouse.coords[name].pos.y,
|
|
|
|
evn,
|
|
|
|
timestamp: performance.now(),
|
|
|
|
});
|
|
|
|
|
|
|
|
// onpaint event
|
|
|
|
if (mouse.coords[name].dragging[key]) {
|
|
|
|
mouse.listen[name][key].onpaint.emit({
|
|
|
|
target: evn.target,
|
|
|
|
initialTarget: mouse.coords[name].dragging[key].target,
|
|
|
|
button: index,
|
2022-11-24 09:30:13 -06:00
|
|
|
ix: mouse.coords[name].dragging[key].x,
|
|
|
|
iy: mouse.coords[name].dragging[key].y,
|
2022-11-23 22:53:14 -06:00
|
|
|
px: mouse.coords[name].prev.x,
|
|
|
|
py: mouse.coords[name].prev.y,
|
|
|
|
x: mouse.coords[name].pos.x,
|
|
|
|
y: mouse.coords[name].pos.y,
|
|
|
|
evn,
|
|
|
|
timestamp: performance.now(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-11-20 21:02:25 -06:00
|
|
|
});
|
2022-11-20 20:03:07 -06:00
|
|
|
};
|
|
|
|
|
2022-11-22 16:24:55 -06:00
|
|
|
window.addEventListener(
|
|
|
|
"wheel",
|
|
|
|
(evn) => {
|
2022-11-27 06:49:02 -06:00
|
|
|
mouse._contexts.forEach(({name}) => {
|
2022-11-23 22:53:14 -06:00
|
|
|
mouse.listen[name].onwheel.emit({
|
2022-11-22 16:24:55 -06:00
|
|
|
target: evn.target,
|
|
|
|
delta: evn.deltaY,
|
|
|
|
deltaX: evn.deltaX,
|
|
|
|
deltaY: evn.deltaY,
|
|
|
|
deltaZ: evn.deltaZ,
|
|
|
|
mode: evn.deltaMode,
|
2022-11-23 22:53:14 -06:00
|
|
|
x: mouse.coords[name].pos.x,
|
|
|
|
y: mouse.coords[name].pos.y,
|
|
|
|
evn,
|
|
|
|
timestamp: performance.now(),
|
2022-11-22 16:24:55 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
{passive: false}
|
2022-11-20 20:03:07 -06:00
|
|
|
);
|
2022-11-23 22:53:14 -06:00
|
|
|
|
|
|
|
mouse.registerContext("window", (evn, ctx) => {
|
|
|
|
ctx.coords.prev.x = ctx.coords.pos.x;
|
|
|
|
ctx.coords.prev.y = ctx.coords.pos.y;
|
|
|
|
ctx.coords.pos.x = evn.clientX;
|
|
|
|
ctx.coords.pos.y = evn.clientY;
|
|
|
|
});
|
|
|
|
|
|
|
|
mouse.registerContext(
|
|
|
|
"canvas",
|
|
|
|
(evn, ctx) => {
|
|
|
|
ctx.coords.prev.x = ctx.coords.pos.x;
|
|
|
|
ctx.coords.prev.y = ctx.coords.pos.y;
|
|
|
|
ctx.coords.pos.x = evn.layerX;
|
|
|
|
ctx.coords.pos.y = evn.layerY;
|
|
|
|
},
|
|
|
|
document.getElementById("overlayCanvas")
|
|
|
|
);
|
2022-11-20 20:23:08 -06:00
|
|
|
/**
|
2022-11-21 20:19:41 -06:00
|
|
|
* Keyboard input processing
|
2022-11-20 20:23:08 -06:00
|
|
|
*/
|
2022-11-21 20:19:41 -06:00
|
|
|
// Base object generator functions
|
|
|
|
|
|
|
|
const keyboard = {
|
|
|
|
keys: {},
|
|
|
|
|
|
|
|
isPressed(code) {
|
|
|
|
return this.keys[key].pressed;
|
|
|
|
},
|
|
|
|
|
|
|
|
isHeld(code) {
|
|
|
|
return !!this;
|
|
|
|
},
|
|
|
|
|
|
|
|
shortcuts: {},
|
|
|
|
onShortcut(shortcut, callback) {
|
|
|
|
/**
|
|
|
|
* Adds a shortcut handler (shorcut must be in format: {ctrl?: bool, alt?: bool, shift?: bool, key: string (code)})
|
|
|
|
* key must be the "code" parameter from keydown event; A key is "KeyA" for example
|
|
|
|
*/
|
|
|
|
if (this.shortcuts[shortcut.key] === undefined)
|
|
|
|
this.shortcuts[shortcut.key] = [];
|
|
|
|
|
|
|
|
this.shortcuts[shortcut.key].push({
|
|
|
|
ctrl: shortcut.ctrl,
|
|
|
|
alt: shortcut.alt,
|
|
|
|
shift: shortcut.shift,
|
|
|
|
id: guid(),
|
|
|
|
callback,
|
|
|
|
});
|
|
|
|
},
|
2022-11-24 21:34:34 -06:00
|
|
|
deleteShortcut(id, key = null) {
|
|
|
|
if (key) {
|
|
|
|
this.shortcuts[key] = this.shortcuts[key].filter(
|
|
|
|
(v) => v.id !== id && v.callback !== id
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2022-11-21 20:19:41 -06:00
|
|
|
this.shortcuts.keys().forEach((key) => {
|
2022-11-24 09:30:13 -06:00
|
|
|
this.shortcuts[key] = this.shortcuts[key].filter(
|
|
|
|
(v) => v.id !== id && v.callback !== id
|
|
|
|
);
|
2022-11-21 20:19:41 -06:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
listen: {
|
|
|
|
onkeydown: new Observer(),
|
|
|
|
onkeyup: new Observer(),
|
|
|
|
onkeyholdstart: new Observer(),
|
|
|
|
onkeyholdend: new Observer(),
|
|
|
|
onkeyclick: new Observer(),
|
|
|
|
onshortcut: new Observer(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
window.onkeydown = (evn) => {
|
2022-11-23 22:53:14 -06:00
|
|
|
// onkeydown event
|
2022-11-21 20:19:41 -06:00
|
|
|
keyboard.listen.onkeydown.emit({
|
2022-11-23 22:53:14 -06:00
|
|
|
target: evn.target,
|
2022-11-21 20:19:41 -06:00
|
|
|
code: evn.code,
|
|
|
|
key: evn.key,
|
|
|
|
evn,
|
|
|
|
});
|
|
|
|
|
|
|
|
keyboard.keys[evn.code] = {
|
|
|
|
pressed: true,
|
|
|
|
held: false,
|
|
|
|
_hold_to: setTimeout(() => {
|
|
|
|
keyboard.keys[evn.code].held = true;
|
|
|
|
delete keyboard.keys[evn.code]._hold_to;
|
2022-11-23 22:53:14 -06:00
|
|
|
// onkeyholdstart event
|
2022-11-21 20:19:41 -06:00
|
|
|
keyboard.listen.onkeyholdstart.emit({
|
2022-11-23 22:53:14 -06:00
|
|
|
target: evn.target,
|
2022-11-21 20:19:41 -06:00
|
|
|
code: evn.code,
|
|
|
|
key: evn.key,
|
|
|
|
evn,
|
|
|
|
});
|
|
|
|
}, inputConfig.keyboardHoldTiming),
|
|
|
|
};
|
|
|
|
|
2022-11-22 19:24:04 -06:00
|
|
|
// Process shortcuts if input target is not a text field
|
2022-11-23 05:24:18 -06:00
|
|
|
switch (evn.target.tagName.toLowerCase()) {
|
|
|
|
case "input":
|
|
|
|
case "textarea":
|
|
|
|
case "select":
|
|
|
|
case "button":
|
|
|
|
return; // If in an input field, do not process shortcuts
|
|
|
|
default:
|
|
|
|
// Do nothing
|
|
|
|
break;
|
|
|
|
}
|
2022-11-22 19:24:04 -06:00
|
|
|
|
2022-11-21 20:19:41 -06:00
|
|
|
const callbacks = keyboard.shortcuts[evn.code];
|
|
|
|
|
|
|
|
if (callbacks)
|
|
|
|
callbacks.forEach((callback) => {
|
|
|
|
if (
|
|
|
|
!!callback.ctrl === evn.ctrlKey &&
|
|
|
|
!!callback.alt === evn.altKey &&
|
|
|
|
!!callback.shift === evn.shiftKey
|
|
|
|
) {
|
2022-11-23 22:53:14 -06:00
|
|
|
// onshortcut event
|
2022-11-21 20:19:41 -06:00
|
|
|
keyboard.listen.onshortcut.emit({
|
2022-11-23 22:53:14 -06:00
|
|
|
target: evn.target,
|
2022-11-21 20:19:41 -06:00
|
|
|
code: evn.code,
|
|
|
|
key: evn.key,
|
|
|
|
id: callback.id,
|
|
|
|
evn,
|
|
|
|
});
|
|
|
|
callback.callback(evn);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
window.onkeyup = (evn) => {
|
2022-11-23 22:53:14 -06:00
|
|
|
// onkeyup event
|
2022-11-21 20:19:41 -06:00
|
|
|
keyboard.listen.onkeyup.emit({
|
2022-11-23 22:53:14 -06:00
|
|
|
target: evn.target,
|
2022-11-21 20:19:41 -06:00
|
|
|
code: evn.code,
|
|
|
|
key: evn.key,
|
|
|
|
evn,
|
|
|
|
});
|
|
|
|
if (keyboard.keys[evn.code] && keyboard.keys[evn.code].held) {
|
2022-11-23 22:53:14 -06:00
|
|
|
// onkeyholdend event
|
2022-11-21 20:19:41 -06:00
|
|
|
keyboard.listen.onkeyholdend.emit({
|
2022-11-23 22:53:14 -06:00
|
|
|
target: evn.target,
|
2022-11-21 20:19:41 -06:00
|
|
|
code: evn.code,
|
|
|
|
key: evn.key,
|
|
|
|
evn,
|
|
|
|
});
|
|
|
|
} else {
|
2022-11-23 22:53:14 -06:00
|
|
|
// onkeyclick event
|
2022-11-21 20:19:41 -06:00
|
|
|
keyboard.listen.onkeyclick.emit({
|
2022-11-23 22:53:14 -06:00
|
|
|
target: evn.target,
|
2022-11-21 20:19:41 -06:00
|
|
|
code: evn.code,
|
|
|
|
key: evn.key,
|
|
|
|
evn,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
keyboard.keys[evn.code] = {
|
|
|
|
pressed: false,
|
|
|
|
held: false,
|
|
|
|
};
|
|
|
|
};
|