3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.KeyNav"></div>/**
16 * <p>Provides a convenient wrapper for normalized keyboard navigation. KeyNav allows you to bind
17 * navigation keys to function calls that will get called when the keys are pressed, providing an easy
18 * way to implement custom navigation schemes for any UI component.</p>
19 * <p>The following are all of the possible keys that can be implemented: enter, left, right, up, down, tab, esc,
20 * pageUp, pageDown, del, home, end. Usage:</p>
22 var nav = new Ext.KeyNav("my-element", {
24 this.moveLeft(e.ctrlKey);
26 "right" : function(e){
27 this.moveRight(e.ctrlKey);
29 "enter" : function(e){
36 * @param {Mixed} el The element to bind to
37 * @param {Object} config The config
39 Ext.KeyNav = function(el, config){
40 this.el = Ext.get(el);
41 Ext.apply(this, config);
48 Ext.KeyNav.prototype = {
49 <div id="cfg-Ext.KeyNav-disabled"></div>/**
50 * @cfg {Boolean} disabled
51 * True to disable this KeyNav instance (defaults to false)
54 <div id="cfg-Ext.KeyNav-defaultEventAction"></div>/**
55 * @cfg {String} defaultEventAction
56 * The method to call on the {@link Ext.EventObject} after this KeyNav intercepts a key. Valid values are
57 * {@link Ext.EventObject#stopEvent}, {@link Ext.EventObject#preventDefault} and
58 * {@link Ext.EventObject#stopPropagation} (defaults to 'stopEvent')
60 defaultEventAction: "stopEvent",
61 <div id="cfg-Ext.KeyNav-forceKeyDown"></div>/**
62 * @cfg {Boolean} forceKeyDown
63 * Handle the keydown event instead of keypress (defaults to false). KeyNav automatically does this for IE since
64 * IE does not propagate special keys on keypress, but setting this to true will force other browsers to also
65 * handle keydown instead of keypress.
72 var h = this.keyToHandler[k];
74 if(this.doRelay(e, this[h], h) !== true){
75 e[this.defaultEventAction]();
81 doRelay : function(e, h, hname){
82 return h.call(this.scope || this, e);
115 stopKeyUp: function(e) {
118 if (k >= 37 && k <= 40) {
119 // *** bugfix - safari 2.x fires 2 keyup events on cursor keys
120 // *** (note: this bugfix sacrifices the "keyup" event originating from keyNav elements in Safari 2)
125 <div id="method-Ext.KeyNav-enable"></div>/**
131 // call stopKeyUp() on "keyup" event
132 this.el.on('keyup', this.stopKeyUp, this);
135 this.el.on(this.isKeydown()? 'keydown' : 'keypress', this.relay, this);
136 this.disabled = false;
140 <div id="method-Ext.KeyNav-disable"></div>/**
141 * Disable this KeyNav
143 disable: function() {
144 if (!this.disabled) {
146 // remove "keyup" event handler
147 this.el.un('keyup', this.stopKeyUp, this);
150 this.el.un(this.isKeydown()? 'keydown' : 'keypress', this.relay, this);
151 this.disabled = true;
155 <div id="method-Ext.KeyNav-setDisabled"></div>/**
156 * Convenience function for setting disabled/enabled by boolean.
157 * @param {Boolean} disabled
159 setDisabled : function(disabled){
160 this[disabled ? "disable" : "enable"]();
164 isKeydown: function(){
165 return this.forceKeyDown || Ext.EventManager.useKeydown;