Keyboard.js 3.3 KB

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