diff options
| author | Phil Crosby | 2012-06-11 17:19:58 -0700 | 
|---|---|---|
| committer | Phil Crosby | 2012-06-12 22:00:35 -0700 | 
| commit | 50a4fd9ab8c95c653370ed01ab35cde62d6bf058 (patch) | |
| tree | 6e20e41c18703c0df3c654728d7b46597a90ca72 /lib | |
| parent | f27180aefbc90c3bb3847faf68e58b440768e713 (diff) | |
| download | vimium-50a4fd9ab8c95c653370ed01ab35cde62d6bf058.tar.bz2 | |
Port keyboard_utils.js to coffeescript
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/keyboard_utils.coffee | 62 | ||||
| -rw-r--r-- | lib/keyboard_utils.js | 66 | 
2 files changed, 62 insertions, 66 deletions
| diff --git a/lib/keyboard_utils.coffee b/lib/keyboard_utils.coffee new file mode 100644 index 00000000..df5bbbad --- /dev/null +++ b/lib/keyboard_utils.coffee @@ -0,0 +1,62 @@ +KeyboardUtils = +  keyCodes: +    { ESC: 27, backspace: 8, deleteKey: 46, enter: 13, space: 32, shiftKey: 16, f1: 112, f12: 123, tab: 9 } + +  keyNames: +    { 37: "left", 38: "up", 39: "right", 40: "down" } + +  # 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 +  # these keys on Windows. See https://bugs.webkit.org/show_bug.cgi?id=19906 for more details. +  keyIdentifierCorrectionMap: +    "U+00C0": ["U+0060", "U+007E"] # `~ +    "U+00BD": ["U+002D", "U+005F"] # -_ +    "U+00BB": ["U+003D", "U+002B"] # =+ +    "U+00DB": ["U+005B", "U+007B"] # [{ +    "U+00DD": ["U+005D", "U+007D"] # ]} +    "U+00DC": ["U+005C", "U+007C"] # \| +    "U+00BA": ["U+003B", "U+003A"] # ;: +    "U+00DE": ["U+0027", "U+0022"] # '" +    "U+00BC": ["U+002C", "U+003C"] # ,< +    "U+00BE": ["U+002E", "U+003E"] # .> +    "U+00BF": ["U+002F", "U+003F"] # /? + +  init: -> +    if (navigator.userAgent.indexOf("Mac") != -1) +      @platform = "Mac" +    else if (navigator.userAgent.indexOf("Linux") != -1) +      @platform = "Linux" +    else +      @platform = "Windows" + +  getKeyChar: (event) -> +    # Not a letter +    if (event.keyIdentifier.slice(0, 2) != "U+") +      return @keyNames[event.keyCode] if (@keyNames[event.keyCode]) +      # F-key +      if (event.keyCode >= @keyCodes.f1 && event.keyCode <= @keyCodes.f12) +        return "f" + (1 + event.keyCode - keyCodes.f1) +      return "" + +    keyIdentifier = event.keyIdentifier +    # On Windows, the keyIdentifiers for non-letter keys are incorrect. See +    # https://bugs.webkit.org/show_bug.cgi?id=19906 for more details. +    if ((@platform == "Windows" || @platform == "Linux") && @keyIdentifierCorrectionMap[keyIdentifier]) +      correctedIdentifiers = @keyIdentifierCorrectionMap[keyIdentifier] +      keyIdentifier = if event.shiftKey then correctedIdentifiers[1] else correctedIdentifiers[0] +    unicodeKeyInHex = "0x" + keyIdentifier.substring(2) +    character = String.fromCharCode(parseInt(unicodeKeyInHex)).toLowerCase() +    if event.shiftKey then character.toUpperCase() else character + +  isPrimaryModifierKey: (event) -> if (@platform == "Mac") then event.metaKey else event.ctrlKey + +  isEscape: (event) -> +    # c-[ is mapped to ESC in Vim by default. +    (event.keyCode == @keyCodes.ESC) || (event.ctrlKey && @getKeyChar(event) == '[') + +KeyboardUtils.init() + +root = exports ? window +root.KeyboardUtils = KeyboardUtils +# TODO(philc): A lot of code uses this keyCodes hash... maybe we shouldn't export it as a global. +root.keyCodes = KeyboardUtils.keyCodes diff --git a/lib/keyboard_utils.js b/lib/keyboard_utils.js deleted file mode 100644 index 52544c73..00000000 --- a/lib/keyboard_utils.js +++ /dev/null @@ -1,66 +0,0 @@ -var keyCodes = { -  ESC: 27, backspace: 8, deleteKey: 46, enter: 13, space: 32, shiftKey: 16, f1: 112, f12: 123, tab: 9 -}; -var keyNames = { 37: "left", 38: "up", 39: "right", 40: "down" } - -// 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 -// these keys on Windows. See https://bugs.webkit.org/show_bug.cgi?id=19906 for more details. -var keyIdentifierCorrectionMap = { -  "U+00C0": ["U+0060", "U+007E"], // `~ -  "U+00BD": ["U+002D", "U+005F"], // -_ -  "U+00BB": ["U+003D", "U+002B"], // =+ -  "U+00DB": ["U+005B", "U+007B"], // [{ -  "U+00DD": ["U+005D", "U+007D"], // ]} -  "U+00DC": ["U+005C", "U+007C"], // \| -  "U+00BA": ["U+003B", "U+003A"], // ;: -  "U+00DE": ["U+0027", "U+0022"], // '" -  "U+00BC": ["U+002C", "U+003C"], // ,< -  "U+00BE": ["U+002E", "U+003E"], // .> -  "U+00BF": ["U+002F", "U+003F"] // /? -}; - -var platform; -if (navigator.userAgent.indexOf("Mac") != -1) -  platform = "Mac"; -else if (navigator.userAgent.indexOf("Linux") != -1) -  platform = "Linux"; -else -  platform = "Windows"; - -function getKeyChar(event) { -    // Not a letter -    if (event.keyIdentifier.slice(0, 2) != "U+") { -        // Named key -        if (keyNames[event.keyCode]) { -          return keyNames[event.keyCode]; -        } -        // F-key -        if (event.keyCode >= keyCodes.f1 && event.keyCode <= keyCodes.f12) { -          return "f" + (1 + event.keyCode - keyCodes.f1); -        } -        return ""; -    } -    var keyIdentifier = event.keyIdentifier; -    // On Windows, the keyIdentifiers for non-letter keys are incorrect. See -    // https://bugs.webkit.org/show_bug.cgi?id=19906 for more details. -    if ((platform == "Windows" || platform == "Linux") && keyIdentifierCorrectionMap[keyIdentifier]) { -      correctedIdentifiers = keyIdentifierCorrectionMap[keyIdentifier]; -      keyIdentifier = event.shiftKey ? correctedIdentifiers[1] : correctedIdentifiers[0]; -    } -    var unicodeKeyInHex = "0x" + keyIdentifier.substring(2); -    var character = String.fromCharCode(parseInt(unicodeKeyInHex)).toLowerCase(); -    return event.shiftKey ? character.toUpperCase() : character; -} - -function isPrimaryModifierKey(event) { -  if (platform == "Mac") -    return event.metaKey; -  else -    return event.ctrlKey; -} - -function isEscape(event) { -  return event.keyCode == keyCodes.ESC || -    (event.ctrlKey && getKeyChar(event) == '['); // c-[ is mapped to ESC in Vim by default. -} | 
