Skip to content

Commit

Permalink
Ignores shift, control and alt modifier keys because they're used for…
Browse files Browse the repository at this point in the history
… both mouse and keyboard actions
  • Loading branch information
ten1seven committed Feb 24, 2016
1 parent 1ee8f1f commit 41310e9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 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.4",
"version": "1.2.5",
"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.4",
"version": "1.2.5",
"description": "A global utility for tracking the current input method (mouse, keyboard or touch).",
"main": "what-input.js",
"repository": {
Expand Down
19 changes: 15 additions & 4 deletions what-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@
// via https://developer.mozilla.org/en-US/docs/Web/Events/wheel
var mouseWheel = detectWheel();

// list of modifier keys commonly used with the mouse and
// can be safely ignored to prevent false keyboard detection
var ignoreMap = [
16, // shift
17, // control
18 // alt
];

// mapping of events to input types
var inputMap = {
'keydown': 'keyboard',
Expand All @@ -64,7 +72,7 @@
// array of all used input types
var inputTypes = [];

// mapping of key codes to common name
// mapping of key codes to a common name
var keyMap = {
9: 'tab',
13: 'enter',
Expand Down Expand Up @@ -130,7 +138,7 @@
var eventTargetType = (eventTargetNode === 'input') ? eventTarget.getAttribute('type') : null;

if (
// only if the user flag to allow typing in form fields isn't set
(// only if the user flag to allow typing in form fields isn't set
!body.hasAttribute('data-whatinput-formtyping') &&

// only if currentInput has a value
Expand All @@ -147,9 +155,12 @@
eventTargetNode === 'textarea' ||
eventTargetNode === 'select' ||
(eventTargetNode === 'input' && nonTypingInputs.indexOf(eventTargetType) < 0)
)) || (
// ignore modifier keys
ignoreMap.indexOf(eventKey) > -1
)
) {
// ignore keyboard typing on form elements
// ignore keyboard typing
} else {
switchInput(value);
}
Expand Down Expand Up @@ -218,7 +229,7 @@
// mouse wheel
body.addEventListener(mouseWheel, bufferedEvent);

// keyboard
// keyboard events
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 41310e9

Please sign in to comment.