From 6596e30392a1ca053223825eda5cde060394a4aa Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 9 Oct 2016 15:20:15 +0100 Subject: Add translate command for key mappings. Under *Custom key mappings* (on the options page), this implements: translate x y Whenever the users types `x` in normal mode or in visual mode, the `x` is replaced by `y`. For example: map รง l (which apparently would be helpful on Brazilian keyboards). Issues: - Do we want yet another hack like this? This would be documented only on the wiki. - If we allowed `translate ` (and extended `isEscape()` to use the translation), then we'd get the `exitMode` command for free (#2253). - Alternatively, instead of adding a new "command" called `translate`, we could overload the existing `map` command. Since these are single-key mappings, there's no ambiguity. (Although, I guess there's a risk some user has junk in their key mappings and would be taken by surprise). Inspired by isssue posted by @vhoyer (#2305). Fixes #2305. --- content_scripts/mode_key_handler.coffee | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'content_scripts') diff --git a/content_scripts/mode_key_handler.coffee b/content_scripts/mode_key_handler.coffee index 8c0ae4b8..7904d801 100644 --- a/content_scripts/mode_key_handler.coffee +++ b/content_scripts/mode_key_handler.coffee @@ -33,8 +33,16 @@ class KeyHandlerMode extends Mode # We cannot track keyup events if we lose the focus. blur: (event) => @alwaysContinueBubbling => @keydownEvents = {} if event.target == window + @keyTranslationRegistry = {} + chrome.storage.local.get "keyTranslationRegistry", (obj) => + @keyTranslationRegistry = obj.keyTranslationRegistry + chrome.storage.onChanged.addListener (changes, area) => + if area == "local" and changes.keyTranslationRegistry?.newValue? + @keyTranslationRegistry = changes.keyTranslationRegistry.newValue + onKeydown: (event) -> keyChar = KeyboardUtils.getKeyCharString event + keyChar = @keyTranslationRegistry[keyChar] ? keyChar isEscape = KeyboardUtils.isEscape event if isEscape and (@countPrefix != 0 or @keyState.length != 1) @keydownEvents[event.keyCode] = true @@ -61,6 +69,7 @@ class KeyHandlerMode extends Mode onKeypress: (event) -> keyChar = KeyboardUtils.getKeyCharString event + keyChar = @keyTranslationRegistry[keyChar] ? keyChar if @isMappedKey keyChar @handleKeyChar keyChar else if @isCountKey keyChar -- cgit v1.2.3 From 9c1012ad3a731b015b8a70b58828fbcd0acb7db0 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 9 Oct 2016 16:31:23 +0100 Subject: Extend key translation to include Escape. Here, these map to escape: translate x translate --- content_scripts/mode_key_handler.coffee | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/mode_key_handler.coffee b/content_scripts/mode_key_handler.coffee index 7904d801..9f8206fb 100644 --- a/content_scripts/mode_key_handler.coffee +++ b/content_scripts/mode_key_handler.coffee @@ -34,11 +34,7 @@ class KeyHandlerMode extends Mode blur: (event) => @alwaysContinueBubbling => @keydownEvents = {} if event.target == window @keyTranslationRegistry = {} - chrome.storage.local.get "keyTranslationRegistry", (obj) => - @keyTranslationRegistry = obj.keyTranslationRegistry - chrome.storage.onChanged.addListener (changes, area) => - if area == "local" and changes.keyTranslationRegistry?.newValue? - @keyTranslationRegistry = changes.keyTranslationRegistry.newValue + Utils.monitorChromeStorage "keyTranslationRegistry", (value) => @keyTranslationRegistry = value onKeydown: (event) -> keyChar = KeyboardUtils.getKeyCharString event -- cgit v1.2.3 From c5e26d75cb8525ba34f6f54f1a1c041df66075e3 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Mon, 10 Oct 2016 13:27:32 +0100 Subject: Rename 'translate' to 'mapkey'. --- content_scripts/mode_key_handler.coffee | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/mode_key_handler.coffee b/content_scripts/mode_key_handler.coffee index 9f8206fb..480a79af 100644 --- a/content_scripts/mode_key_handler.coffee +++ b/content_scripts/mode_key_handler.coffee @@ -33,12 +33,12 @@ class KeyHandlerMode extends Mode # We cannot track keyup events if we lose the focus. blur: (event) => @alwaysContinueBubbling => @keydownEvents = {} if event.target == window - @keyTranslationRegistry = {} - Utils.monitorChromeStorage "keyTranslationRegistry", (value) => @keyTranslationRegistry = value + @mapKeyRegistry = {} + Utils.monitorChromeStorage "mapKeyRegistry", (value) => @mapKeyRegistry = value onKeydown: (event) -> keyChar = KeyboardUtils.getKeyCharString event - keyChar = @keyTranslationRegistry[keyChar] ? keyChar + keyChar = @mapKeyRegistry[keyChar] ? keyChar isEscape = KeyboardUtils.isEscape event if isEscape and (@countPrefix != 0 or @keyState.length != 1) @keydownEvents[event.keyCode] = true @@ -65,7 +65,7 @@ class KeyHandlerMode extends Mode onKeypress: (event) -> keyChar = KeyboardUtils.getKeyCharString event - keyChar = @keyTranslationRegistry[keyChar] ? keyChar + keyChar = @mapKeyRegistry[keyChar] ? keyChar if @isMappedKey keyChar @handleKeyChar keyChar else if @isCountKey keyChar -- cgit v1.2.3