Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(function () {
var previousInputMode = false;
var inputMode = (function() {
try {
var inputMode = window.sessionStorage.getItem('inputMode');
if (!inputMode) {
return undefined;
}
if (window.ga) {
window.ga('set', 'dimension1', inputMode);
}
return inputMode;
} catch(e) {
return undefined;
}
})();
var touched = false;
var touchTimer;
var touchCount = 0;
var mouseMoved = false;
var mouseCount = 0;
var tabCount = 0;
var cursorCount = 0;
var spaceCount = 0;
var keyCount = 0;
function inputmodeDetect() {
switch (true) {
case touchCount > 1 && mouseCount > 50 && keyCount > 3:
inputMode = 'mixed touch, mouse and keyboard navigation';
break;
case touchCount > 1 && mouseCount > 50:
inputMode = 'mixed touch and mouse navigation';
break;
case touchCount > 1 && keyCount > 50:
inputMode = 'mixed touch and keyboard navigation';
break;
case mouseCount > 50 && keyCount > 3:
inputMode = 'mixed mouse and keyboard navigation';
break;
case touchCount > 1:
inputMode = 'touch only navigation';
break;
case mouseCount > 50:
inputMode = 'mouse only navigation';
break;
case keyCount > 3:
inputMode = 'keyboard only navigation';
break;
default:
inputMode = undefined;
break;
}
if (inputMode && inputMode !== previousInputMode) {
previousInputMode = inputMode;
try {
window.sessionStorage.setItem('inputMode', inputMode);
} catch (e) {}
console.log(inputMode);
if (window.ga) {
ga('set', 'dimension1', inputMode);
}
}
}
function touch() {
window.clearTimeout(touchTimer);
touchCount += 1;
touched = true;
touchTimer = window.setTimeout(function () {
touched = false;
}, 100);
inputmodeDetect();
}
function mousemove() {
mouseMoved = true;
}
function mousewheel() {
mouseMoved = true;
}
function mouse() {
if (!mouseMoved) {
return;
}
mouseMoved = false;
if (touched) {
return;
}
mouseCount += 1;
inputmodeDetect();
}
function keyboard(e) {
switch (true) {
default:
break;
// Tab key
case e.keyCode === 9:
tabCount += 1;
keyCount += 1;
break;
// Space key
case e.keyCode === 32:
if (e.target.matches('input[type]:not([type="text"]), select, button')) {
spaceCount += 1;
keyCount += 1;
}
break;
// Cursor keys
case e.keyCode === 37:
case e.keyCode === 38:
case e.keyCode === 39:
case e.keyCode === 40:
if (e.target.closest('select,.park-select')) {
cursorCount += 1;
keyCount += 1;
}
break;
}
inputmodeDetect();
}
document.addEventListener('keydown', keyboard, true);
document.addEventListener('touchstart', touch, {passive: true});
document.addEventListener('mousemove', mousemove, true);
document.addEventListener('mousewheel', mousewheel, {passive: true});
window.setInterval(mouse, 50);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment