From 36d8a207ab8d0d27b0a93c19f95c19dd44361ed8 Mon Sep 17 00:00:00 2001 From: Henrik Fehlauer Date: Tue, 26 May 2020 18:00:00 +0000 Subject: [PATCH] Convert all shortcuts to act on keydown already Contrary to mouse clicks, when pressing keys on a keyboard the standard behavior is to perform the associated action immediately, not only when releasing the key again. This should also improve the perceived performance slightly. Note that the 'D' shortcut had formerly been handled by Leaflet, which we now have to do on our own. While at it, move the character codes over to the options variable, as found in other parts of the codebase already. Also removing the listener from the container does not seem needed anymore nowadays. --- js/plugin/POIMarkers.js | 12 +++++------- js/plugin/Routing.js | 26 +++++++++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/js/plugin/POIMarkers.js b/js/plugin/POIMarkers.js index 51c156c..25efd33 100644 --- a/js/plugin/POIMarkers.js +++ b/js/plugin/POIMarkers.js @@ -42,11 +42,9 @@ BR.PoiMarkers = L.Control.extend({ self.draw(false); }); - var container = new L.DomUtil.create('div'); - // keys not working when map container does not have focus, use document instead - L.DomEvent.removeListener(container, 'keyup', this._keyupListener); - L.DomEvent.addListener(document, 'keyup', this._keyupListener, this); + L.DomEvent.addListener(document, 'keydown', this._keydownListener, this); + var container = new L.DomUtil.create('div'); return container; }, @@ -62,14 +60,14 @@ BR.PoiMarkers = L.Control.extend({ } }, - _keyupListener: function(e) { + _keydownListener: function(e) { // Suppress shortcut handling when a text input field is focussed if (document.activeElement.type == 'text' || document.activeElement.type == 'textarea') { return; } - if (e.keyCode === this.options.shortcut.draw.disable) { + if (e.keyCode === this.options.shortcut.draw.disable && !e.repeat) { this.draw(false); - } else if (e.keyCode === this.options.shortcut.draw.enable) { + } else if (e.keyCode === this.options.shortcut.draw.enable && !e.repeat) { this.draw(true); } }, diff --git a/js/plugin/Routing.js b/js/plugin/Routing.js index 9249dca..668818c 100644 --- a/js/plugin/Routing.js +++ b/js/plugin/Routing.js @@ -23,6 +23,12 @@ BR.Routing = L.Routing.extend({ textFunction: function(distance) { return distance / 1000; } + }, + shortcut: { + draw: { + enable: 68, // char code for 'd' + disable: 27 // char code for 'ESC' + } } }, @@ -163,8 +169,7 @@ BR.Routing = L.Routing.extend({ this._draw ); - // keys not working when map container does not have focus, use document instead - L.DomEvent.removeListener(this._container, 'keyup', this._keyupListener); + L.DomEvent.addListener(document, 'keydown', this._keydownListener, this); L.DomEvent.addListener(document, 'keyup', this._keyupListener, this); // enable drawing mode @@ -336,16 +341,23 @@ BR.Routing = L.Routing.extend({ return segments; }, - _keyupListener: function(e) { + _keydownListener: function(e) { // Suppress shortcut handling when a text input field is focussed if (document.activeElement.type == 'text' || document.activeElement.type == 'textarea') { return; } - // add 'esc' to disable drawing - if (e.keyCode === 27) { + if (e.keyCode === this.options.shortcut.draw.disable && !e.repeat) { this._draw.disable(); - } else { - L.Routing.prototype._keyupListener.call(this, e); + } else if (e.keyCode === this.options.shortcut.draw.enable && !e.repeat) { + this._draw.enable(); + } + }, + + _keyupListener: function(e) { + // Prevent Leaflet from triggering drawing a second time on keyup, + // since this is already done in _keydownListener + if (e.keyCode === this.options.shortcut.draw.enable) { + return; } },