Print Friendly

# Class Ext.KeyMap

Package: | Ext
Class: | KeyMap
Extends: | Object
Defined In: | KeyMap.js

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.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.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.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 KepMap starts enabled
Properties - Methods - Events

## Public Properties

Property | Defined By
--- | ---
| stopEvent : Boolean | KeyMap
True to stop the event from bubbling and prevent the default browser action if the key was handled by the KeyMap (def...

## Public Methods

Method | Defined By
--- | ---
| KeyMap(`String/HTMLElement/Ext.Element el`, `Object config`, [`String eventName`]) | KeyMap
| addBinding(`Object config`) : void | KeyMap
Add a new binding to this KeyMap. The following config object properties are supported: Property Type ...
| disable() : void | KeyMap
Disable this KeyMap
| enable() : void | KeyMap
Enable this KeyMap
| isEnabled() : Boolean | KeyMap
Returns true if this KepMap is enabled

## Public Events

This class has no public events.

## Property Details

### stopEvent

`public Boolean stopEvent`
True to stop the event from bubbling and prevent the default browser action if the key was handled by the KeyMap (defaults to false)
This property is defined by KeyMap.

## Constructor Details

### KeyMap

`public function KeyMap(`String/HTMLElement/Ext.Element el`, `Object config`, [`String eventName`])`
Parameters:

- `el` : String/HTMLElement/Ext.Element
  The element to bind to
- `config` : Object
  The config
- `eventName` : String
  (optional) The event to bind to (defaults to "keydown")

## Method Details

### addBinding

`public function addBinding(`Object config`)`
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 (defaults to false)
    ctrl        Boolean          True to handle key only when ctrl is pressed (defaults to false)
    alt         Boolean          True to handle key only when alt is pressed (defaults to false)
    fn          Function         The function to call when KeyMap finds the expected key combination
    scope       Object           The scope of the callback function

Usage:

    // Create a KeyMap
    var map = new Ext.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:

- `config` : Object
  A single KeyMap config

Returns:

- `void`

This method is defined by KeyMap.

### disable

`public function disable()`
Disable this KeyMap
Parameters:

- None.

Returns:

- `void`

This method is defined by KeyMap.

### enable

`public function enable()`
Enable this KeyMap
Parameters:

- None.

Returns:

- `void`

This method is defined by KeyMap.

### isEnabled

`public function isEnabled()`
Returns true if this KepMap is enabled
Parameters:

- None.

Returns:

- `Boolean`

This method is defined by KeyMap.

Ext - Copyright © 2006-2007 Ext JS, LLC
All rights reserved.