4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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'>/**
19 </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. By default KeyMap starts enabled.<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>
59 Ext.define('Ext.util.KeyMap', {
60 alternateClassName: 'Ext.KeyMap',
62 <span id='Ext-util-KeyMap-method-constructor'> /**
63 </span> * Creates new KeyMap.
64 * @param {String/HTMLElement/Ext.Element} el The element or its ID to bind to
65 * @param {Object} binding The binding (see {@link #addBinding})
66 * @param {String} [eventName="keydown"] The event to bind to
68 constructor: function(el, binding, eventName){
73 eventName: eventName || me.eventName,
77 me.addBinding(binding);
84 <span id='Ext-util-KeyMap-method-addBinding'> /**
85 </span> * Add a new binding to this KeyMap. The following config object properties are supported:
87 Property Type Description
88 ---------- --------------- ----------------------------------------------------------------------
89 key String/Array A single keycode or an array of keycodes to handle
90 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)
91 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)
92 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)
93 handler Function The function to call when KeyMap finds the expected key combination
94 fn Function Alias of handler (for backwards-compatibility)
95 scope Object The scope of the callback function
96 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.
100 * <pre><code>
102 var map = new Ext.util.KeyMap(document, {
103 key: Ext.EventObject.ENTER,
108 //Add a new binding to the existing KeyMap later
115 </code></pre>
116 * @param {Object/Object[]} binding A single KeyMap config or an array of configs
118 addBinding : function(binding){
119 if (Ext.isArray(binding)) {
120 Ext.each(binding, this.addBinding, this);
124 var keyCode = binding.key,
132 if (Ext.isString(keyCode)) {
134 keyString = keyCode.toUpperCase();
136 for (i = 0, len = keyString.length; i < len; ++i){
137 keys.push(keyString.charCodeAt(i));
143 if (!Ext.isArray(keyCode)) {
148 for (i = 0, len = keyCode.length; i < len; ++i) {
150 if (Ext.isString(key)) {
151 keyCode[i] = key.toUpperCase().charCodeAt(0);
156 this.bindings.push(Ext.apply({
161 <span id='Ext-util-KeyMap-method-handleKeyDown'> /**
162 </span> * Process any keydown events on the element
164 * @param {Ext.EventObject} event
166 handleKeyDown: function(event) {
167 if (this.enabled) { //just in case
168 var bindings = this.bindings,
170 len = bindings.length;
172 event = this.processEvent(event);
173 for(; i < len; ++i){
174 this.processBinding(bindings[i], event);
179 <span id='Ext-util-KeyMap-method-processEvent'> /**
180 </span> * Ugly hack to allow this class to be tested. Currently WebKit gives
181 * no way to raise a key event properly with both
183 * b) The alt/ctrl/shift modifiers
184 * So we have to simulate them here. Yuk!
185 * This is a stub method intended to be overridden by tests.
186 * More info: https://bugs.webkit.org/show_bug.cgi?id=16735
189 processEvent: function(event){
193 <span id='Ext-util-KeyMap-method-processBinding'> /**
194 </span> * Process a particular binding and fire the handler if necessary.
196 * @param {Object} binding The binding information
197 * @param {Ext.EventObject} event
199 processBinding: function(binding, event){
200 if (this.checkModifiers(binding, event)) {
201 var key = event.getKey(),
202 handler = binding.fn || binding.handler,
203 scope = binding.scope || this,
204 keyCode = binding.keyCode,
205 defaultEventAction = binding.defaultEventAction,
208 keydownEvent = new Ext.EventObjectImpl(event);
211 for (i = 0, len = keyCode.length; i < len; ++i) {
212 if (key === keyCode[i]) {
213 if (handler.call(scope, key, event) !== true && defaultEventAction) {
214 keydownEvent[defaultEventAction]();
222 <span id='Ext-util-KeyMap-method-checkModifiers'> /**
223 </span> * Check if the modifiers on the event match those on the binding
225 * @param {Object} binding
226 * @param {Ext.EventObject} event
227 * @return {Boolean} True if the event matches the binding
229 checkModifiers: function(binding, e){
230 var keys = ['shift', 'ctrl', 'alt'],
235 for (; i < len; ++i){
238 if (!(val === undefined || (val === e[key + 'Key']))) {
245 <span id='Ext-util-KeyMap-method-on'> /**
246 </span> * Shorthand for adding a single key listener
247 * @param {Number/Number[]/Object} key Either the numeric key code, array of key codes or an object with the
249 * {key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}
250 * @param {Function} fn The function to call
251 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.
253 on: function(key, fn, scope) {
254 var keyCode, shift, ctrl, alt;
255 if (Ext.isObject(key) && !Ext.isArray(key)) {
273 <span id='Ext-util-KeyMap-method-isEnabled'> /**
274 </span> * Returns true if this KeyMap is enabled
277 isEnabled : function(){
281 <span id='Ext-util-KeyMap-method-enable'> /**
282 </span> * Enables this KeyMap
288 me.el.on(me.eventName, me.handleKeyDown, me);
293 <span id='Ext-util-KeyMap-method-disable'> /**
294 </span> * Disable this KeyMap
300 me.el.removeListener(me.eventName, me.handleKeyDown, me);
305 <span id='Ext-util-KeyMap-method-setDisabled'> /**
306 </span> * Convenience function for setting disabled/enabled by boolean.
307 * @param {Boolean} disabled
309 setDisabled : function(disabled){
317 <span id='Ext-util-KeyMap-method-destroy'> /**
318 </span> * Destroys the KeyMap instance and removes all handlers.
319 * @param {Boolean} removeEl True to also remove the attached element
321 destroy: function(removeEl){
326 if (removeEl === true) {