1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt 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 | * Constructor: OpenLayers.Handler.Keyboard |
---|
38 | * Returns a new keyboard handler. |
---|
39 | * |
---|
40 | * Parameters: |
---|
41 | * control - {<OpenLayers.Control>} The control that is making use of |
---|
42 | * this handler. If a handler is being used without a control, the |
---|
43 | * handlers setMap method must be overridden to deal properly with |
---|
44 | * the map. |
---|
45 | * callbacks - {Object} An object containing a single function to be |
---|
46 | * called when the drag operation is finished. The callback should |
---|
47 | * expect to recieve a single argument, the pixel location of the event. |
---|
48 | * Callbacks for 'keydown', 'keypress', and 'keyup' are supported. |
---|
49 | * options - {Object} Optional object whose properties will be set on the |
---|
50 | * handler. |
---|
51 | */ |
---|
52 | initialize: function(control, callbacks, options) { |
---|
53 | OpenLayers.Handler.prototype.initialize.apply(this, arguments); |
---|
54 | // cache the bound event listener method so it can be unobserved later |
---|
55 | this.eventListener = OpenLayers.Function.bindAsEventListener( |
---|
56 | this.handleKeyEvent, this |
---|
57 | ); |
---|
58 | }, |
---|
59 | |
---|
60 | /** |
---|
61 | * Method: destroy |
---|
62 | */ |
---|
63 | destroy: function() { |
---|
64 | this.deactivate(); |
---|
65 | this.eventListener = null; |
---|
66 | OpenLayers.Handler.prototype.destroy.apply(this, arguments); |
---|
67 | }, |
---|
68 | |
---|
69 | /** |
---|
70 | * Method: activate |
---|
71 | */ |
---|
72 | activate: function() { |
---|
73 | if (OpenLayers.Handler.prototype.activate.apply(this, arguments)) { |
---|
74 | for (var i=0, len=this.KEY_EVENTS.length; i<len; i++) { |
---|
75 | OpenLayers.Event.observe( |
---|
76 | document, this.KEY_EVENTS[i], this.eventListener); |
---|
77 | } |
---|
78 | return true; |
---|
79 | } else { |
---|
80 | return false; |
---|
81 | } |
---|
82 | }, |
---|
83 | |
---|
84 | /** |
---|
85 | * Method: deactivate |
---|
86 | */ |
---|
87 | deactivate: function() { |
---|
88 | var deactivated = false; |
---|
89 | if (OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) { |
---|
90 | for (var i=0, len=this.KEY_EVENTS.length; i<len; i++) { |
---|
91 | OpenLayers.Event.stopObserving( |
---|
92 | document, this.KEY_EVENTS[i], this.eventListener); |
---|
93 | } |
---|
94 | deactivated = true; |
---|
95 | } |
---|
96 | return deactivated; |
---|
97 | }, |
---|
98 | |
---|
99 | /** |
---|
100 | * Method: handleKeyEvent |
---|
101 | */ |
---|
102 | handleKeyEvent: function (evt) { |
---|
103 | if (this.checkModifiers(evt)) { |
---|
104 | this.callback(evt.type, [evt]); |
---|
105 | } |
---|
106 | }, |
---|
107 | |
---|
108 | CLASS_NAME: "OpenLayers.Handler.Keyboard" |
---|
109 | }); |
---|