diff --git a/examples/accessible.html b/examples/accessible.html index fcde73ebdb..36236d5b97 100644 --- a/examples/accessible.html +++ b/examples/accessible.html @@ -46,7 +46,11 @@ var map = null; function init(){ var options = { - controls: [new OpenLayers.Control.KeyboardDefaults()] + controls: [ + new OpenLayers.Control.KeyboardDefaults({ + observeElement: 'map' + }) + ] }; map = new OpenLayers.Map('map', options); var wms = new OpenLayers.Layer.WMS( diff --git a/lib/OpenLayers/Control/KeyboardDefaults.js b/lib/OpenLayers/Control/KeyboardDefaults.js index ee4b38c5d9..2589269590 100644 --- a/lib/OpenLayers/Control/KeyboardDefaults.js +++ b/lib/OpenLayers/Control/KeyboardDefaults.js @@ -34,6 +34,15 @@ OpenLayers.Control.KeyboardDefaults = OpenLayers.Class(OpenLayers.Control, { */ slideFactor: 75, + /** + * APIProperty: observeElement + * {DOMelement|String} The DOM element to handle keys for. You + * can use the map div here, to have the navigation keys + * work when the map div has the focus. If undefined the + * document is used. + */ + observeElement: null, + /** * Constructor: OpenLayers.Control.KeyboardDefaults */ @@ -43,8 +52,11 @@ OpenLayers.Control.KeyboardDefaults = OpenLayers.Class(OpenLayers.Control, { * Create handler. */ draw: function() { - this.handler = new OpenLayers.Handler.Keyboard( this, { - "keydown": this.defaultKeyPress }); + var observeElement = this.observeElement || document; + this.handler = new OpenLayers.Handler.Keyboard( this, + {"keydown": this.defaultKeyPress}, + {observeElement: observeElement} + ); }, /** diff --git a/lib/OpenLayers/Handler/Keyboard.js b/lib/OpenLayers/Handler/Keyboard.js index c446cb0988..8b3d697f6b 100644 --- a/lib/OpenLayers/Handler/Keyboard.js +++ b/lib/OpenLayers/Handler/Keyboard.js @@ -33,6 +33,13 @@ OpenLayers.Handler.Keyboard = OpenLayers.Class(OpenLayers.Handler, { */ eventListener: null, + /** + * Property: observeElement + * {DOMElement|String} The DOM element on which we listen for + * key events. Default to the document. + */ + observeElement: null, + /** * Constructor: OpenLayers.Handler.Keyboard * Returns a new keyboard handler. @@ -71,9 +78,10 @@ OpenLayers.Handler.Keyboard = OpenLayers.Class(OpenLayers.Handler, { */ activate: function() { if (OpenLayers.Handler.prototype.activate.apply(this, arguments)) { + this.observeElement = this.observeElement || document; for (var i=0, len=this.KEY_EVENTS.length; i