Project

General

Profile

Download (3.57 KB) Statistics
| Branch: | Tag: | Revision:
1
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
2
 * full list of contributors). Published under the 2-clause BSD license.
3
 * See license.txt in the OpenLayers distribution or repository for the
4
 * full text of the license. */
5

    
6
/**
7
 * @requires OpenLayers/Handler.js
8
 * @requires OpenLayers/Events.js
9
 */
10

    
11
/**
12
 * Class: OpenLayers.handler.Keyboard
13
 * A handler for keyboard events.  Create a new instance with the
14
 *     <OpenLayers.Handler.Keyboard> constructor.
15
 * 
16
 * Inherits from:
17
 *  - <OpenLayers.Handler> 
18
 */
19
OpenLayers.Handler.Keyboard = OpenLayers.Class(OpenLayers.Handler, {
20

    
21
    /* http://www.quirksmode.org/js/keys.html explains key x-browser
22
        key handling quirks in pretty nice detail */
23

    
24
    /** 
25
     * Constant: KEY_EVENTS
26
     * keydown, keypress, keyup
27
     */
28
    KEY_EVENTS: ["keydown", "keyup"],
29

    
30
    /** 
31
    * Property: eventListener
32
    * {Function}
33
    */
34
    eventListener: null,
35

    
36
    /**
37
     * Property: observeElement
38
     * {DOMElement|String} The DOM element on which we listen for
39
     *     key events. Default to the document.
40
     */
41
    observeElement: null,
42

    
43
    /**
44
     * Constructor: OpenLayers.Handler.Keyboard
45
     * Returns a new keyboard handler.
46
     * 
47
     * Parameters:
48
     * control - {<OpenLayers.Control>} The control that is making use of
49
     *     this handler.  If a handler is being used without a control, the
50
     *     handlers setMap method must be overridden to deal properly with
51
     *     the map.
52
     * callbacks - {Object} An object containing a single function to be
53
     *     called when the drag operation is finished. The callback should
54
     *     expect to recieve a single argument, the pixel location of the event.
55
     *     Callbacks for 'keydown', 'keypress', and 'keyup' are supported.
56
     * options - {Object} Optional object whose properties will be set on the
57
     *     handler.
58
     */
59
    initialize: function(control, callbacks, options) {
60
        OpenLayers.Handler.prototype.initialize.apply(this, arguments);
61
        // cache the bound event listener method so it can be unobserved later
62
        this.eventListener = OpenLayers.Function.bindAsEventListener(
63
            this.handleKeyEvent, this
64
        );
65
    },
66
    
67
    /**
68
     * Method: destroy
69
     */
70
    destroy: function() {
71
        this.deactivate();
72
        this.eventListener = null;
73
        OpenLayers.Handler.prototype.destroy.apply(this, arguments);
74
    },
75

    
76
    /**
77
     * Method: activate
78
     */
79
    activate: function() {
80
        if (OpenLayers.Handler.prototype.activate.apply(this, arguments)) {
81
            this.observeElement = this.observeElement || document;
82
            for (var i=0, len=this.KEY_EVENTS.length; i<len; i++) {
83
                OpenLayers.Event.observe(
84
                    this.observeElement, this.KEY_EVENTS[i], this.eventListener);
85
            }
86
            return true;
87
        } else {
88
            return false;
89
        }
90
    },
91

    
92
    /**
93
     * Method: deactivate
94
     */
95
    deactivate: function() {
96
        var deactivated = false;
97
        if (OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) {
98
            for (var i=0, len=this.KEY_EVENTS.length; i<len; i++) {
99
                OpenLayers.Event.stopObserving(
100
                    this.observeElement, this.KEY_EVENTS[i], this.eventListener);
101
            }
102
            deactivated = true;
103
        }
104
        return deactivated;
105
    },
106

    
107
    /**
108
     * Method: handleKeyEvent 
109
     */
110
    handleKeyEvent: function (evt) {
111
        if (this.checkModifiers(evt)) {
112
            this.callback(evt.type, [evt]);
113
        }
114
    },
115

    
116
    CLASS_NAME: "OpenLayers.Handler.Keyboard"
117
});
(6-6/12)