3
* Copyright(c) 2006-2008, Ext JS, LLC.
6
* http://extjs.com/license
11
* Handles mapping keys to actions for an element. One key map can be used for multiple actions.
12
* The constructor accepts the same config object as defined by {@link #addBinding}.
13
* If you bind a callback function to a KeyMap, anytime the KeyMap handles an expected key
14
* combination it will call the function with this signature (if the match is a multi-key
15
* combination the callback will still be called only once): (String key, Ext.EventObject e)
16
* A KeyMap can also handle a string representation of keys.<br />
19
// map one key by key code
20
var map = new Ext.KeyMap("my-element", {
21
key: 13, // or Ext.EventObject.ENTER
26
// map multiple keys to one action by string
27
var map = new Ext.KeyMap("my-element", {
33
// map multiple keys to multiple actions by strings and array of codes
34
var map = new Ext.KeyMap("my-element", [
37
fn: function(){ alert("Return was pressed"); }
40
fn: function(){ alert('a, b or c was pressed'); }
45
fn: function(){ alert('Control + shift + tab was pressed.'); }
49
* <b>Note: A KeyMap starts enabled</b>
51
* @param {Mixed} el The element to bind to
52
* @param {Object} config The config (see {@link #addBinding})
53
* @param {String} eventName (optional) The event to bind to (defaults to "keydown")
55
Ext.KeyMap = function(el, config, eventName){
56
this.el = Ext.get(el);
57
this.eventName = eventName || "keydown";
60
this.addBinding(config);
65
Ext.KeyMap.prototype = {
67
* True to stop the event from bubbling and prevent the default browser action if the
68
* key was handled by the KeyMap (defaults to false)
74
* Add a new binding to this KeyMap. The following config object properties are supported:
76
Property Type Description
77
---------- --------------- ----------------------------------------------------------------------
78
key String/Array A single keycode or an array of keycodes to handle
79
shift Boolean True to handle key only when shift is pressed (defaults to false)
80
ctrl Boolean True to handle key only when ctrl is pressed (defaults to false)
81
alt Boolean True to handle key only when alt is pressed (defaults to false)
82
handler Function The function to call when KeyMap finds the expected key combination
83
fn Function Alias of handler (for backwards-compatibility)
84
scope Object The scope of the callback function
85
stopEvent Boolean True to stop the event
91
var map = new Ext.KeyMap(document, {
92
key: Ext.EventObject.ENTER,
97
//Add a new binding to the existing KeyMap later
105
* @param {Object/Array} config A single KeyMap config or an array of configs
107
addBinding : function(config){
108
if(Ext.isArray(config)){
109
for(var i = 0, len = config.length; i < len; i++){
110
this.addBinding(config[i]);
114
var keyCode = config.key,
115
shift = config.shift,
118
fn = config.fn || config.handler,
119
scope = config.scope;
121
if (config.stopEvent) {
122
this.stopEvent = config.stopEvent;
125
if(typeof keyCode == "string"){
127
var keyString = keyCode.toUpperCase();
128
for(var j = 0, len = keyString.length; j < len; j++){
129
ks.push(keyString.charCodeAt(j));
133
var keyArray = Ext.isArray(keyCode);
135
var handler = function(e){
136
if((!shift || e.shiftKey) && (!ctrl || e.ctrlKey) && (!alt || e.altKey)){
139
for(var i = 0, len = keyCode.length; i < len; i++){
144
fn.call(scope || window, k, e);
153
fn.call(scope || window, k, e);
158
this.bindings.push(handler);
162
* Shorthand for adding a single key listener
163
* @param {Number/Array/Object} key Either the numeric key code, array of key codes or an object with the
165
* {key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}
166
* @param {Function} fn The function to call
167
* @param {Object} scope (optional) The scope of the function
169
on : function(key, fn, scope){
170
var keyCode, shift, ctrl, alt;
171
if(typeof key == "object" && !Ext.isArray(key)){
190
handleKeyDown : function(e){
191
if(this.enabled){ //just in case
192
var b = this.bindings;
193
for(var i = 0, len = b.length; i < len; i++){
200
* Returns true if this KeyMap is enabled
203
isEnabled : function(){
208
* Enables this KeyMap
212
this.el.on(this.eventName, this.handleKeyDown, this);
218
* Disable this KeyMap
222
this.el.removeListener(this.eventName, this.handleKeyDown, this);
223
this.enabled = false;
b'\\ No newline at end of file'