Skip to content

Commit

Permalink
Adding support for keyup to help buffer mouse events fired from scree…
Browse files Browse the repository at this point in the history
…n readers
  • Loading branch information
ten1seven committed Feb 22, 2016
1 parent 2179ef1 commit 1ee8f1f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "what-input",
"version": "1.2.3",
"version": "1.2.4",
"homepage": "https://github.com/ten1seven/what-input",
"authors": [
"Jeremy Fields <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "what-input",
"version": "1.2.3",
"version": "1.2.4",
"description": "A global utility for tracking the current input method (mouse, keyboard or touch).",
"main": "what-input.js",
"repository": {
Expand Down
47 changes: 34 additions & 13 deletions what-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@
// the last used input type
var currentInput = null;

// array of form elements that take keyboard input
var formInputs = [
'input',
'select',
'textarea'
// `input` types that don't accept text
var nonTypingInputs = [
'button',
'checkbox',
'file',
'image',
'radio',
'reset',
'submit'
];

// detect version of mouse wheel event to use
Expand All @@ -44,6 +48,7 @@
// mapping of events to input types
var inputMap = {
'keydown': 'keyboard',
'keyup': 'keyboard',
'mousedown': 'mouse',
'mousemove': 'mouse',
'MSPointerDown': 'pointer',
Expand Down Expand Up @@ -91,12 +96,11 @@

// allows events that are also triggered to be filtered out for `touchstart`
function eventBuffer() {
clearTimeout(timer);

clearTimer();
setInput(event);

buffer = true;
timer = setTimeout(function() {
timer = window.setTimeout(function() {
buffer = false;
}, 650);
}
Expand All @@ -105,14 +109,26 @@
if (!buffer) setInput(event);
}

function unBufferedEvent(event) {
clearTimer();
setInput(event);
}

function clearTimer() {
window.clearTimeout(timer);
}

function setInput(event) {
var eventKey = key(event);
var eventTarget = target(event);
var value = inputMap[event.type];
if (value === 'pointer') value = pointerType(event);

// don't do anything if the value matches the input type already set
if (currentInput !== value) {
var eventTarget = target(event);
var eventTargetNode = eventTarget.nodeName.toLowerCase();
var eventTargetType = (eventTargetNode === 'input') ? eventTarget.getAttribute('type') : null;

if (
// only if the user flag to allow typing in form fields isn't set
!body.hasAttribute('data-whatinput-formtyping') &&
Expand All @@ -126,8 +142,12 @@
// not if the key is `TAB`
keyMap[eventKey] !== 'tab' &&

// only if the target is one of the elements in `formInputs`
formInputs.indexOf(eventTarget.nodeName.toLowerCase()) >= 0
// only if the target is a form input that accepts text
(
eventTargetNode === 'textarea' ||
eventTargetNode === 'select' ||
(eventTargetNode === 'input' && nonTypingInputs.indexOf(eventTargetType) < 0)
)
) {
// ignore keyboard typing on form elements
} else {
Expand Down Expand Up @@ -196,10 +216,11 @@
}

// mouse wheel
body.addEventListener(mouseWheel, setInput);
body.addEventListener(mouseWheel, bufferedEvent);

// keyboard
body.addEventListener('keydown', setInput);
body.addEventListener('keydown', unBufferedEvent);
body.addEventListener('keyup', unBufferedEvent);
document.addEventListener('keyup', unLogKeys);
}

Expand Down
4 changes: 2 additions & 2 deletions what-input.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1ee8f1f

Please sign in to comment.