3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
9 * Handles mapping keys to actions for an element. One key map can be used for multiple actions.
10 * The constructor accepts the same config object as defined by {@link #addBinding}.
11 * If you bind a callback function to a KeyMap, anytime the KeyMap handles an expected key
12 * combination it will call the function with this signature (if the match is a multi-key
13 * combination the callback will still be called only once): (String key, Ext.EventObject e)
14 * A KeyMap can also handle a string representation of keys.<br />
17 // map one key by key code
18 var map = new Ext.KeyMap("my-element", {
19 key: 13, // or Ext.EventObject.ENTER
24 // map multiple keys to one action by string
25 var map = new Ext.KeyMap("my-element", {
31 // map multiple keys to multiple actions by strings and array of codes
32 var map = new Ext.KeyMap("my-element", [
35 fn: function(){ alert("Return was pressed"); }
38 fn: function(){ alert('a, b or c was pressed'); }
43 fn: function(){ alert('Control + shift + tab was pressed.'); }
47 * <b>Note: A KeyMap starts enabled</b>
49 * @param {Mixed} el The element to bind to
50 * @param {Object} config The config (see {@link #addBinding})
51 * @param {String} eventName (optional) The event to bind to (defaults to "keydown")
53 Ext.KeyMap = function(el, config, eventName){
54 this.el = Ext.get(el);
55 this.eventName = eventName || "keydown";
58 this.addBinding(config);
63 Ext.KeyMap.prototype = {
65 * True to stop the event from bubbling and prevent the default browser action if the
66 * key was handled by the KeyMap (defaults to false)
72 * Add a new binding to this KeyMap. The following config object properties are supported:
74 Property Type Description
75 ---------- --------------- ----------------------------------------------------------------------
76 key String/Array A single keycode or an array of keycodes to handle
77 shift Boolean True to handle key only when shift is pressed, False to handle the key only when shift is not pressed (defaults to undefined)
78 ctrl Boolean True to handle key only when ctrl is pressed, False to handle the key only when ctrl is not pressed (defaults to undefined)
79 alt Boolean True to handle key only when alt is pressed, False to handle the key only when alt is not pressed (defaults to undefined)
80 handler Function The function to call when KeyMap finds the expected key combination
81 fn Function Alias of handler (for backwards-compatibility)
82 scope Object The scope of the callback function
83 stopEvent Boolean True to stop the event from bubbling and prevent the default browser action if the key was handled by the KeyMap (defaults to false)
89 var map = new Ext.KeyMap(document, {
90 key: Ext.EventObject.ENTER,
95 //Add a new binding to the existing KeyMap later
103 * @param {Object/Array} config A single KeyMap config or an array of configs
105 addBinding : function(config){
106 if(Ext.isArray(config)){
107 Ext.each(config, function(c){
112 var keyCode = config.key,
113 fn = config.fn || config.handler,
114 scope = config.scope;
116 if (config.stopEvent) {
117 this.stopEvent = config.stopEvent;
120 if(typeof keyCode == "string"){
122 var keyString = keyCode.toUpperCase();
123 for(var j = 0, len = keyString.length; j < len; j++){
124 ks.push(keyString.charCodeAt(j));
128 var keyArray = Ext.isArray(keyCode);
130 var handler = function(e){
131 if(this.checkModifiers(config, e)){
134 for(var i = 0, len = keyCode.length; i < len; i++){
139 fn.call(scope || window, k, e);
148 fn.call(scope || window, k, e);
153 this.bindings.push(handler);
157 checkModifiers: function(config, e){
158 var val, key, keys = ['shift', 'ctrl', 'alt'];
159 for (var i = 0, len = keys.length; i < len; ++i){
162 if(!(val === undefined || (val === e[key + 'Key']))){
170 * Shorthand for adding a single key listener
171 * @param {Number/Array/Object} key Either the numeric key code, array of key codes or an object with the
173 * {key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}
174 * @param {Function} fn The function to call
175 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.
177 on : function(key, fn, scope){
178 var keyCode, shift, ctrl, alt;
179 if(typeof key == "object" && !Ext.isArray(key)){
198 handleKeyDown : function(e){
199 if(this.enabled){ //just in case
200 var b = this.bindings;
201 for(var i = 0, len = b.length; i < len; i++){
208 * Returns true if this KeyMap is enabled
211 isEnabled : function(){
216 * Enables this KeyMap
220 this.el.on(this.eventName, this.handleKeyDown, this);
226 * Disable this KeyMap
230 this.el.removeListener(this.eventName, this.handleKeyDown, this);
231 this.enabled = false;
236 * Convenience function for setting disabled/enabled by boolean.
237 * @param {Boolean} disabled
239 setDisabled : function(disabled){
240 this[disabled ? "disable" : "enable"]();