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