2
* Ext JS Library 3.0 RC2
3
* Copyright(c) 2006-2009, 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, False to handle the key only when shift is not pressed (defaults to undefined)
80
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)
81
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)
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 from bubbling and prevent the default browser action if the key was handled by the KeyMap (defaults to false)
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
fn = config.fn || config.handler,
116
scope = config.scope;
118
if (config.stopEvent) {
119
this.stopEvent = config.stopEvent;
122
if(typeof keyCode == "string"){
124
var keyString = keyCode.toUpperCase();
125
for(var j = 0, len = keyString.length; j < len; j++){
126
ks.push(keyString.charCodeAt(j));
130
var keyArray = Ext.isArray(keyCode);
132
var handler = function(e){
133
if(this.checkModifiers(config, e)){
136
for(var i = 0, len = keyCode.length; i < len; i++){
141
fn.call(scope || window, k, e);
150
fn.call(scope || window, k, e);
155
this.bindings.push(handler);
159
checkModifiers: function(config, e){
160
var val, key, keys = ['shift', 'ctrl', 'alt'];
161
for (var i = 0, len = keys.length; i < len; ++i){
162
key = keys[i], val = config[key];
163
if(!(val === undefined || (val === e[key + 'Key']))){
171
* Shorthand for adding a single key listener
172
* @param {Number/Array/Object} key Either the numeric key code, array of key codes or an object with the
174
* {key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}
175
* @param {Function} fn The function to call
176
* @param {Object} scope (optional) The scope of the function
178
on : function(key, fn, scope){
179
var keyCode, shift, ctrl, alt;
180
if(typeof key == "object" && !Ext.isArray(key)){
199
handleKeyDown : function(e){
200
if(this.enabled){ //just in case
201
var b = this.bindings;
202
for(var i = 0, len = b.length; i < len; i++){
209
* Returns true if this KeyMap is enabled
212
isEnabled : function(){
217
* Enables this KeyMap
221
this.el.on(this.eventName, this.handleKeyDown, this);
227
* Disable this KeyMap
231
this.el.removeListener(this.eventName, this.handleKeyDown, this);
232
this.enabled = false;
237
* Convenience function for setting disabled/enabled by boolean.
238
* @param {Boolean} disabled
240
setDisabled : function(disabled){
241
this[disabled ? "disable" : "enable"]();
b'\\ No newline at end of file'