KeyboardDefaults.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright (c) 2006-2011 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. * @requires OpenLayers/Control.js
  7. * @requires OpenLayers/Handler/Keyboard.js
  8. */
  9. /**
  10. * Class: OpenLayers.Control.KeyboardDefaults
  11. * The KeyboardDefaults control adds panning and zooming functions, controlled
  12. * with the keyboard. By default arrow keys pan, +/- keys zoom & Page Up/Page
  13. * Down/Home/End scroll by three quarters of a page.
  14. *
  15. * This control has no visible appearance.
  16. *
  17. * Inherits from:
  18. * - <OpenLayers.Control>
  19. */
  20. OpenLayers.Control.KeyboardDefaults = OpenLayers.Class(OpenLayers.Control, {
  21. /**
  22. * APIProperty: autoActivate
  23. * {Boolean} Activate the control when it is added to a map. Default is
  24. * true.
  25. */
  26. autoActivate: true,
  27. /**
  28. * APIProperty: slideFactor
  29. * Pixels to slide by.
  30. */
  31. slideFactor: 75,
  32. /**
  33. * Constructor: OpenLayers.Control.KeyboardDefaults
  34. */
  35. /**
  36. * Method: draw
  37. * Create handler.
  38. */
  39. draw: function() {
  40. this.handler = new OpenLayers.Handler.Keyboard( this, {
  41. "keydown": this.defaultKeyPress });
  42. },
  43. /**
  44. * Method: defaultKeyPress
  45. * When handling the key event, we only use evt.keyCode. This holds
  46. * some drawbacks, though we get around them below. When interpretting
  47. * the keycodes below (including the comments associated with them),
  48. * consult the URL below. For instance, the Safari browser returns
  49. * "IE keycodes", and so is supported by any keycode labeled "IE".
  50. *
  51. * Very informative URL:
  52. * http://unixpapa.com/js/key.html
  53. *
  54. * Parameters:
  55. * code - {Integer}
  56. */
  57. defaultKeyPress: function (evt) {
  58. switch(evt.keyCode) {
  59. case OpenLayers.Event.KEY_LEFT:
  60. this.map.pan(-this.slideFactor, 0);
  61. break;
  62. case OpenLayers.Event.KEY_RIGHT:
  63. this.map.pan(this.slideFactor, 0);
  64. break;
  65. case OpenLayers.Event.KEY_UP:
  66. this.map.pan(0, -this.slideFactor);
  67. break;
  68. case OpenLayers.Event.KEY_DOWN:
  69. this.map.pan(0, this.slideFactor);
  70. break;
  71. case 33: // Page Up. Same in all browsers.
  72. var size = this.map.getSize();
  73. this.map.pan(0, -0.75*size.h);
  74. break;
  75. case 34: // Page Down. Same in all browsers.
  76. var size = this.map.getSize();
  77. this.map.pan(0, 0.75*size.h);
  78. break;
  79. case 35: // End. Same in all browsers.
  80. var size = this.map.getSize();
  81. this.map.pan(0.75*size.w, 0);
  82. break;
  83. case 36: // Home. Same in all browsers.
  84. var size = this.map.getSize();
  85. this.map.pan(-0.75*size.w, 0);
  86. break;
  87. case 43: // +/= (ASCII), keypad + (ASCII, Opera)
  88. case 61: // +/= (Mozilla, Opera, some ASCII)
  89. case 187: // +/= (IE)
  90. case 107: // keypad + (IE, Mozilla)
  91. this.map.zoomIn();
  92. break;
  93. case 45: // -/_ (ASCII, Opera), keypad - (ASCII, Opera)
  94. case 109: // -/_ (Mozilla), keypad - (Mozilla, IE)
  95. case 189: // -/_ (IE)
  96. case 95: // -/_ (some ASCII)
  97. this.map.zoomOut();
  98. break;
  99. }
  100. },
  101. CLASS_NAME: "OpenLayers.Control.KeyboardDefaults"
  102. });