forked from samuel-p/sp-codes.de
7 lines
2.3 KiB
JavaScript
7 lines
2.3 KiB
JavaScript
|
/**
|
||
|
* what-input - A global utility for tracking the current input method (mouse, keyboard or touch).
|
||
|
* @version v2.0.1
|
||
|
* @link https://github.com/ten1seven/what-input
|
||
|
* @license MIT
|
||
|
*/
|
||
|
window.whatInput=function(){"use strict";function e(){o(),i(event),v=!0,L=window.setTimeout(function(){v=!1},650)}function t(e){v||i(e)}function n(e){o(),i(e)}function o(){window.clearTimeout(L)}function i(e){var t=u(e),n=b[e.type];if("pointer"===n&&(n=a(e)),y!==n){var o=d(e),i=o.nodeName.toLowerCase(),c="input"===i?o.getAttribute("type"):null;!f.hasAttribute("data-whatinput-formtyping")&&y&&"keyboard"===n&&"tab"!==M[t]&&("textarea"===i||"select"===i||"input"===i&&h.indexOf(c)<0)||E.indexOf(t)>-1||r(n)}"keyboard"===n&&s(t)}function r(e){y=e,f.setAttribute("data-whatinput",y),-1===k.indexOf(y)&&k.push(y)}function u(e){return e.keyCode?e.keyCode:e.which}function d(e){return e.target||e.srcElement}function a(e){return"number"==typeof e.pointerType?x[e.pointerType]:"pen"===e.pointerType?"touch":e.pointerType}function s(e){-1===m.indexOf(M[e])&&M[e]&&m.push(M[e])}function c(e){var t=u(e),n=m.indexOf(M[t]);-1!==n&&m.splice(n,1)}function p(){f=document.body,window.PointerEvent?(f.addEventListener("pointerdown",t),f.addEventListener("pointermove",t)):window.MSPointerEvent?(f.addEventListener("MSPointerDown",t),f.addEventListener("MSPointerMove",t)):(f.addEventListener("mousedown",t),f.addEventListener("mousemove",t),"ontouchstart"in window&&f.addEventListener("touchstart",e)),f.addEventListener(l,t),f.addEventListener("keydown",n),f.addEventListener("keyup",n),document.addEventListener("keyup",c)}function w(){return l="onwheel"in document.createElement("div")?"wheel":void 0!==document.onmousewheel?"mousewheel":"DOMMouseScroll"}var f,m=[],v=!1,y=null,h=["button","checkbox","file","image","radio","reset","submit"],l=w(),E=[16,17,18,91,93],b={keydown:"keyboard",keyup:"keyboard",mousedown:"mouse",mousemove:"mouse",MSPointerDown:"pointer",MSPointerMove:"pointer",pointerdown:"pointer",pointermove:"pointer",touchstart:"touch"};b[w()]="mouse";var L,k=[],M={9:"tab",13:"enter",16:"shift",27:"esc",32:"space",37:"left",38:"up",39:"right",40:"down"},x={2:"touch",3:"touch",4:"mouse"};return"addEventListener"in window&&Array.prototype.indexOf&&(document.body?p():document.addEventListener("DOMContentLoaded",p)),{ask:function(){return y},keys:function(){return m},types:function(){return k},set:r}}();
|