KeyboardDefaults.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * @requires OpenLayers/Control.js
  7. * @requires OpenLayers/Handler/Keyboard.js
  8. * @requires OpenLayers/Events.js
  9. */
  10. /**
  11. * Class: OpenLayers.Control.KeyboardDefaults
  12. * The KeyboardDefaults control adds panning and zooming functions, controlled
  13. * with the keyboard. By default arrow keys pan, +/- keys zoom & Page Up/Page
  14. * Down/Home/End scroll by three quarters of a page.
  15. *
  16. * This control has no visible appearance.
  17. *
  18. * Inherits from:
  19. * - <OpenLayers.Control>
  20. */
  21. OpenLayers.Control.KeyboardDefaults = OpenLayers.Class(OpenLayers.Control, {
  22. /**
  23. * APIProperty: autoActivate
  24. * {Boolean} Activate the control when it is added to a map. Default is
  25. * true.
  26. */
  27. autoActivate: true,
  28. /**
  29. * APIProperty: slideFactor
  30. * Pixels to slide by.
  31. */
  32. slideFactor: 75,
  33. /**
  34. * APIProperty: observeElement
  35. * {DOMelement|String} The DOM element to handle keys for. You
  36. * can use the map div here, to have the navigation keys
  37. * work when the map div has the focus. If undefined the
  38. * document is used.
  39. */
  40. observeElement: null,
  41. /**
  42. * Constructor: OpenLayers.Control.KeyboardDefaults
  43. */
  44. /**
  45. * Method: draw
  46. * Create handler.
  47. */
  48. draw: function() {
  49. var observeElement = this.observeElement || document;
  50. this.handler = new OpenLayers.Handler.Keyboard( this,
  51. {"keydown": this.defaultKeyPress},
  52. {observeElement: observeElement}
  53. );
  54. },
  55. /**
  56. * Method: defaultKeyPress
  57. * When handling the key event, we only use evt.keyCode. This holds
  58. * some drawbacks, though we get around them below. When interpretting
  59. * the keycodes below (including the comments associated with them),
  60. * consult the URL below. For instance, the Safari browser returns
  61. * "IE keycodes", and so is supported by any keycode labeled "IE".
  62. *
  63. * Very informative URL:
  64. * http://unixpapa.com/js/key.html
  65. *
  66. * Parameters:
  67. * evt - {Event}
  68. */
  69. defaultKeyPress: function (evt) {
  70. var size, handled = true;
  71. var target = OpenLayers.Event.element(evt);
  72. if (target &&
  73. (target.tagName == 'INPUT' ||
  74. target.tagName == 'TEXTAREA' ||
  75. target.tagName == 'SELECT')) {
  76. return;
  77. }
  78. switch (evt.keyCode) {
  79. case OpenLayers.Event.KEY_LEFT:
  80. this.map.pan(-this.slideFactor, 0);
  81. break;
  82. case OpenLayers.Event.KEY_RIGHT:
  83. this.map.pan(this.slideFactor, 0);
  84. break;
  85. case OpenLayers.Event.KEY_UP:
  86. this.map.pan(0, -this.slideFactor);
  87. break;
  88. case OpenLayers.Event.KEY_DOWN:
  89. this.map.pan(0, this.slideFactor);
  90. break;
  91. case 33: // Page Up. Same in all browsers.
  92. size = this.map.getSize();
  93. this.map.pan(0, -0.75*size.h);
  94. break;
  95. case 34: // Page Down. Same in all browsers.
  96. size = this.map.getSize();
  97. this.map.pan(0, 0.75*size.h);
  98. break;
  99. case 35: // End. Same in all browsers.
  100. size = this.map.getSize();
  101. this.map.pan(0.75*size.w, 0);
  102. break;
  103. case 36: // Home. Same in all browsers.
  104. size = this.map.getSize();
  105. this.map.pan(-0.75*size.w, 0);
  106. break;
  107. case 43: // +/= (ASCII), keypad + (ASCII, Opera)
  108. case 61: // +/= (Mozilla, Opera, some ASCII)
  109. case 187: // +/= (IE)
  110. case 107: // keypad + (IE, Mozilla)
  111. this.map.zoomIn();
  112. break;
  113. case 45: // -/_ (ASCII, Opera), keypad - (ASCII, Opera)
  114. case 109: // -/_ (Mozilla), keypad - (Mozilla, IE)
  115. case 189: // -/_ (IE)
  116. case 95: // -/_ (some ASCII)
  117. this.map.zoomOut();
  118. break;
  119. default:
  120. handled = false;
  121. }
  122. if (handled) {
  123. // prevent browser default not to move the page
  124. // when moving the page with the keyboard
  125. OpenLayers.Event.stop(evt);
  126. }
  127. },
  128. CLASS_NAME: "OpenLayers.Control.KeyboardDefaults"
  129. });