Cleanup: Keyboard code

File: keyboard.js
Tests Added: False (already present)
Changes:
- Fixed JSHint Errors
- Moved functions outside loops
- Added proper include directives to tests
This commit is contained in:
Solly Ross 2014-05-20 19:16:01 -04:00
parent 0d9ae5170e
commit 38f7cb546e
3 changed files with 25 additions and 13 deletions

View File

@ -15,7 +15,7 @@ var kbdUtil = (function() {
var sub = substitutions[cp];
return sub ? sub : cp;
};
}
function isMac() {
return navigator && !!(/mac/i).exec(navigator.platform);
@ -387,17 +387,22 @@ function VerifyCharModifier(next) {
if (timer) {
return;
}
var delayProcess = function () {
clearTimeout(timer);
timer = null;
process();
};
while (queue.length !== 0) {
var cur = queue[0];
queue = queue.splice(1);
switch (cur.type) {
case 'stall':
// insert a delay before processing available events.
timer = setTimeout(function() {
clearTimeout(timer);
timer = null;
process();
}, 5);
/* jshint loopfunc: true */
timer = setTimeout(delayProcess, 5);
/* jshint loopfunc: false */
return;
case 'keydown':
// is the next element a keypress? Then we should merge the two
@ -489,23 +494,25 @@ function TrackKeyState(next) {
var item = state.splice(idx, 1)[0];
// for each keysym tracked by this key entry, clone the current event and override the keysym
var clone = (function(){
function Clone(){}
return function (obj) { Clone.prototype=obj; return new Clone(); };
}());
for (var key in item.keysyms) {
var clone = (function(){
function Clone(){}
return function (obj) { Clone.prototype=obj; return new Clone(); };
}());
var out = clone(evt);
out.keysym = item.keysyms[key];
next(out);
}
break;
case 'releaseall':
/* jshint shadow: true */
for (var i = 0; i < state.length; ++i) {
for (var key in state[i].keysyms) {
var keysym = state[i].keysyms[key];
next({keyId: 0, keysym: keysym, type: 'keyup'});
}
}
/* jshint shadow: false */
state = [];
}
};
@ -527,8 +534,10 @@ function EscapeModifiers(next) {
// send the character event
next(evt);
// redo modifiers
/* jshint shadow: true */
for (var i = 0; i < evt.escape.length; ++i) {
next({type: 'keydown', keyId: 0, keysym: keysyms.lookup(evt.escape[i])});
}
/* jshint shadow: false */
};
}

View File

@ -1,4 +1,6 @@
var assert = chai.assert;
// requires local modules: keysym, keysymdef, keyboard
var assert = chai.assert;
var expect = chai.expect;
describe('Helpers', function() {

View File

@ -1,7 +1,8 @@
// requires local modules: input, keyboard, keysymdef
var assert = chai.assert;
var expect = chai.expect;
/* jshint newcap: false, expr: true */
describe('Key Event Pipeline Stages', function() {
"use strict";
describe('Decode Keyboard Events', function() {
@ -50,7 +51,7 @@ describe('Key Event Pipeline Stages', function() {
KeyEventDecoder(kbdUtil.ModifierSync(), function(evt) {
expect(evt).to.be.deep.equal({keyId: 0x41, type: 'keydown'});
done();
}).keydown({keyCode: 0x41})
}).keydown({keyCode: 0x41});
});
it('should forward keyup events with the right type', function(done) {
KeyEventDecoder(kbdUtil.ModifierSync(), function(evt) {