mouse
or touch
).mouse
input initially and bind a 'touchstart'
event listener to the document.'touchstart'
, add a 'mousemove'
event listener to listen for two consecutive 'mousemove'
events firing within 20ms, using performance.now()
.const onUserInputChange = callback => {
let type = 'mouse',
lastTime = 0;
const mousemoveHandler = () => {
const now = performance.now();
if (now - lastTime < 20)
(type = 'mouse'),
callback(type),
document.removeEventListener('mousemove', mousemoveHandler);
lastTime = now;
};
document.addEventListener('touchstart', () => {
if (type === 'touch') return;
(type = 'touch'),
callback(type),
document.addEventListener('mousemove', mousemoveHandler);
});
};
onUserInputChange(type => {
console.log('The user is now using', type, 'as an input method.');
});
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️