diff --git a/source/Ox.UI/js/Core/Ox.Element.js b/source/Ox.UI/js/Core/Ox.Element.js index 261e3c7e..9c7c32da 100644 --- a/source/Ox.UI/js/Core/Ox.Element.js +++ b/source/Ox.UI/js/Core/Ox.Element.js @@ -301,6 +301,11 @@ Ox.Element = function(options, self) { return that; }; + that.bindKeyboard = function() { + Ox.Keyboard.bind(that.id); + return that; + }; + /*@ defaults Sets the default options for an element object ({key: value, ...}) -> This element object @@ -335,7 +340,6 @@ Ox.Element = function(options, self) { loseFocus Makes an element object lose focus () -> 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; }; diff --git a/source/Ox.UI/js/Core/Ox.Keyboard.js b/source/Ox.UI/js/Core/Ox.Keyboard.js index c9346036..a3772582 100644 --- a/source/Ox.UI/js/Core/Ox.Keyboard.js +++ b/source/Ox.UI/js/Core/Ox.Keyboard.js @@ -1,42 +1,11 @@ // vim: et:ts=4:sw=4:sts=4:ft=javascript -/*** - Ox.Keyboard -***/ +/*@ +Ox.Keyboard 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); + } + }; + })();