]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Control/KeyboardDefaults.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Control / KeyboardDefaults.js
1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2  * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3  * full text of the license. */
4
5
6 /**
7  * @requires OpenLayers/Control.js
8  * @requires OpenLayers/Handler/Keyboard.js
9  */
10
11 /**
12  * Class: OpenLayers.Control.KeyboardDefaults
13  * The KeyboardDefaults control adds panning and zooming functions, controlled
14  * with the keyboard. By default arrow keys pan, +/- keys zoom & Page Up/Page
15  * Down/Home/End scroll by three quarters of a page.
16  * 
17  * This control has no visible appearance.
18  *
19  * Inherits from:
20  *  - <OpenLayers.Control>
21  */
22 OpenLayers.Control.KeyboardDefaults = OpenLayers.Class(OpenLayers.Control, {
23
24     /**
25      * APIProperty: slideFactor
26      * Pixels to slide by.
27      */
28     slideFactor: 75,
29
30     /**
31      * Constructor: OpenLayers.Control.KeyboardDefaults
32      */
33     initialize: function() {
34         OpenLayers.Control.prototype.initialize.apply(this, arguments);
35     },
36     
37     /**
38      * APIMethod: destroy
39      */
40     destroy: function() {
41         if (this.handler) {
42             this.handler.destroy();
43         }        
44         this.handler = null;
45         
46         OpenLayers.Control.prototype.destroy.apply(this, arguments);
47     },
48     
49     /**
50      * Method: draw
51      * Create handler.
52      */
53     draw: function() {
54         this.handler = new OpenLayers.Handler.Keyboard( this, { 
55                                 "keydown": this.defaultKeyPress });
56         this.activate();
57     },
58     
59     /**
60      * Method: defaultKeyPress
61      * When handling the key event, we only use evt.keyCode. This holds 
62      * some drawbacks, though we get around them below. When interpretting
63      * the keycodes below (including the comments associated with them),
64      * consult the URL below. For instance, the Safari browser returns
65      * "IE keycodes", and so is supported by any keycode labeled "IE".
66      * 
67      * Very informative URL:
68      *    http://unixpapa.com/js/key.html
69      *
70      * Parameters:
71      * code - {Integer} 
72      */
73     defaultKeyPress: function (evt) {
74         switch(evt.keyCode) {
75             case OpenLayers.Event.KEY_LEFT:
76                 this.map.pan(-this.slideFactor, 0);
77                 break;
78             case OpenLayers.Event.KEY_RIGHT: 
79                 this.map.pan(this.slideFactor, 0);
80                 break;
81             case OpenLayers.Event.KEY_UP:
82                 this.map.pan(0, -this.slideFactor);
83                 break;
84             case OpenLayers.Event.KEY_DOWN:
85                 this.map.pan(0, this.slideFactor);
86                 break;
87             
88             case 33: // Page Up. Same in all browsers.
89                 var size = this.map.getSize();
90                 this.map.pan(0, -0.75*size.h);
91                 break;
92             case 34: // Page Down. Same in all browsers.
93                 var size = this.map.getSize();
94                 this.map.pan(0, 0.75*size.h);
95                 break; 
96             case 35: // End. Same in all browsers.
97                 var size = this.map.getSize();
98                 this.map.pan(0.75*size.w, 0);
99                 break; 
100             case 36: // Home. Same in all browsers.
101                 var size = this.map.getSize();
102                 this.map.pan(-0.75*size.w, 0);
103                 break; 
104
105             case 43:  // +/= (ASCII), keypad + (ASCII, Opera)
106             case 61:  // +/= (Mozilla, Opera, some ASCII)
107             case 187: // +/= (IE)
108             case 107: // keypad + (IE, Mozilla)
109                 this.map.zoomIn();
110                 break; 
111             case 45:  // -/_ (ASCII, Opera), keypad - (ASCII, Opera)
112             case 109: // -/_ (Mozilla), keypad - (Mozilla, IE)
113             case 189: // -/_ (IE)
114             case 95:  // -/_ (some ASCII)
115                 this.map.zoomOut();
116                 break; 
117         } 
118     },
119
120     CLASS_NAME: "OpenLayers.Control.KeyboardDefaults"
121 });