Skip to content

Commit 359693a

Browse files
committed
Support compound keys (<a-f>, <c-[>, etc.) in passkeys
1 parent b38f011 commit 359693a

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

content_scripts/vimium_frontend.coffee

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@ keyPort = null
1919
# are passed through to the underlying page.
2020
isEnabledForUrl = true
2121
passKeys = null
22+
splitPassKeys = []
2223
keyQueue = null
2324
# The user's operating system.
2425
currentCompletionKeys = null
2526
validFirstKeys = null
2627

28+
# Keys are either literal characters, or "named" - for example <a-b> (alt+b), <left> (left arrow) or <f12>
29+
# This regular expression captures two groups: the first is a named key, the second is the remainder of
30+
# the string.
31+
namedKeyRegex = /^(<(?:[amc]-.|(?:[amc]-)?[a-z0-9]{2,5})>)$/
32+
2733
# The types in <input type="..."> that we consider for focusInput command. Right now this is recalculated in
2834
# each content script. Alternatively we could calculate it once in the background page and use a request to
2935
# fetch it each time.
@@ -149,7 +155,7 @@ installListener = (element, event, callback) ->
149155
installedListeners = false
150156
initializeWhenEnabled = (newPassKeys) ->
151157
isEnabledForUrl = true
152-
passKeys = newPassKeys
158+
setPassKeys newPassKeys
153159
if (!installedListeners)
154160
# Key event handlers fire on window before they do on document. Prefer window for key events so the page
155161
# can't set handlers to grab the keys before us.
@@ -165,7 +171,7 @@ initializeWhenEnabled = (newPassKeys) ->
165171
setState = (request) ->
166172
initializeWhenEnabled(request.passKeys) if request.enabled
167173
isEnabledForUrl = request.enabled
168-
passKeys = request.passKeys
174+
setPassKeys request.passKeys
169175

170176
#
171177
# The backend needs to know which frame has focus.
@@ -345,8 +351,19 @@ extend window,
345351
# Decide whether this keyChar should be passed to the underlying page.
346352
# Keystrokes are *never* considered passKeys if the keyQueue is not empty. So, for example, if 't' is a
347353
# passKey, then 'gt' and '99t' will neverthless be handled by vimium.
348-
isPassKey = ( keyChar ) ->
349-
return !keyQueue and passKeys and 0 <= passKeys.indexOf(keyChar)
354+
isPassKey = (keyChar) ->
355+
not keyQueue and splitPassKeys and 0 <= splitPassKeys.indexOf(keyChar)
356+
357+
setPassKeys = (newPassKeys) ->
358+
passKeys = newPassKeys
359+
passKeyGroups = passKeys.split " "
360+
splitPassKeys = []
361+
for passKeyGroup in passKeyGroups
362+
namedKeyMatch = passKeyGroup.match namedKeyRegex
363+
if namedKeyMatch # The current space delimited group is a single named key.
364+
splitPassKeys.push passKeyGroup # Push this to as a single key
365+
else
366+
splitPassKeys = splitPassKeys.concat passKeyGroup.split "" # Pass all the characters as seperate keys.
350367

351368
# Track which keydown events we have handled, so that we can subsequently suppress the corresponding keyup
352369
# event.
@@ -473,6 +490,8 @@ onKeydown = (event) ->
473490

474491
else if (!isInsertMode() && !findMode)
475492
if (keyChar)
493+
if isPassKey keyChar
494+
return undefined
476495
if (currentCompletionKeys.indexOf(keyChar) != -1 or isValidFirstKey(keyChar))
477496
DomUtils.suppressEvent event
478497
KeydownEvents.push event

pages/options.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@
253253
<br/><br/>
254254
If "Keys" is left empty, then vimium is wholly disabled.
255255
Otherwise, just the listed keys are disabled (they are passed through).
256+
<br/><br/>
257+
To enter complex keys such as
258+
<pre>&lt;c-[&gt;, &lt;a-f&gt;, &lt;c-a-A&gt;</pre>
259+
simply seperate them from other passkeys with a space.
256260
</div>
257261
</div>
258262
<div>

0 commit comments

Comments
 (0)