What is what-input?
The what-input npm package is a utility that helps you track the current input method (mouse, keyboard, touch, etc.) being used by the user. It can be particularly useful for improving accessibility and user experience by allowing developers to tailor interactions based on the input method.
What are what-input's main functionalities?
Detecting Input Method
This feature allows you to detect the current input method being used by the user. The `ask` method returns a string representing the input method, such as 'mouse', 'keyboard', or 'touch'.
const whatInput = require('what-input');
console.log(whatInput.ask()); // Logs the current input method, e.g., 'mouse', 'keyboard', 'touch'
Listening for Input Method Changes
This feature allows you to set up a listener that triggers a callback function whenever the input method changes. This can be useful for dynamically adjusting the user interface based on the input method.
const whatInput = require('what-input');
whatInput.onChange(function(method) {
console.log('Input method changed to:', method);
});
Getting Specific Input Information
This feature provides additional information about the input methods and key codes that the library can detect. The `types` method returns an array of all possible input types, while the `keys` method returns an array of all possible key codes.
const whatInput = require('what-input');
console.log(whatInput.types()); // Logs an array of all possible input types
console.log(whatInput.keys()); // Logs an array of all possible key codes
Other packages similar to what-input
detect-it
The detect-it package provides similar functionality by detecting the primary input method (mouse, touch, or keyboard) and whether the device supports touch. It also offers additional features like detecting passive event listeners and pointer events. Compared to what-input, detect-it provides a broader range of device capability detections.
What Input?
A global utility for tracking the current input method (mouse, keyboard or touch).
What Input improves on track-focus by adding a data attribute on the <body>
instead of littering the DOM with classes on elements that have been interacted with. It also exposes a simple API that can be used for scripting interactions.
How it works
What Input uses event bubbling on the <body>
to watch for mouse, keyboard and touch events (via mousedown
, keydown
and touchstart
). It then sets or updates a data-whatinput
on the <body>
.
Since the form fields input
and textarea
rely on the keyboard as their only means of input, What Input does not switch the input type to keyboard when typing to preserve the last detected input type. To override this behavior and allow the keyboard to be recorded, add:
<body data-whatinput-formtyping>
Where present, Pointer Events are supported, but note that pen
inputs are remapped to touch
.
What Input also exposes a tiny API that allows the developer to ask for or set the current input.
What Input does not make assumptions about the input environment before the user makes their first interaction.
Installing
Download the file directly...
or install via Bower...
bower install what-input
or install via NPM...
npm install what-input
Usage
Include the script directly in your project.
<script src="assets/scripts/what-input.js"></script>
Or require with a script loader (What Input is AMD compatible).
require('what-input');
What Input will start doing its thing while you do yours.
Example Styling
a:focus {
outline: 3px dotted #06c;
}
[data-whatinput="mouse"] a:focus,
[data-whatinput="touch"] a:focus {
outline: none;
}
Scripting
Current Input
Ask What Input what the current input method is. This works best if asked after the events What Input is bound to (mousedown
, keydown
and touchstart
). Because click
always executes last in the event tree, What Input will be able to answer with the event that just happened.
whatInput.ask();
myButton.addEventListener('click', function() {
if (whatInput.ask() === 'mouse') {
} else if (whatInput.ask() === 'keyboard') {
}
});
Ask What Input to return an array of all the input types that have been used so far.
whatInput.types();
Tell What Input what's being used. This can be useful if you'd like to set an input method before the user has actually interacted with the page. What Input is not so assumptive on its own.
whatInput.set('hamster');
whatInput.ask();
Key Logging
Along with tracking the use of the keyboard, What Input keeps track of the currently pressed keys and stores them in an array. Instead of returning cryptic key codes, What Input uses plain language.
This can be used if, for example, you want to track how an element is being interacted with.
whatInput.keys();
myMenuTab.addEventListener('keyup', function() {
if (whatInput.keys().indexOf('down') !== -1) {
}
});
What Input only responds to the following "action" keys: 'tab', 'enter', 'shift', 'esc', 'space', 'left', 'up', 'right' and 'down'.
Compatibility
What Input works in all modern browsers. For compatibility with IE8, polyfills are required for:
Add your own, or grab the bundle included here.
Demo
Check out the demo to see What Input in action.
http://ten1seven.github.io/what-input
Acknowledgments
Special thanks to Viget for their encouragement and commitment to open source projects. Visit code.viget.com to see more projects from Viget.
What Input is written and maintained by @ten1seven.
License
What Input is freely available under the MIT License.