For up to date documentation and features, visit http://docs.sencha.com/ext-js/4-0

## Sencha Documentation

# Ext.util.KeyMap

Handles mapping keys to actions for an element. One key map can be used for multiple actions. The constructor accepts the same config object as defined by addBinding. If you bind a callback function to a KeyMap, anytime the KeyMap handles an expected key combination it will call the function with this signature (if the match is a multi-key combination the callback will still be called only once): (String key, Ext.EventObject e) A KeyMap can also handle a string representation of keys.
Usage:

    // map one key by key code
    var map = new Ext.util.KeyMap("my-element", {
        key: 13, // or Ext.EventObject.ENTER
        fn: myHandler,
        scope: myObject
    });

    // map multiple keys to one action by string
    var map = new Ext.util.KeyMap("my-element", {
        key: "a\r\n\t",
        fn: myHandler,
        scope: myObject
    });

    // map multiple keys to multiple actions by strings and array of codes
    var map = new Ext.util.KeyMap("my-element", [
        {
            key: [10,13],
            fn: function(){ alert("Return was pressed"); }
        }, {
            key: "abc",
            fn: function(){ alert('a, b or c was pressed'); }
        }, {
            key: "\t",
            ctrl:true,
            shift:true,
            fn: function(){ alert('Control + shift + tab was pressed.'); }
        }
    ]);

Note: A KeyMap starts enabled

Defined By

### Methods

Ext.util.KeyMap
view source
KeyMap( Mixed el, Object binding, [String eventName]) : void

### Parameters

- el : Mixed

  The element to bind to

- binding : Object

  The binding (see addBinding)

- eventName : String

  (optional) The event to bind to (defaults to "keydown")

### Returns

- void

Ext.util.KeyMap
view source
addBinding( Object/Array binding) : void
Add a new binding to this KeyMap. The following config object properties are supported: Property Type ...

Add a new binding to this KeyMap. The following config object properties are supported:

    Property            Type             Description
    ----------          ---------------  ----------------------------------------------------------------------
    key                 String/Array     A single keycode or an array of keycodes to handle
    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)
    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)
    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)
    handler             Function         The function to call when KeyMap finds the expected key combination
    fn                  Function         Alias of handler (for backwards-compatibility)
    scope               Object           The scope of the callback function
    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.

Usage:

    // Create a KeyMap
    var map = new Ext.util.KeyMap(document, {
        key: Ext.EventObject.ENTER,
        fn: handleKey,
        scope: this
    });

    //Add a new binding to the existing KeyMap later
    map.addBinding({
        key: 'abc',
        shift: true,
        fn: handleKey,
        scope: this
    });

### Parameters

- binding : Object/Array

  A single KeyMap config or an array of configs

### Returns

- void

Ext.util.KeyMap
view source
destroy( Boolean removeEl) : void

Destroys the KeyMap instance and removes all handlers.

Destroys the KeyMap instance and removes all handlers.

### Parameters

- removeEl : Boolean

  True to also remove the attached element

### Returns

- void

Ext.util.KeyMap
view source
disable : void

Disable this KeyMap

Disable this KeyMap

### Returns

- void

Ext.util.KeyMap
view source
enable : void

Enables this KeyMap

Enables this KeyMap

### Returns

- void

Ext.util.KeyMap
view source
isEnabled : Boolean

Returns true if this KeyMap is enabled

Returns true if this KeyMap is enabled

### Returns

- Boolean

Ext.util.KeyMap
view source
on( Number/Array/Object key, Function fn, [Object scope]) : void

Shorthand for adding a single key listener

Shorthand for adding a single key listener

### Parameters

- key : Number/Array/Object

  Either the numeric key code, array of key codes or an object with the following options: {key: (number or array), shift: (true/false), ctrl: (true/false), alt: (true/false)}

- fn : Function

  The function to call

- scope : Object

  (optional) The scope (`this` reference) in which the function is executed. Defaults to the browser window.

### Returns

- void

Ext.util.KeyMap
view source
setDisabled( Boolean disabled) : void

Convenience function for setting disabled/enabled by boolean.

Convenience function for setting disabled/enabled by boolean.

### Parameters

- disabled : Boolean

### Returns

- void