3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.KeyMap"></div>/**
\r
10 * Handles mapping keys to actions for an element. One key map can be used for multiple actions.
\r
11 * The constructor accepts the same config object as defined by {@link #addBinding}.
\r
12 * If you bind a callback function to a KeyMap, anytime the KeyMap handles an expected key
\r
13 * combination it will call the function with this signature (if the match is a multi-key
\r
14 * combination the callback will still be called only once): (String key, Ext.EventObject e)
\r
15 * A KeyMap can also handle a string representation of keys.<br />
\r
18 // map one key by key code
\r
19 var map = new Ext.KeyMap("my-element", {
\r
20 key: 13, // or Ext.EventObject.ENTER
\r
25 // map multiple keys to one action by string
\r
26 var map = new Ext.KeyMap("my-element", {
\r
32 // map multiple keys to multiple actions by strings and array of codes
\r
33 var map = new Ext.KeyMap("my-element", [
\r
36 fn: function(){ alert("Return was pressed"); }
\r
39 fn: function(){ alert('a, b or c was pressed'); }
\r
44 fn: function(){ alert('Control + shift + tab was pressed.'); }
\r
48 * <b>Note: A KeyMap starts enabled</b>
\r
50 * @param {Mixed} el The element to bind to
\r
51 * @param {Object} config The config (see {@link #addBinding})
\r
52 * @param {String} eventName (optional) The event to bind to (defaults to "keydown")
\r
54 Ext.KeyMap = function(el, config, eventName){
\r
55 this.el = Ext.get(el);
\r
56 this.eventName = eventName || "keydown";
\r
59 this.addBinding(config);
\r
64 Ext.KeyMap.prototype = {
\r
65 <div id="prop-Ext.KeyMap-stopEvent"></div>/**
\r
66 * True to stop the event from bubbling and prevent the default browser action if the
\r
67 * key was handled by the KeyMap (defaults to false)
\r
72 <div id="method-Ext.KeyMap-addBinding"></div>/**
\r
73 * Add a new binding to this KeyMap. The following config object properties are supported:
\r
75 Property Type Description
\r
76 ---------- --------------- ----------------------------------------------------------------------
\r
77 key String/Array A single keycode or an array of keycodes to handle
\r
78 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
79 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
80 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
81 handler Function The function to call when KeyMap finds the expected key combination
\r
82 fn Function Alias of handler (for backwards-compatibility)
\r
83 scope Object The scope of the callback function
\r
84 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
90 var map = new Ext.KeyMap(document, {
\r
91 key: Ext.EventObject.ENTER,
\r
96 //Add a new binding to the existing KeyMap later
\r
104 * @param {Object/Array} config A single KeyMap config or an array of configs
\r
106 addBinding : function(config){
\r
107 if(Ext.isArray(config)){
\r
108 Ext.each(config, function(c){
\r
109 this.addBinding(c);
\r
113 var keyCode = config.key,
\r
114 fn = config.fn || config.handler,
\r
115 scope = config.scope;
\r
117 if (config.stopEvent) {
\r
118 this.stopEvent = config.stopEvent;
\r
121 if(typeof keyCode == "string"){
\r
123 var keyString = keyCode.toUpperCase();
\r
124 for(var j = 0, len = keyString.length; j < len; j++){
\r
125 ks.push(keyString.charCodeAt(j));
\r
129 var keyArray = Ext.isArray(keyCode);
\r
131 var handler = function(e){
\r
132 if(this.checkModifiers(config, e)){
\r
133 var k = e.getKey();
\r
135 for(var i = 0, len = keyCode.length; i < len; i++){
\r
136 if(keyCode[i] == k){
\r
137 if(this.stopEvent){
\r
140 fn.call(scope || window, k, e);
\r
146 if(this.stopEvent){
\r
149 fn.call(scope || window, k, e);
\r
154 this.bindings.push(handler);
\r
158 checkModifiers: function(config, e){
\r
159 var val, key, keys = ['shift', 'ctrl', 'alt'];
\r
160 for (var i = 0, len = keys.length; i < len; ++i){
\r
163 if(!(val === undefined || (val === e[key + 'Key']))){
\r
170 <div id="method-Ext.KeyMap-on"></div>/**
\r
171 * Shorthand for adding a single key listener
\r
172 * @param {Number/Array/Object} key Either the numeric key code, array of key codes or an object with the
\r
173 * following options:
\r
174 * {key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}
\r
175 * @param {Function} fn The function to call
\r
176 * @param {Object} scope (optional) The scope of the function
\r
178 on : function(key, fn, scope){
\r
179 var keyCode, shift, ctrl, alt;
\r
180 if(typeof key == "object" && !Ext.isArray(key)){
\r
199 handleKeyDown : function(e){
\r
200 if(this.enabled){ //just in case
\r
201 var b = this.bindings;
\r
202 for(var i = 0, len = b.length; i < len; i++){
\r
203 b[i].call(this, e);
\r
208 <div id="method-Ext.KeyMap-isEnabled"></div>/**
\r
209 * Returns true if this KeyMap is enabled
\r
210 * @return {Boolean}
\r
212 isEnabled : function(){
\r
213 return this.enabled;
\r
216 <div id="method-Ext.KeyMap-enable"></div>/**
\r
217 * Enables this KeyMap
\r
219 enable: function(){
\r
221 this.el.on(this.eventName, this.handleKeyDown, this);
\r
222 this.enabled = true;
\r
226 <div id="method-Ext.KeyMap-disable"></div>/**
\r
227 * Disable this KeyMap
\r
229 disable: function(){
\r
231 this.el.removeListener(this.eventName, this.handleKeyDown, this);
\r
232 this.enabled = false;
\r
236 <div id="method-Ext.KeyMap-setDisabled"></div>/**
\r
237 * Convenience function for setting disabled/enabled by boolean.
\r
238 * @param {Boolean} disabled
\r
240 setDisabled : function(disabled){
\r
241 this[disabled ? "disable" : "enable"]();
\r