Skip to content

Commit

Permalink
Fixed auto-repeat detection issue
Browse files Browse the repository at this point in the history
git-svn-id: http://ourlibrary.googlecode.com/svn/trunk@126 fad0cf9c-0ac3-11df-8f06-1508ab885915
  • Loading branch information
david-mark committed Sep 5, 2010
1 parent 5526f2c commit f149efe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion mylib-keyboard-min.js

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

51 changes: 31 additions & 20 deletions mylib-keyboard.js
Expand Up @@ -16,7 +16,7 @@
onnavkeypress (function) - called on arrow key, PageUp/Down and Esc key presses
callbackContext (object) - set the - this - object for callbacks
callbackContext (object) - sets the - this - object for callbacks
suppressControlKeyAutoRepeat - prevents repeated onkey callbacks for control keys
Expand All @@ -25,7 +25,7 @@
Test page at: http://www.cinsoft.net/mylib-keyboard.html
*/

var API, E, D, global = this;
var API, Q, E, D, global = this;

if (API && API.attachListener && Function.prototype.apply) {
(function() {
Expand Down Expand Up @@ -126,14 +126,20 @@ if (API && API.attachListener && Function.prototype.apply) {
}
}
} else if (!charCode && !suppressControlKeyAutoRepeat) {
if (lastControlKeyPress == keyCode) {
lastControlKeyPressRepeat++;
}
if (typeof autoRepeatModel == 'undefined') {
if (lastKeyDownRepeat) {
autoRepeatModel = lastControlKeyPressRepeat ? 'both' : 'down';
} else if (lastControlKeyPressRepeat) {
autoRepeatModel = 'press';

// Escape and Tab are not considered for auto-repeat detection
// as they can cause the focus to change, possibly losing a keyup event

if (keyCode != 27 && keyCode != 9) {
if (lastControlKeyPress == keyCode) {
lastControlKeyPressRepeat++;
}
if (typeof autoRepeatModel == 'undefined') {
if (lastKeyDownRepeat) {
autoRepeatModel = lastControlKeyPressRepeat ? 'both' : 'down';
} else if (lastControlKeyPressRepeat) {
autoRepeatModel = 'press';
}
}
}

Expand All @@ -143,7 +149,10 @@ if (API && API.attachListener && Function.prototype.apply) {
onkey.apply(context || this, [e, keyCode]);
}

lastControlKeyPress = keyCode;

if (keyCode != 27 && keyCode != 9) {
lastControlKeyPress = keyCode;
}

if (keyCode > 36 && keyCode < 41 || (keyCode == 33 || keyCode == 34) || keyCode == 27) {
if (onnavkeypress) {
Expand Down Expand Up @@ -191,10 +200,8 @@ if (API && API.attachListener && Function.prototype.apply) {
if (keyDownMap[key]) {
return;
}
} else {
if (lastKeyDown === key && (time - keyDownMap[key] < 500)) {
lastKeyDownRepeat++;
}
} else if (lastKeyDown === key && key != 27 && key != 9) {
lastKeyDownRepeat++;
}

if (autoRepeatModel == 'press') {
Expand Down Expand Up @@ -232,17 +239,21 @@ if (API && API.attachListener && Function.prototype.apply) {

if (E && E.prototype) {
E.prototype.onKeyboard = function(options) {
var el = this.element();

attachKeyboardListeners(el, options);
attachKeyboardListeners(this.element(), options);
};
}

if (D && D.prototype) {
D.prototype.onKeyboard = function(options) {
var doc = this.node();
attachKeyboardListeners(this.node(), options);
};
}

attachKeyboardListeners(doc, options);
if (Q && Q.prototype) {
Q.prototype.onKeyboard = function(options) {
this.forEach(function(el) {
attachKeyboardListeners(el, options);
});
};
}
})();
Expand Down

0 comments on commit f149efe

Please sign in to comment.