4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-util-KeyMap-method-constructor'><span id='Ext-util-KeyMap'>/**
19 </span></span> * @class Ext.util.KeyMap
20 * Handles mapping keys to actions for an element. One key map can be used for multiple actions.
21 * The constructor accepts the same config object as defined by {@link #addBinding}.
22 * If you bind a callback function to a KeyMap, anytime the KeyMap handles an expected key
23 * combination it will call the function with this signature (if the match is a multi-key
24 * combination the callback will still be called only once): (String key, Ext.EventObject e)
25 * A KeyMap can also handle a string representation of keys.<br />
27 <pre><code>
28 // map one key by key code
29 var map = new Ext.util.KeyMap("my-element", {
30 key: 13, // or Ext.EventObject.ENTER
35 // map multiple keys to one action by string
36 var map = new Ext.util.KeyMap("my-element", {
37 key: "a\r\n\t",
42 // map multiple keys to multiple actions by strings and array of codes
43 var map = new Ext.util.KeyMap("my-element", [
46 fn: function(){ alert("Return was pressed"); }
49 fn: function(){ alert('a, b or c was pressed'); }
54 fn: function(){ alert('Control + shift + tab was pressed.'); }
57 </code></pre>
58 * <b>Note: A KeyMap starts enabled</b>
60 * @param {Mixed} el The element to bind to
61 * @param {Object} binding The binding (see {@link #addBinding})
62 * @param {String} eventName (optional) The event to bind to (defaults to "keydown")
64 Ext.define('Ext.util.KeyMap', {
65 alternateClassName: 'Ext.KeyMap',
67 constructor: function(el, binding, eventName){
72 eventName: eventName || me.eventName,
76 me.addBinding(binding);
83 <span id='Ext-util-KeyMap-method-addBinding'> /**
84 </span> * Add a new binding to this KeyMap. The following config object properties are supported:
86 Property Type Description
87 ---------- --------------- ----------------------------------------------------------------------
88 key String/Array A single keycode or an array of keycodes to handle
89 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)
90 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)
91 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)
92 handler Function The function to call when KeyMap finds the expected key combination
93 fn Function Alias of handler (for backwards-compatibility)
94 scope Object The scope of the callback function
95 defaultEventAction String A default action to apply to the event. Possible values are: stopEvent, stopPropagation, preventDefault. If no value is set no action is performed.
99 * <pre><code>
101 var map = new Ext.util.KeyMap(document, {
102 key: Ext.EventObject.ENTER,
107 //Add a new binding to the existing KeyMap later
114 </code></pre>
115 * @param {Object/Array} binding A single KeyMap config or an array of configs
117 addBinding : function(binding){
118 if (Ext.isArray(binding)) {
119 Ext.each(binding, this.addBinding, this);
123 var keyCode = binding.key,
131 if (Ext.isString(keyCode)) {
133 keyString = keyCode.toLowerCase();
135 for (i = 0, len = keyString.length; i < len; ++i){
136 keys.push(keyString.charCodeAt(i));
142 if (!Ext.isArray(keyCode)) {
147 for (i = 0, len = keyCode.length; i < len; ++i) {
149 if (Ext.isString(key)) {
150 keyCode[i] = key.toLowerCase().charCodeAt(0);
155 this.bindings.push(Ext.apply({
160 <span id='Ext-util-KeyMap-method-handleKeyDown'> /**
161 </span> * Process any keydown events on the element
163 * @param {Ext.EventObject} event
165 handleKeyDown: function(event) {
166 if (this.enabled) { //just in case
167 var bindings = this.bindings,
169 len = bindings.length;
171 event = this.processEvent(event);
172 for(; i < len; ++i){
173 this.processBinding(bindings[i], event);
178 <span id='Ext-util-KeyMap-method-processEvent'> /**
179 </span> * Ugly hack to allow this class to be tested. Currently WebKit gives
180 * no way to raise a key event properly with both
182 * b) The alt/ctrl/shift modifiers
183 * So we have to simulate them here. Yuk!
184 * This is a stub method intended to be overridden by tests.
185 * More info: https://bugs.webkit.org/show_bug.cgi?id=16735
188 processEvent: function(event){
192 <span id='Ext-util-KeyMap-method-processBinding'> /**
193 </span> * Process a particular binding and fire the handler if necessary.
195 * @param {Object} binding The binding information
196 * @param {Ext.EventObject} event
198 processBinding: function(binding, event){
199 if (this.checkModifiers(binding, event)) {
200 var key = event.getKey(),
201 handler = binding.fn || binding.handler,
202 scope = binding.scope || this,
203 keyCode = binding.keyCode,
204 defaultEventAction = binding.defaultEventAction,
207 keydownEvent = new Ext.EventObjectImpl(event);
210 for (i = 0, len = keyCode.length; i < len; ++i) {
211 if (key === keyCode[i]) {
212 if (handler.call(scope, key, event) !== true && defaultEventAction) {
213 keydownEvent[defaultEventAction]();
221 <span id='Ext-util-KeyMap-method-checkModifiers'> /**
222 </span> * Check if the modifiers on the event match those on the binding
224 * @param {Object} binding
225 * @param {Ext.EventObject} event
226 * @return {Boolean} True if the event matches the binding
228 checkModifiers: function(binding, e){
229 var keys = ['shift', 'ctrl', 'alt'],
234 for (; i < len; ++i){
237 if (!(val === undefined || (val === e[key + 'Key']))) {
244 <span id='Ext-util-KeyMap-method-on'> /**
245 </span> * Shorthand for adding a single key listener
246 * @param {Number/Array/Object} key Either the numeric key code, array of key codes or an object with the
248 * {key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}
249 * @param {Function} fn The function to call
250 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.
252 on: function(key, fn, scope) {
253 var keyCode, shift, ctrl, alt;
254 if (Ext.isObject(key) && !Ext.isArray(key)) {
272 <span id='Ext-util-KeyMap-method-isEnabled'> /**
273 </span> * Returns true if this KeyMap is enabled
276 isEnabled : function(){
280 <span id='Ext-util-KeyMap-method-enable'> /**
281 </span> * Enables this KeyMap
285 this.el.on(this.eventName, this.handleKeyDown, this);
290 <span id='Ext-util-KeyMap-method-disable'> /**
291 </span> * Disable this KeyMap
295 this.el.removeListener(this.eventName, this.handleKeyDown, this);
296 this.enabled = false;
300 <span id='Ext-util-KeyMap-method-setDisabled'> /**
301 </span> * Convenience function for setting disabled/enabled by boolean.
302 * @param {Boolean} disabled
304 setDisabled : function(disabled){
312 <span id='Ext-util-KeyMap-method-destroy'> /**
313 </span> * Destroys the KeyMap instance and removes all handlers.
314 * @param {Boolean} removeEl True to also remove the attached element
316 destroy: function(removeEl){
321 if (removeEl === true) {