diff options
| author | Stephen Blott | 2016-05-14 07:35:31 +0100 |
|---|---|---|
| committer | Stephen Blott | 2016-05-15 07:10:26 +0100 |
| commit | a5262f4e68f62a922c9c05d871c4a874f6737a7b (patch) | |
| tree | 9a1bd8927b8d5bed212891839c49a5899789690c | |
| parent | a1c00c38abee530f84572c36c7422e4ee793316b (diff) | |
| download | vimium-a5262f4e68f62a922c9c05d871c4a874f6737a7b.tar.bz2 | |
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.
| -rw-r--r-- | lib/keyboard_utils.coffee | 22 |
1 files changed, 21 insertions, 1 deletions
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]) |
