3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
9 * <p>Provides a convenient wrapper for normalized keyboard navigation. KeyNav allows you to bind
10 * navigation keys to function calls that will get called when the keys are pressed, providing an easy
11 * way to implement custom navigation schemes for any UI component.</p>
12 * <p>The following are all of the possible keys that can be implemented: enter, left, right, up, down, tab, esc,
13 * pageUp, pageDown, del, home, end. Usage:</p>
15 var nav = new Ext.KeyNav("my-element", {
17 this.moveLeft(e.ctrlKey);
19 "right" : function(e){
20 this.moveRight(e.ctrlKey);
22 "enter" : function(e){
29 * @param {Mixed} el The element to bind to
30 * @param {Object} config The config
32 Ext.KeyNav = function(el, config){
33 this.el = Ext.get(el);
34 Ext.apply(this, config);
41 Ext.KeyNav.prototype = {
43 * @cfg {Boolean} disabled
44 * True to disable this KeyNav instance (defaults to false)
48 * @cfg {String} defaultEventAction
49 * The method to call on the {@link Ext.EventObject} after this KeyNav intercepts a key. Valid values are
50 * {@link Ext.EventObject#stopEvent}, {@link Ext.EventObject#preventDefault} and
51 * {@link Ext.EventObject#stopPropagation} (defaults to 'stopEvent')
53 defaultEventAction: "stopEvent",
55 * @cfg {Boolean} forceKeyDown
56 * Handle the keydown event instead of keypress (defaults to false). KeyNav automatically does this for IE since
57 * IE does not propagate special keys on keypress, but setting this to true will force other browsers to also
58 * handle keydown instead of keypress.
65 var h = this.keyToHandler[k];
67 if(this.doRelay(e, this[h], h) !== true){
68 e[this.defaultEventAction]();
74 doRelay : function(e, h, hname){
75 return h.call(this.scope || this, e);
108 stopKeyUp: function(e) {
111 if (k >= 37 && k <= 40) {
112 // *** bugfix - safari 2.x fires 2 keyup events on cursor keys
113 // *** (note: this bugfix sacrifices the "keyup" event originating from keyNav elements in Safari 2)
124 // call stopKeyUp() on "keyup" event
125 this.el.on('keyup', this.stopKeyUp, this);
128 this.el.on(this.isKeydown()? 'keydown' : 'keypress', this.relay, this);
129 this.disabled = false;
134 * Disable this KeyNav
136 disable: function() {
137 if (!this.disabled) {
139 // remove "keyup" event handler
140 this.el.un('keyup', this.stopKeyUp, this);
143 this.el.un(this.isKeydown()? 'keydown' : 'keypress', this.relay, this);
144 this.disabled = true;
149 * Convenience function for setting disabled/enabled by boolean.
150 * @param {Boolean} disabled
152 setDisabled : function(disabled){
153 this[disabled ? "disable" : "enable"]();
157 isKeydown: function(){
158 return this.forceKeyDown || Ext.EventManager.useKeydown;