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.KeyMap"></div>/**
\r
16 * Handles mapping keys to actions for an element. One key map can be used for multiple actions.
\r
17 * The constructor accepts the same config object as defined by {@link #addBinding}.
\r
18 * If you bind a callback function to a KeyMap, anytime the KeyMap handles an expected key
\r
19 * combination it will call the function with this signature (if the match is a multi-key
\r
20 * combination the callback will still be called only once): (String key, Ext.EventObject e)
\r
21 * A KeyMap can also handle a string representation of keys.<br />
\r
24 // map one key by key code
\r
25 var map = new Ext.KeyMap("my-element", {
\r
26 key: 13, // or Ext.EventObject.ENTER
\r
31 // map multiple keys to one action by string
\r
32 var map = new Ext.KeyMap("my-element", {
\r
38 // map multiple keys to multiple actions by strings and array of codes
\r
39 var map = new Ext.KeyMap("my-element", [
\r
42 fn: function(){ alert("Return was pressed"); }
\r
45 fn: function(){ alert('a, b or c was pressed'); }
\r
50 fn: function(){ alert('Control + shift + tab was pressed.'); }
\r
54 * <b>Note: A KeyMap starts enabled</b>
\r
56 * @param {Mixed} el The element to bind to
\r
57 * @param {Object} config The config (see {@link #addBinding})
\r
58 * @param {String} eventName (optional) The event to bind to (defaults to "keydown")
\r
60 Ext.KeyMap = function(el, config, eventName){
\r
61 this.el = Ext.get(el);
\r
62 this.eventName = eventName || "keydown";
\r
65 this.addBinding(config);
\r
70 Ext.KeyMap.prototype = {
\r
71 <div id="prop-Ext.KeyMap-stopEvent"></div>/**
\r
72 * True to stop the event from bubbling and prevent the default browser action if the
\r
73 * key was handled by the KeyMap (defaults to false)
\r
78 <div id="method-Ext.KeyMap-addBinding"></div>/**
\r
79 * Add a new binding to this KeyMap. The following config object properties are supported:
\r
81 Property Type Description
\r
82 ---------- --------------- ----------------------------------------------------------------------
\r
83 key String/Array A single keycode or an array of keycodes to handle
\r
84 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
85 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
86 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
87 handler Function The function to call when KeyMap finds the expected key combination
\r
88 fn Function Alias of handler (for backwards-compatibility)
\r
89 scope Object The scope of the callback function
\r
90 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
96 var map = new Ext.KeyMap(document, {
\r
97 key: Ext.EventObject.ENTER,
\r
102 //Add a new binding to the existing KeyMap later
\r
110 * @param {Object/Array} config A single KeyMap config or an array of configs
\r
112 addBinding : function(config){
\r
113 if(Ext.isArray(config)){
\r
114 Ext.each(config, function(c){
\r
115 this.addBinding(c);
\r
119 var keyCode = config.key,
\r
120 fn = config.fn || config.handler,
\r
121 scope = config.scope;
\r
123 if (config.stopEvent) {
\r
124 this.stopEvent = config.stopEvent;
\r
127 if(typeof keyCode == "string"){
\r
129 var keyString = keyCode.toUpperCase();
\r
130 for(var j = 0, len = keyString.length; j < len; j++){
\r
131 ks.push(keyString.charCodeAt(j));
\r
135 var keyArray = Ext.isArray(keyCode);
\r
137 var handler = function(e){
\r
138 if(this.checkModifiers(config, e)){
\r
139 var k = e.getKey();
\r
141 for(var i = 0, len = keyCode.length; i < len; i++){
\r
142 if(keyCode[i] == k){
\r
143 if(this.stopEvent){
\r
146 fn.call(scope || window, k, e);
\r
152 if(this.stopEvent){
\r
155 fn.call(scope || window, k, e);
\r
160 this.bindings.push(handler);
\r
164 checkModifiers: function(config, e){
\r
165 var val, key, keys = ['shift', 'ctrl', 'alt'];
\r
166 for (var i = 0, len = keys.length; i < len; ++i){
\r
169 if(!(val === undefined || (val === e[key + 'Key']))){
\r
176 <div id="method-Ext.KeyMap-on"></div>/**
\r
177 * Shorthand for adding a single key listener
\r
178 * @param {Number/Array/Object} key Either the numeric key code, array of key codes or an object with the
\r
179 * following options:
\r
180 * {key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}
\r
181 * @param {Function} fn The function to call
\r
182 * @param {Object} scope (optional) The scope of the function
\r
184 on : function(key, fn, scope){
\r
185 var keyCode, shift, ctrl, alt;
\r
186 if(typeof key == "object" && !Ext.isArray(key)){
\r
205 handleKeyDown : function(e){
\r
206 if(this.enabled){ //just in case
\r
207 var b = this.bindings;
\r
208 for(var i = 0, len = b.length; i < len; i++){
\r
209 b[i].call(this, e);
\r
214 <div id="method-Ext.KeyMap-isEnabled"></div>/**
\r
215 * Returns true if this KeyMap is enabled
\r
216 * @return {Boolean}
\r
218 isEnabled : function(){
\r
219 return this.enabled;
\r
222 <div id="method-Ext.KeyMap-enable"></div>/**
\r
223 * Enables this KeyMap
\r
225 enable: function(){
\r
227 this.el.on(this.eventName, this.handleKeyDown, this);
\r
228 this.enabled = true;
\r
232 <div id="method-Ext.KeyMap-disable"></div>/**
\r
233 * Disable this KeyMap
\r
235 disable: function(){
\r
237 this.el.removeListener(this.eventName, this.handleKeyDown, this);
\r
238 this.enabled = false;
\r
242 <div id="method-Ext.KeyMap-setDisabled"></div>/**
\r
243 * Convenience function for setting disabled/enabled by boolean.
\r
244 * @param {Boolean} disabled
\r
246 setDisabled : function(disabled){
\r
247 this[disabled ? "disable" : "enable"]();
\r