From a5262f4e68f62a922c9c05d871c4a874f6737a7b Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 14 May 2016 07:35:31 +0100 Subject: Use event.key (not event.keyIdentifier). event.keyIdentifier is depricated and will be removed soon. It is being replaced by event.key. Unfortunatelty, event.key is not yet available in the stable Chrome version. Here, we use whichever API is available. In due course, we can remove the event.keyIdentifier implementation (and a considerable amount of machinery surrounding it). For the time being, if both APIs are available, then we verify one against the other and show a warning message of they do not match. This should help us track down any issues which arise. Using event.key has the additional benefit of correctly detecting shifted characters on the numbers row on keydown, which has been a problem for some users. Fixes #2128. Note: We have a problem with the tests. phantomjs does not currently support event.key, and possibly never will. --- lib/keyboard_utils.coffee | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/keyboard_utils.coffee b/lib/keyboard_utils.coffee index 10b5f46e..e3fa6007 100644 --- a/lib/keyboard_utils.coffee +++ b/lib/keyboard_utils.coffee @@ -4,7 +4,7 @@ KeyboardUtils = f12: 123, tab: 9, downArrow: 40, upArrow: 38 } keyNames: - { 37: "left", 38: "up", 39: "right", 40: "down" } + { 37: "left", 38: "up", 39: "right", 40: "down", 32: "space" } # This is a mapping of the incorrect keyIdentifiers generated by Webkit on Windows during keydown events to # the correct identifiers, which are correctly generated on Mac. We require this mapping to properly handle @@ -30,7 +30,27 @@ KeyboardUtils = else @platform = "Windows" + # We are migrating from using event.keyIdentifier to using event.key. For some period of time, we must + # support both. This wrapper can be removed once Chrome 52 is considered too old to support. getKeyChar: (event) -> + if event.key? + @getKeyCharUsingKey event + else + @getKeyCharUsingKeyIdentifier event + + getKeyCharUsingKey: (event) -> + if event.keyCode of @keyNames + @keyNames[event.keyCode] + else if event.key.length == 1 + event.key + else if event.key.length == 2 and "F1" <= event.key <= "F9" + event.key.toLowerCase() # F1 to F9. + else if event.key.length == 3 and "F10" <= event.key <= "F12" + event.key.toLowerCase() # F10 to F12. + else + "" + + getKeyCharUsingKeyIdentifier: (event) -> # Not a letter if (event.keyIdentifier.slice(0, 2) != "U+") return @keyNames[event.keyCode] if (@keyNames[event.keyCode]) -- cgit v1.2.3 From 6f08521e946b39ca9254bbc94455641f90f33215 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 15 May 2016 07:58:33 +0100 Subject: Also remove event.keyIdentifier in getKeyCharString(). This was an oversight from a5262f4e68f62a922c9c05d871c4a874f6737a7b. --- lib/keyboard_utils.coffee | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/lib/keyboard_utils.coffee b/lib/keyboard_utils.coffee index e3fa6007..364ab949 100644 --- a/lib/keyboard_utils.coffee +++ b/lib/keyboard_utils.coffee @@ -96,23 +96,18 @@ KeyboardUtils = String.fromCharCode event.charCode when "keydown" - # handle special keys, and normal input keys with modifiers being pressed. don't handle shiftKey alone (to - # avoid / being interpreted as ? - if (((event.metaKey || event.ctrlKey || event.altKey) && event.keyCode > 31) || ( - # TODO(philc): some events don't have a keyidentifier. How is that possible? - event.keyIdentifier && event.keyIdentifier.slice(0, 2) != "U+")) - keyChar = @getKeyChar event - # Again, ignore just modifiers. Maybe this should replace the keyCode>31 condition. - if 0 < keyChar.length - modifiers = [] - - keyChar = keyChar.toUpperCase() if event.shiftKey - modifiers.push "m" if event.metaKey - modifiers.push "c" if event.ctrlKey - modifiers.push "a" if event.altKey - - keyChar = [modifiers..., keyChar].join "-" - if 1 < keyChar.length then "<#{keyChar}>" else keyChar + # Handle special keys and normal input keys with modifiers being pressed. + keyChar = @getKeyChar event + if 1 < keyChar.length or (keyChar.length == 1 and (event.metaKey or event.ctrlKey or event.altKey)) + modifiers = [] + + keyChar = keyChar.toUpperCase() if event.shiftKey + modifiers.push "m" if event.metaKey + modifiers.push "c" if event.ctrlKey + modifiers.push "a" if event.altKey + + keyChar = [modifiers..., keyChar].join "-" + if 1 < keyChar.length then "<#{keyChar}>" else keyChar KeyboardUtils.init() -- cgit v1.2.3 From eced401764bb679af6f34559861c2b1bc39c2752 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 15 May 2016 09:05:21 +0100 Subject: Handle in the same way as other special keys. --- background_scripts/commands.coffee | 1 - 1 file changed, 1 deletion(-) diff --git a/background_scripts/commands.coffee b/background_scripts/commands.coffee index 0905daee..c334cf9d 100644 --- a/background_scripts/commands.coffee +++ b/background_scripts/commands.coffee @@ -43,7 +43,6 @@ Commands = key.replace(/<[acm]-/ig, (match) -> match.toLowerCase()) .replace(/<([acm]-)?([a-zA-Z0-9]{2,5})>/g, (match, optionalPrefix, keyName) -> "<" + (if optionalPrefix then optionalPrefix else "") + keyName.toLowerCase() + ">") - .replace //ig, " " parseCustomKeyMappings: (customKeyMappings) -> for line in customKeyMappings.split "\n" -- cgit v1.2.3