aboutsummaryrefslogtreecommitdiffstats
path: root/lib/keyboard_utils.coffee
diff options
context:
space:
mode:
authorStephen Blott2016-02-10 13:27:45 +0000
committerStephen Blott2016-02-18 07:35:07 +0000
commitaaccc059b2cbf47ee4d5290f8cf608b0ab318d92 (patch)
tree6261c4f507f625cd8d49680abbb15d64830f29d8 /lib/keyboard_utils.coffee
parentdcc5427b4b3c4a9440675c8443b31dc98fb62fdd (diff)
downloadvimium-aaccc059b2cbf47ee4d5290f8cf608b0ab318d92.tar.bz2
PassNextKey; move key parsing to keyboard_utils.coffee.
Previously, key event parsing was embedded in the normal-mode key handlers. Here, we move it to a new function (getKeyCharString) in KeyboardUtils so that it can also be used from elsewhere... In particular for detecting the pass-next-key key in insertmode.
Diffstat (limited to 'lib/keyboard_utils.coffee')
-rw-r--r--lib/keyboard_utils.coffee29
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/keyboard_utils.coffee b/lib/keyboard_utils.coffee
index f123f75a..4c149b48 100644
--- a/lib/keyboard_utils.coffee
+++ b/lib/keyboard_utils.coffee
@@ -66,6 +66,35 @@ KeyboardUtils =
@getKeyChar event
keyChar.length == 1
+ # Return the Vimium key representation for this keyboard event. Return a falsy value (the empty string or
+ # undefined) when no Vimium representation is appropriate.
+ getKeyCharString: (event) ->
+ switch event.type
+ when "keypress"
+ # Ignore modifier keys by themselves.
+ if 31 < event.keyCode
+ 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 "-"
+ keyChar = "<#{keyChar}>" if 1 < keyChar.length
+ keyChar
+
KeyboardUtils.init()
root = exports ? window