2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
11 * Handles mapping keys to actions for an element. One key map can be used for multiple actions.
\r
12 * The constructor accepts the same config object as defined by {@link #addBinding}.
\r
13 * If you bind a callback function to a KeyMap, anytime the KeyMap handles an expected key
\r
14 * combination it will call the function with this signature (if the match is a multi-key
\r
15 * combination the callback will still be called only once): (String key, Ext.EventObject e)
\r
16 * A KeyMap can also handle a string representation of keys.<br />
\r
19 // map one key by key code
\r
20 var map = new Ext.KeyMap("my-element", {
\r
21 key: 13, // or Ext.EventObject.ENTER
\r
26 // map multiple keys to one action by string
\r
27 var map = new Ext.KeyMap("my-element", {
\r
33 // map multiple keys to multiple actions by strings and array of codes
\r
34 var map = new Ext.KeyMap("my-element", [
\r
37 fn: function(){ alert("Return was pressed"); }
\r
40 fn: function(){ alert('a, b or c was pressed'); }
\r
45 fn: function(){ alert('Control + shift + tab was pressed.'); }
\r
49 * <b>Note: A KeyMap starts enabled</b>
\r
51 * @param {Mixed} el The element to bind to
\r
52 * @param {Object} config The config (see {@link #addBinding})
\r
53 * @param {String} eventName (optional) The event to bind to (defaults to "keydown")
\r
55 Ext.KeyMap = function(el, config, eventName){
\r
56 this.el = Ext.get(el);
\r
57 this.eventName = eventName || "keydown";
\r
60 this.addBinding(config);
\r
65 Ext.KeyMap.prototype = {
\r
67 * True to stop the event from bubbling and prevent the default browser action if the
\r
68 * key was handled by the KeyMap (defaults to false)
\r
74 * Add a new binding to this KeyMap. The following config object properties are supported:
\r
76 Property Type Description
\r
77 ---------- --------------- ----------------------------------------------------------------------
\r
78 key String/Array A single keycode or an array of keycodes to handle
\r
79 shift Boolean True to handle key only when shift is pressed (defaults to false)
\r
80 ctrl Boolean True to handle key only when ctrl is pressed (defaults to false)
\r
81 alt Boolean True to handle key only when alt is pressed (defaults to false)
\r
82 handler Function The function to call when KeyMap finds the expected key combination
\r
83 fn Function Alias of handler (for backwards-compatibility)
\r
84 scope Object The scope of the callback function
\r
85 stopEvent Boolean True to stop the event
\r
91 var map = new Ext.KeyMap(document, {
\r
92 key: Ext.EventObject.ENTER,
\r
97 //Add a new binding to the existing KeyMap later
\r
105 * @param {Object/Array} config A single KeyMap config or an array of configs
\r
107 addBinding : function(config){
\r
108 if(Ext.isArray(config)){
\r
109 for(var i = 0, len = config.length; i < len; i++){
\r
110 this.addBinding(config[i]);
\r
114 var keyCode = config.key,
\r
115 shift = config.shift,
\r
116 ctrl = config.ctrl,
\r
118 fn = config.fn || config.handler,
\r
119 scope = config.scope;
\r
121 if (config.stopEvent) {
\r
122 this.stopEvent = config.stopEvent;
\r
125 if(typeof keyCode == "string"){
\r
127 var keyString = keyCode.toUpperCase();
\r
128 for(var j = 0, len = keyString.length; j < len; j++){
\r
129 ks.push(keyString.charCodeAt(j));
\r
133 var keyArray = Ext.isArray(keyCode);
\r
135 var handler = function(e){
\r
136 if((!shift || e.shiftKey) && (!ctrl || e.ctrlKey) && (!alt || e.altKey)){
\r
137 var k = e.getKey();
\r
139 for(var i = 0, len = keyCode.length; i < len; i++){
\r
140 if(keyCode[i] == k){
\r
141 if(this.stopEvent){
\r
144 fn.call(scope || window, k, e);
\r
150 if(this.stopEvent){
\r
153 fn.call(scope || window, k, e);
\r
158 this.bindings.push(handler);
\r
162 * Shorthand for adding a single key listener
\r
163 * @param {Number/Array/Object} key Either the numeric key code, array of key codes or an object with the
\r
164 * following options:
\r
165 * {key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}
\r
166 * @param {Function} fn The function to call
\r
167 * @param {Object} scope (optional) The scope of the function
\r
169 on : function(key, fn, scope){
\r
170 var keyCode, shift, ctrl, alt;
\r
171 if(typeof key == "object" && !Ext.isArray(key)){
\r
190 handleKeyDown : function(e){
\r
191 if(this.enabled){ //just in case
\r
192 var b = this.bindings;
\r
193 for(var i = 0, len = b.length; i < len; i++){
\r
194 b[i].call(this, e);
\r
200 * Returns true if this KeyMap is enabled
\r
201 * @return {Boolean}
\r
203 isEnabled : function(){
\r
204 return this.enabled;
\r
208 * Enables this KeyMap
\r
210 enable: function(){
\r
212 this.el.on(this.eventName, this.handleKeyDown, this);
\r
213 this.enabled = true;
\r
218 * Disable this KeyMap
\r
220 disable: function(){
\r
222 this.el.removeListener(this.eventName, this.handleKeyDown, this);
\r
223 this.enabled = false;
\r