diff --git a/what-input.js b/what-input.js index 58c4448..f0754f6 100644 --- a/what-input.js +++ b/what-input.js @@ -24,6 +24,9 @@ // cache document.body var body = document.body; + // boolean: true if touch buffer timer is running + var buffer = false; + // the last used input type var currentInput = null; @@ -76,6 +79,9 @@ 4: 'mouse' }; + // touch buffer timer + var timer; + /* --------------- @@ -83,6 +89,22 @@ --------------- */ + // allows events that are also triggered to be filtered out for `touchstart` + function eventBuffer() { + clearTimeout(timer); + + setInput(event); + + buffer = true; + timer = setTimeout(function() { + buffer = false; + }, 300); + } + + function bufferedEvent(event) { + if (!buffer) setInput(event); + } + function setInput(event) { var eventKey = key(event); var eventTarget = target(event); @@ -151,20 +173,20 @@ // pointer events (mouse, pen, touch) if (window.PointerEvent) { - body.addEventListener('pointerdown', setInput); - body.addEventListener('pointermove', setInput); + body.addEventListener('pointerdown', bufferedEvent); + body.addEventListener('pointermove', bufferedEvent); } else if (window.MSPointerEvent) { - body.addEventListener('MSPointerDown', setInput); - body.addEventListener('MSPointerMove', setInput); + body.addEventListener('MSPointerDown', bufferedEvent); + body.addEventListener('MSPointerMove', bufferedEvent); } else { // mouse events - body.addEventListener('mousedown', setInput); - body.addEventListener('mousemove', setInput); + body.addEventListener('mousedown', bufferedEvent); + body.addEventListener('mousemove', bufferedEvent); // touch events if ('ontouchstart' in window) { - body.addEventListener('touchstart', setInput); + body.addEventListener('touchstart', eventBuffer); } } diff --git a/what-input.min.js b/what-input.min.js index c5898e6..2884635 100644 --- a/what-input.min.js +++ b/what-input.min.js @@ -4,4 +4,4 @@ * @link https://github.com/ten1seven/what-input * @license MIT */ -!function(e,t){"function"==typeof define&&define.amd?define([],function(){return t()}):"object"==typeof exports?module.exports=t():e.whatInput=t()}(this,function(){"use strict";function e(e){var i=t(e),u=n(e),d=w[e.type];"pointer"===d&&(d=o(e)),p!==d&&(!a.hasAttribute("data-whatinput-formtyping")&&p&&"keyboard"===d&&"tab"!==v[i]&&c.indexOf(u.nodeName.toLowerCase())>=0||(p=d,a.setAttribute("data-whatinput",p),-1===m.indexOf(p)&&m.push(p))),"keyboard"===d&&r(i)}function t(e){return e.keyCode?e.keyCode:e.which}function n(e){return e.target||e.srcElement}function o(e){return"number"==typeof e.pointerType?h[e.pointerType]:"pen"===e.pointerType?"touch":e.pointerType}function r(e){-1===s.indexOf(v[e])&&v[e]&&s.push(v[e])}function i(e){var n=t(e),o=s.indexOf(v[n]);-1!==o&&s.splice(o,1)}function u(){window.PointerEvent?(a.addEventListener("pointerdown",e),a.addEventListener("pointermove",e)):window.MSPointerEvent?(a.addEventListener("MSPointerDown",e),a.addEventListener("MSPointerMove",e)):(a.addEventListener("mousedown",e),a.addEventListener("mousemove",e),"ontouchstart"in window&&a.addEventListener("touchstart",e)),a.addEventListener(f,e),a.addEventListener("keydown",e),document.addEventListener("keyup",i)}function d(){return f="onwheel"in document.createElement("div")?"wheel":void 0!==document.onmousewheel?"mousewheel":"DOMMouseScroll"}var s=[],a=document.body,p=null,c=["input","select","textarea"],f=d(),w={keydown:"keyboard",mousedown:"mouse",mousemove:"mouse",MSPointerDown:"pointer",MSPointerMove:"pointer",pointerdown:"pointer",pointermove:"pointer",touchstart:"touch"};w[d()]="mouse";var m=[],v={9:"tab",13:"enter",16:"shift",27:"esc",32:"space",37:"left",38:"up",39:"right",40:"down"},h={2:"touch",3:"touch",4:"mouse"};return"addEventListener"in window&&Array.prototype.indexOf&&u(),{ask:function(){return p},keys:function(){return s},types:function(){return m},set:e}}); \ No newline at end of file +!function(e,t){"function"==typeof define&&define.amd?define([],function(){return t()}):"object"==typeof exports?module.exports=t():e.whatInput=t()}(this,function(){"use strict";function e(){clearTimeout(y),n(event),f=!0,y=setTimeout(function(){f=!1},300)}function t(e){f||n(e)}function n(e){var t=o(e),n=i(e),d=h[e.type];"pointer"===d&&(d=r(e)),m!==d&&(!p.hasAttribute("data-whatinput-formtyping")&&m&&"keyboard"===d&&"tab"!==E[t]&&v.indexOf(n.nodeName.toLowerCase())>=0||(m=d,p.setAttribute("data-whatinput",m),-1===l.indexOf(m)&&l.push(m))),"keyboard"===d&&u(t)}function o(e){return e.keyCode?e.keyCode:e.which}function i(e){return e.target||e.srcElement}function r(e){return"number"==typeof e.pointerType?L[e.pointerType]:"pen"===e.pointerType?"touch":e.pointerType}function u(e){-1===c.indexOf(E[e])&&E[e]&&c.push(E[e])}function d(e){var t=o(e),n=c.indexOf(E[t]);-1!==n&&c.splice(n,1)}function s(){window.PointerEvent?(p.addEventListener("pointerdown",t),p.addEventListener("pointermove",t)):window.MSPointerEvent?(p.addEventListener("MSPointerDown",t),p.addEventListener("MSPointerMove",t)):(p.addEventListener("mousedown",t),p.addEventListener("mousemove",t),"ontouchstart"in window&&p.addEventListener("touchstart",e)),p.addEventListener(w,n),p.addEventListener("keydown",n),document.addEventListener("keyup",d)}function a(){return w="onwheel"in document.createElement("div")?"wheel":void 0!==document.onmousewheel?"mousewheel":"DOMMouseScroll"}var c=[],p=document.body,f=!1,m=null,v=["input","select","textarea"],w=a(),h={keydown:"keyboard",mousedown:"mouse",mousemove:"mouse",MSPointerDown:"pointer",MSPointerMove:"pointer",pointerdown:"pointer",pointermove:"pointer",touchstart:"touch"};h[a()]="mouse";var y,l=[],E={9:"tab",13:"enter",16:"shift",27:"esc",32:"space",37:"left",38:"up",39:"right",40:"down"},L={2:"touch",3:"touch",4:"mouse"};return"addEventListener"in window&&Array.prototype.indexOf&&s(),{ask:function(){return m},keys:function(){return c},types:function(){return l},set:n}}); \ No newline at end of file