From 2d665dbc1ba58db55c486597c62f44e50128a5da Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Fri, 9 Jun 2017 11:42:35 -0400 Subject: [PATCH] Ignore Ctrl and Alt if both are present (e.g. AltGr on Windows) (see #4096) --- modules/lib/d3.keybinding.js | 6 ++++-- test/spec/lib/d3.keybinding.js | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/modules/lib/d3.keybinding.js b/modules/lib/d3.keybinding.js index 443f0dfe4..9094a2ba8 100644 --- a/modules/lib/d3.keybinding.js +++ b/modules/lib/d3.keybinding.js @@ -66,8 +66,10 @@ export function d3keybinding(namespace) { } // test modifier keys - if (event.ctrlKey !== binding.event.modifiers.ctrlKey) return false; - if (event.altKey !== binding.event.modifiers.altKey) return false; + if (!(event.ctrlKey && event.altKey)) { // if both are set, assume AltGr and skip it - #4096 + if (event.ctrlKey !== binding.event.modifiers.ctrlKey) return false; + if (event.altKey !== binding.event.modifiers.altKey) return false; + } if (event.metaKey !== binding.event.modifiers.metaKey) return false; if (testShift && event.shiftKey !== binding.event.modifiers.shiftKey) return false; diff --git a/test/spec/lib/d3.keybinding.js b/test/spec/lib/d3.keybinding.js index 62f45118b..c4ce6a5da 100644 --- a/test/spec/lib/d3.keybinding.js +++ b/test/spec/lib/d3.keybinding.js @@ -38,6 +38,30 @@ describe('d3.keybinding', function() { expect(spy).to.have.been.calledOnce; }); + it('matches the binding even when shift is present', function () { + d3.select(document).call(keybinding.on('A', spy)); + + happen.keydown(document, {keyCode: 65, shiftKey: true}); + expect(spy).to.have.been.calledOnce; + }); + + it('matches shifted bindings before unshifted bindings', function () { + var spy2 = sinon.spy(); + d3.select(document).call(keybinding.on('A', spy2)); + d3.select(document).call(keybinding.on('⇧A', spy)); + + happen.keydown(document, {keyCode: 65, shiftKey: true}); + expect(spy).to.have.been.calledOnce; + expect(spy2).not.to.have.been.called; + }); + + it('ignores alt and control if both are present (e.g. as AltGr) #4096', function () { + d3.select(document).call(keybinding.on('A', spy)); + + happen.keydown(document, {keyCode: 65, altKey: true, ctrlKey: true}); + expect(spy).to.have.been.calledOnce; + }); + it('adds multiple bindings given an array of keys', function () { d3.select(document).call(keybinding.on(['A','B'], spy));