1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-util.KeyMap-method-constructor'><span id='Ext-util.KeyMap'>/**
2 </span></span> * @class Ext.util.KeyMap
3 * Handles mapping keys to actions for an element. One key map can be used for multiple actions.
4 * The constructor accepts the same config object as defined by {@link #addBinding}.
5 * If you bind a callback function to a KeyMap, anytime the KeyMap handles an expected key
6 * combination it will call the function with this signature (if the match is a multi-key
7 * combination the callback will still be called only once): (String key, Ext.EventObject e)
8 * A KeyMap can also handle a string representation of keys.<br />
10 <pre><code>
11 // map one key by key code
12 var map = new Ext.util.KeyMap("my-element", {
13 key: 13, // or Ext.EventObject.ENTER
18 // map multiple keys to one action by string
19 var map = new Ext.util.KeyMap("my-element", {
20 key: "a\r\n\t",
25 // map multiple keys to multiple actions by strings and array of codes
26 var map = new Ext.util.KeyMap("my-element", [
29 fn: function(){ alert("Return was pressed"); }
32 fn: function(){ alert('a, b or c was pressed'); }
37 fn: function(){ alert('Control + shift + tab was pressed.'); }
40 </code></pre>
41 * <b>Note: A KeyMap starts enabled</b>
43 * @param {Mixed} el The element to bind to
44 * @param {Object} binding The binding (see {@link #addBinding})
45 * @param {String} eventName (optional) The event to bind to (defaults to "keydown")
47 Ext.define('Ext.util.KeyMap', {
48 alternateClassName: 'Ext.KeyMap',
50 constructor: function(el, binding, eventName){
55 eventName: eventName || me.eventName,
59 me.addBinding(binding);
66 <span id='Ext-util.KeyMap-method-addBinding'> /**
67 </span> * Add a new binding to this KeyMap. The following config object properties are supported:
69 Property Type Description
70 ---------- --------------- ----------------------------------------------------------------------
71 key String/Array A single keycode or an array of keycodes to handle
72 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)
73 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)
74 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)
75 handler Function The function to call when KeyMap finds the expected key combination
76 fn Function Alias of handler (for backwards-compatibility)
77 scope Object The scope of the callback function
78 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.
82 * <pre><code>
84 var map = new Ext.util.KeyMap(document, {
85 key: Ext.EventObject.ENTER,
90 //Add a new binding to the existing KeyMap later
97 </code></pre>
98 * @param {Object/Array} binding A single KeyMap config or an array of configs
100 addBinding : function(binding){
101 if (Ext.isArray(binding)) {
102 Ext.each(binding, this.addBinding, this);
106 var keyCode = binding.key,
114 if (Ext.isString(keyCode)) {
116 keyString = keyCode.toLowerCase();
118 for (i = 0, len = keyString.length; i < len; ++i){
119 keys.push(keyString.charCodeAt(i));
125 if (!Ext.isArray(keyCode)) {
130 for (i = 0, len = keyCode.length; i < len; ++i) {
132 if (Ext.isString(key)) {
133 keyCode[i] = key.toLowerCase().charCodeAt(0);
138 this.bindings.push(Ext.apply({
143 <span id='Ext-util.KeyMap-method-handleKeyDown'> /**
144 </span> * Process any keydown events on the element
146 * @param {Ext.EventObject} event
148 handleKeyDown: function(event) {
149 if (this.enabled) { //just in case
150 var bindings = this.bindings,
152 len = bindings.length;
154 event = this.processEvent(event);
155 for(; i < len; ++i){
156 this.processBinding(bindings[i], event);
161 <span id='Ext-util.KeyMap-method-processEvent'> /**
162 </span> * Ugly hack to allow this class to be tested. Currently WebKit gives
163 * no way to raise a key event properly with both
165 * b) The alt/ctrl/shift modifiers
166 * So we have to simulate them here. Yuk!
167 * This is a stub method intended to be overridden by tests.
168 * More info: https://bugs.webkit.org/show_bug.cgi?id=16735
171 processEvent: function(event){
175 <span id='Ext-util.KeyMap-method-processBinding'> /**
176 </span> * Process a particular binding and fire the handler if necessary.
178 * @param {Object} binding The binding information
179 * @param {Ext.EventObject} event
181 processBinding: function(binding, event){
182 if (this.checkModifiers(binding, event)) {
183 var key = event.getKey(),
184 handler = binding.fn || binding.handler,
185 scope = binding.scope || this,
186 keyCode = binding.keyCode,
187 defaultEventAction = binding.defaultEventAction,
190 keydownEvent = new Ext.EventObjectImpl(event);
193 for (i = 0, len = keyCode.length; i < len; ++i) {
194 if (key === keyCode[i]) {
195 if (handler.call(scope, key, event) !== true && defaultEventAction) {
196 keydownEvent[defaultEventAction]();
204 <span id='Ext-util.KeyMap-method-checkModifiers'> /**
205 </span> * Check if the modifiers on the event match those on the binding
207 * @param {Object} binding
208 * @param {Ext.EventObject} event
209 * @return {Boolean} True if the event matches the binding
211 checkModifiers: function(binding, e){
212 var keys = ['shift', 'ctrl', 'alt'],
217 for (; i < len; ++i){
220 if (!(val === undefined || (val === e[key + 'Key']))) {
227 <span id='Ext-util.KeyMap-method-on'> /**
228 </span> * Shorthand for adding a single key listener
229 * @param {Number/Array/Object} key Either the numeric key code, array of key codes or an object with the
231 * {key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}
232 * @param {Function} fn The function to call
233 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.
235 on: function(key, fn, scope) {
236 var keyCode, shift, ctrl, alt;
237 if (Ext.isObject(key) && !Ext.isArray(key)) {
255 <span id='Ext-util.KeyMap-method-isEnabled'> /**
256 </span> * Returns true if this KeyMap is enabled
259 isEnabled : function(){
263 <span id='Ext-util.KeyMap-method-enable'> /**
264 </span> * Enables this KeyMap
268 this.el.on(this.eventName, this.handleKeyDown, this);
273 <span id='Ext-util.KeyMap-method-disable'> /**
274 </span> * Disable this KeyMap
278 this.el.removeListener(this.eventName, this.handleKeyDown, this);
279 this.enabled = false;
283 <span id='Ext-util.KeyMap-method-setDisabled'> /**
284 </span> * Convenience function for setting disabled/enabled by boolean.
285 * @param {Boolean} disabled
287 setDisabled : function(disabled){
295 <span id='Ext-util.KeyMap-method-destroy'> /**
296 </span> * Destroys the KeyMap instance and removes all handlers.
297 * @param {Boolean} removeEl True to also remove the attached element
299 destroy: function(removeEl){
304 if (removeEl === true) {
309 });</pre></pre></body></html>