Keyboard.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/Handler.js
  7. * @requires OpenLayers/Events.js
  8. */
  9. /**
  10. * Class: OpenLayers.handler.Keyboard
  11. * A handler for keyboard events. Create a new instance with the
  12. * <OpenLayers.Handler.Keyboard> constructor.
  13. *
  14. * Inherits from:
  15. * - <OpenLayers.Handler>
  16. */
  17. OpenLayers.Handler.Keyboard = OpenLayers.Class(OpenLayers.Handler, {
  18. /* http://www.quirksmode.org/js/keys.html explains key x-browser
  19. key handling quirks in pretty nice detail */
  20. /**
  21. * Constant: KEY_EVENTS
  22. * keydown, keypress, keyup
  23. */
  24. KEY_EVENTS: ["keydown", "keyup"],
  25. /**
  26. * Property: eventListener
  27. * {Function}
  28. */
  29. eventListener: null,
  30. /**
  31. * Property: observeElement
  32. * {DOMElement|String} The DOM element on which we listen for
  33. * key events. Default to the document.
  34. */
  35. observeElement: null,
  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. * Method: destroy
  61. */
  62. destroy: function() {
  63. this.deactivate();
  64. this.eventListener = null;
  65. OpenLayers.Handler.prototype.destroy.apply(this, arguments);
  66. },
  67. /**
  68. * Method: activate
  69. */
  70. activate: function() {
  71. if (OpenLayers.Handler.prototype.activate.apply(this, arguments)) {
  72. this.observeElement = this.observeElement || document;
  73. for (var i=0, len=this.KEY_EVENTS.length; i<len; i++) {
  74. OpenLayers.Event.observe(
  75. this.observeElement, this.KEY_EVENTS[i], this.eventListener);
  76. }
  77. return true;
  78. } else {
  79. return false;
  80. }
  81. },
  82. /**
  83. * Method: deactivate
  84. */
  85. deactivate: function() {
  86. var deactivated = false;
  87. if (OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) {
  88. for (var i=0, len=this.KEY_EVENTS.length; i<len; i++) {
  89. OpenLayers.Event.stopObserving(
  90. this.observeElement, this.KEY_EVENTS[i], this.eventListener);
  91. }
  92. deactivated = true;
  93. }
  94. return deactivated;
  95. },
  96. /**
  97. * Method: handleKeyEvent
  98. */
  99. handleKeyEvent: function (evt) {
  100. if (this.checkModifiers(evt)) {
  101. this.callback(evt.type, [evt]);
  102. }
  103. },
  104. CLASS_NAME: "OpenLayers.Handler.Keyboard"
  105. });