add bindKeyboard and unbindKeyboard methods to Ox.Element, allowing an element to receive all keyboard events, independently of focus

This commit is contained in:
rlx 2011-11-01 11:49:46 +00:00
parent b6a8ed28b4
commit 3aa10edec8
2 changed files with 30 additions and 38 deletions

View file

@ -301,6 +301,11 @@ Ox.Element = function(options, self) {
return that;
};
that.bindKeyboard = function() {
Ox.Keyboard.bind(that.id);
return that;
};
/*@
defaults <function> Sets the default options for an element object
({key: value, ...}) -> <obj> This element object
@ -335,7 +340,6 @@ Ox.Element = function(options, self) {
loseFocus <function> Makes an element object lose focus
() -> <object> This element object
@*/
that.loseFocus = function() {
Ox.Focus.blur(that.id);
return that;
@ -428,6 +432,11 @@ Ox.Element = function(options, self) {
return that;
};
that.unbindKeyboard = function() {
Ox.Keyboard.unbind(that.id);
return that;
};
return that;
};

View file

@ -1,42 +1,11 @@
// vim: et:ts=4:sw=4:sts=4:ft=javascript
/***
Ox.Keyboard
***/
/*@
Ox.Keyboard <o> Basic keyboard handler
@*/
(function() {
Ox.Keyboard = (function() {
var buffer = '', resetTimeout, triggerTimeout;
/*
Ox.UI.ready(function() {
// fixme: how to do this better?
// in firefox on mac, keypress doesn't fire for up/down
// if the cursor is at the start/end of an input element
// on linux, it doesn't seem to fire if the input element has focus
if ($.browser.mozilla) {
Ox.UI.$document.keypress(keypress);
Ox.UI.$document.keydown(function(event) {
var $element = $('input:focus');
if ($element.length) {
if (
(
Ox.KEYS[event.keyCode] == 'up' &&
$element[0].selectionStart + $element[0].selectionEnd == 0
) || (
Ox.KEYS[event.keyCode] == 'down' &&
$element[0].selectionStart == $element.val().length &&
$element[0].selectionEnd == $element.val().length
)
) {
keypress(event);
}
}
});
} else {
Ox.UI.$document.keydown(keypress);
}
});
*/
var buffer = '', bound = [], resetTimeout, triggerTimeout;
Ox.UI.ready(function() {
Ox.UI.$document.keydown(keydown)
@ -59,7 +28,10 @@
}
});
key = keyNames.join('_');
if (focused !== null) {
bound.forEach(function(id) {
Ox.UI.elements[id].triggerEvent('key_' + key);
});
if (focused !== null && bound.indexOf(focused) == -1) {
Ox.UI.elements[focused].triggerEvent('key_' + key);
// prevent Chrome from scrolling, or going back in history
if (
@ -105,4 +77,15 @@
}
return {
bind: function(id) {
var index = bound.indexOf(id);
index == -1 && bound.push(id);
},
unbind: function(id) {
var index = bound.indexOf(id);
index > -1 && bound.splice(index, 1);
}
};
})();