What-input

what-input

A global utility for tracking the current input method (mouse, keyboard or touch).

What Input adds data attributes to the tag based on the type of input being used.

It also exposes a simple API that can be used for scripting interactions.

GET IT: npm install --save what-input

Sample usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
whatInput.ask(); // returns `mouse`, `keyboard` or `touch`

whatInput.types(); // ex. returns ['mouse', 'keyboard']

whatInput.ask('loose'); // returns `mouse` because mouse movement was detected

myButton.addEventListener('click', function() {

  if (whatInput.ask() === 'mouse') {
    // do mousy things
  } else if (whatInput.ask() === 'keyboard') {
    // do keyboard things
  }

});

Event mapping:

1
2
3
4
5
6
7
8
9
10
11
// mapping of events to input types
const inputMap = {
    'keyup': 'keyboard',
    'mousedown': 'mouse',
    'mousemove': 'mouse',
    'MSPointerDown': 'pointer',
    'MSPointerMove': 'pointer',
    'pointerdown': 'pointer',
    'pointermove': 'pointer',
    'touchstart': 'touch'
};

GIF FTW:

what-input

Suggest a module

Comments