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.
63 prepareEvent : function(e){
65 var h = this.keyToHandler[k];
66 if(Ext.isSafari2 && h && k >= 37 && k <= 40){
74 var h = this.keyToHandler[k];
76 if(this.doRelay(e, this[h], h) !== true){
77 e[this.defaultEventAction]();
83 doRelay : function(e, h, hname){
84 return h.call(this.scope || this, e);
122 // ie won't do special keys on keypress, no one else will repeat keys with keydown
123 // the EventObject will normalize Safari automatically
124 if(this.isKeydown()){
125 this.el.on("keydown", this.relay, this);
127 this.el.on("keydown", this.prepareEvent, this);
128 this.el.on("keypress", this.relay, this);
130 this.disabled = false;
135 * Disable this KeyNav
139 if(this.isKeydown()){
140 this.el.un("keydown", this.relay, this);
142 this.el.un("keydown", this.prepareEvent, this);
143 this.el.un("keypress", this.relay, this);
145 this.disabled = true;
150 * Convenience function for setting disabled/enabled by boolean.
151 * @param {Boolean} disabled
153 setDisabled : function(disabled){
154 this[disabled ? "disable" : "enable"]();
158 isKeydown: function(){
159 return this.forceKeyDown || Ext.EventManager.useKeydown;