From 4e94695c758215a950fe53911e1c1e30e47b9c98 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 4 Nov 2017 23:55:09 +0900 Subject: add key utils --- src/shared/utils/keys.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/shared/utils/keys.js (limited to 'src/shared/utils') diff --git a/src/shared/utils/keys.js b/src/shared/utils/keys.js new file mode 100644 index 0000000..dfdb954 --- /dev/null +++ b/src/shared/utils/keys.js @@ -0,0 +1,78 @@ +const modifierdKeyName = (name) => { + if (name.length === 1) { + return name; + } else if (name === 'Escape') { + return 'Esc'; + } + return name; +}; + +const fromKeyboardEvent = (e) => { + return { + key: modifierdKeyName(e.key), + shiftKey: e.shiftKey, + ctrlKey: e.ctrlKey, + altKey: e.altKey, + metaKey: e.metaKey, + }; +}; + +const fromMapKey = (key) => { + if (key.startsWith('<') && key.endsWith('>')) { + let inner = key.slice(1, -1); + let shift = inner.includes('S-'); + let base = inner.slice(inner.lastIndexOf('-') + 1); + if (shift && base.length === 1) { + base = base.toUpperCase(); + } else if (!shift && base.length === 1) { + base = base.toLowerCase(); + } + return { + key: base, + shiftKey: inner.includes('S-'), + ctrlKey: inner.includes('C-'), + altKey: inner.includes('A-'), + metaKey: inner.includes('M-'), + }; + } + return { + key: key, + shiftKey: key.toLowerCase() !== key, + ctrlKey: false, + altKey: false, + metaKey: false, + }; +}; + +const fromMapKeys = (keys) => { + const fromMapKeysRecursive = (remainings, mappedKeys) => { + if (remainings.length === 0) { + return mappedKeys; + } + + let nextPos = 1; + if (remainings.startsWith('<')) { + let ltPos = remainings.indexOf('>'); + if (ltPos > 0) { + nextPos = ltPos + 1; + } + } + + return fromMapKeysRecursive( + remainings.slice(nextPos), + mappedKeys.concat([fromMapKey(remainings.slice(0, nextPos))]) + ); + }; + + return fromMapKeysRecursive(keys, []); +}; + +const equals = (e1, e2) => { + return e1.key === e2.key && + e1.ctrlKey === e2.ctrlKey && + e1.metaKey === e2.metaKey && + e1.altKey === e2.altKey && + e1.shiftKey === e2.shiftKey; +}; + +export { fromKeyboardEvent, fromMapKey, fromMapKeys, equals }; -- cgit v1.2.3 From ccf3c7b421e804172827dd34a995290afc85af10 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 12 Nov 2017 18:21:28 +0900 Subject: fix for symbol keys --- src/shared/utils/keys.js | 10 +++++++++- test/shared/utils/keys.test.js | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) (limited to 'src/shared/utils') diff --git a/src/shared/utils/keys.js b/src/shared/utils/keys.js index dfdb954..fba8ce3 100644 --- a/src/shared/utils/keys.js +++ b/src/shared/utils/keys.js @@ -8,9 +8,17 @@ const modifierdKeyName = (name) => { }; const fromKeyboardEvent = (e) => { + let key = modifierdKeyName(e.key); + let shift = e.shiftKey; + if (key.length === 1 && key.toUpperCase() === key.toLowerCase()) { + // make shift false for symbols to enable key bindings by symbold keys. + // But this limits key bindings by symbol keys with Shift (such as Shift+$>. + shift = false; + } + return { key: modifierdKeyName(e.key), - shiftKey: e.shiftKey, + shiftKey: shift, ctrlKey: e.ctrlKey, altKey: e.altKey, metaKey: e.metaKey, diff --git a/test/shared/utils/keys.test.js b/test/shared/utils/keys.test.js index 77e2b12..5ca8b54 100644 --- a/test/shared/utils/keys.test.js +++ b/test/shared/utils/keys.test.js @@ -24,6 +24,18 @@ describe("keys util", () => { expect(k.altKey).to.be.false; expect(k.metaKey).to.be.true; }); + + it('returns from keyboard input Ctrl+$', () => { + // $ required shift pressing on most keyboards + let k = keys.fromKeyboardEvent({ + key: '$', shiftKey: true, ctrlKey: true, altKey: false, metaKey: false + }); + expect(k.key).to.equal('$'); + expect(k.shiftKey).to.be.false; + expect(k.ctrlKey).to.be.true; + expect(k.altKey).to.be.false; + expect(k.metaKey).to.be.false; + }); }); describe('fromMapKey', () => { -- cgit v1.2.3