Skip to content

Commit

Permalink
Merge pull request #23 from ahoym/disable_modifier_keys
Browse files Browse the repository at this point in the history
Prevent keyboard from playing if modifier key is pressed
  • Loading branch information
stuartmemo committed Sep 23, 2015
2 parents ad44961 + fb6b49e commit b40cd22
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
29 changes: 25 additions & 4 deletions lib/key-simulator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// via http://stackoverflow.com/questions/10455626/keydown-simulation-in-chrome-fires-normally-but-not-the-correct-key/10520017#10520017
var pressKey = function(k) {
var pressKey = function(k, options) {
options = options || {};
var oEvent = document.createEvent('KeyboardEvent');

// Chromium Hack
Expand All @@ -14,10 +15,30 @@ var pressKey = function(k) {
}
});

if (oEvent.initKeyboardEvent) {
oEvent.initKeyboardEvent('keydown', true, true, document.defaultView, false, false, false, false, k, k);
if (oEvent.initKeyEvent) {
oEvent.initKeyEvent('keydown',
true, // bubbles
true, // cancelable
document.defaultView, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
!!options.metaKey, // metaKeyArg
k, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
} else {
oEvent.initKeyEvent('keydown', true, true, document.defaultView, false, false, false, false, k, 0);
oEvent.initKeyboardEvent('keydown',
true, // bubbles
true, // cancelable
document.defaultView, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
!!options.metaKey, // metaKeyArg
k, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
}

oEvent.keyCodeVal = k;
Expand Down
14 changes: 14 additions & 0 deletions src/qwerty-hancock.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,14 @@
}
};

/**
* Determine whether pressed key is a modifier key or not.
* @param {KeyboardEvent} The keydown event of a pressed key
*/
var isModifierKey = function (key) {
return key.ctrlKey || key.metaKey || key.altKey;
};

/**
* Add event listeners to keyboard.
* @param {element} keyboard_element
Expand All @@ -443,11 +451,17 @@

// Key is pressed down on keyboard.
globalWindow.addEventListener('keydown', function (key) {
if (isModifierKey(key)) {
return;
}
keyboardDown(key, that.keyDown);
});

// Key is released on keyboard.
globalWindow.addEventListener('keyup', function (key) {
if (isModifierKey(key)) {
return;
}
keyboardUp(key, that.keyUp);
});

Expand Down
10 changes: 10 additions & 0 deletions tests/qh-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ describe('Qwerty Hancock tests', function () {
expect(c4_key.style.backgroundColor).toBe('yellow');
});

it('When user presses modifier key on computer keyboard, related keyboard key should not change colour', function () {
var qh = new QwertyHancock(),
d4_key = document.querySelector('#D4');

pressKey(83, { metaKey: true });

expect(d4_key.style.backgroundColor).not.toBe('yellow');
expect(d4_key.style.backgroundColor).toBe('rgb(255, 255, 255)');
});

afterEach(function () {
document.body.removeChild(element);
});
Expand Down

0 comments on commit b40cd22

Please sign in to comment.