Commit 31f169e8 authored by Solly Ross's avatar Solly Ross

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
parent d6e281ba
...@@ -15,7 +15,7 @@ var kbdUtil = (function() { ...@@ -15,7 +15,7 @@ var kbdUtil = (function() {
var sub = substitutions[cp]; var sub = substitutions[cp];
return sub ? sub : cp; return sub ? sub : cp;
}; }
function isMac() { function isMac() {
return navigator && !!(/mac/i).exec(navigator.platform); return navigator && !!(/mac/i).exec(navigator.platform);
...@@ -387,17 +387,22 @@ function VerifyCharModifier(next) { ...@@ -387,17 +387,22 @@ function VerifyCharModifier(next) {
if (timer) { if (timer) {
return; return;
} }
var delayProcess = function () {
clearTimeout(timer);
timer = null;
process();
};
while (queue.length !== 0) { while (queue.length !== 0) {
var cur = queue[0]; var cur = queue[0];
queue = queue.splice(1); queue = queue.splice(1);
switch (cur.type) { switch (cur.type) {
case 'stall': case 'stall':
// insert a delay before processing available events. // insert a delay before processing available events.
timer = setTimeout(function() { /* jshint loopfunc: true */
clearTimeout(timer); timer = setTimeout(delayProcess, 5);
timer = null; /* jshint loopfunc: false */
process();
}, 5);
return; return;
case 'keydown': case 'keydown':
// is the next element a keypress? Then we should merge the two // is the next element a keypress? Then we should merge the two
...@@ -489,23 +494,25 @@ function TrackKeyState(next) { ...@@ -489,23 +494,25 @@ function TrackKeyState(next) {
var item = state.splice(idx, 1)[0]; var item = state.splice(idx, 1)[0];
// for each keysym tracked by this key entry, clone the current event and override the keysym // for each keysym tracked by this key entry, clone the current event and override the keysym
for (var key in item.keysyms) {
var clone = (function(){ var clone = (function(){
function Clone(){} function Clone(){}
return function (obj) { Clone.prototype=obj; return new Clone(); }; return function (obj) { Clone.prototype=obj; return new Clone(); };
}()); }());
for (var key in item.keysyms) {
var out = clone(evt); var out = clone(evt);
out.keysym = item.keysyms[key]; out.keysym = item.keysyms[key];
next(out); next(out);
} }
break; break;
case 'releaseall': case 'releaseall':
/* jshint shadow: true */
for (var i = 0; i < state.length; ++i) { for (var i = 0; i < state.length; ++i) {
for (var key in state[i].keysyms) { for (var key in state[i].keysyms) {
var keysym = state[i].keysyms[key]; var keysym = state[i].keysyms[key];
next({keyId: 0, keysym: keysym, type: 'keyup'}); next({keyId: 0, keysym: keysym, type: 'keyup'});
} }
} }
/* jshint shadow: false */
state = []; state = [];
} }
}; };
...@@ -527,8 +534,10 @@ function EscapeModifiers(next) { ...@@ -527,8 +534,10 @@ function EscapeModifiers(next) {
// send the character event // send the character event
next(evt); next(evt);
// redo modifiers // redo modifiers
/* jshint shadow: true */
for (var i = 0; i < evt.escape.length; ++i) { for (var i = 0; i < evt.escape.length; ++i) {
next({type: 'keydown', keyId: 0, keysym: keysyms.lookup(evt.escape[i])}); next({type: 'keydown', keyId: 0, keysym: keysyms.lookup(evt.escape[i])});
} }
/* jshint shadow: false */
}; };
} }
var assert = chai.assert; // requires local modules: keysym, keysymdef, keyboard
var assert = chai.assert;
var expect = chai.expect; var expect = chai.expect;
describe('Helpers', function() { describe('Helpers', function() {
......
// requires local modules: input, keyboard, keysymdef
var assert = chai.assert; var assert = chai.assert;
var expect = chai.expect; var expect = chai.expect;
/* jshint newcap: false, expr: true */
describe('Key Event Pipeline Stages', function() { describe('Key Event Pipeline Stages', function() {
"use strict"; "use strict";
describe('Decode Keyboard Events', function() { describe('Decode Keyboard Events', function() {
...@@ -50,7 +51,7 @@ describe('Key Event Pipeline Stages', function() { ...@@ -50,7 +51,7 @@ describe('Key Event Pipeline Stages', function() {
KeyEventDecoder(kbdUtil.ModifierSync(), function(evt) { KeyEventDecoder(kbdUtil.ModifierSync(), function(evt) {
expect(evt).to.be.deep.equal({keyId: 0x41, type: 'keydown'}); expect(evt).to.be.deep.equal({keyId: 0x41, type: 'keydown'});
done(); done();
}).keydown({keyCode: 0x41}) }).keydown({keyCode: 0x41});
}); });
it('should forward keyup events with the right type', function(done) { it('should forward keyup events with the right type', function(done) {
KeyEventDecoder(kbdUtil.ModifierSync(), function(evt) { KeyEventDecoder(kbdUtil.ModifierSync(), function(evt) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment