Skip to content

Commit a02c4d2

Browse files
committed
run alt shortcut on keyup
#631 part 1
1 parent 9992148 commit a02c4d2

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

js/keybindings.js

+34-32
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ var menuBarShortcuts = ['mod+t', 'shift+mod+p', 'mod+n'] // shortcuts that are a
154154

155155
var shortcutsList = []
156156

157-
function defineShortcut (keysOrKeyMapName, fn) {
157+
function defineShortcut (keysOrKeyMapName, fn, options = {}) {
158158
if (keysOrKeyMapName.keys) {
159159
var binding = keysOrKeyMapName.keys
160160
} else {
@@ -193,11 +193,12 @@ function defineShortcut (keysOrKeyMapName, fn) {
193193
shortcutsList.push({
194194
combo: keys,
195195
keys: keys.split('+'),
196-
fn: shortcutCallback
196+
fn: shortcutCallback,
197+
keyUp: options.keyUp
197198
})
198199
})
199200

200-
Mousetrap.bind(binding, shortcutCallback)
201+
Mousetrap.bind(binding, shortcutCallback, (options.keyUp ? "keyup" : null))
201202
}
202203

203204
settings.get('keyMap', function (keyMapSettings) {
@@ -425,7 +426,7 @@ settings.get('keyMap', function (keyMapSettings) {
425426

426427
defineShortcut('showAndHideMenuBar', function () {
427428
menuBarVisibility.toggleMenuBar()
428-
})
429+
}, {keyUp: true}) //run on keyUp to avoid interfering with alt+f4 shortcut, see https://github.com/minbrowser/min/issues/631
429430

430431
defineShortcut('followLink', function () {
431432
findinpage.end({ action: 'activateSelection' })
@@ -457,34 +458,35 @@ webviews.bindEvent('before-input-event', function (e, input) {
457458
expectedKeys++
458459
}
459460

460-
if (input.type === 'keyDown') {
461-
shortcutsList.forEach(function (shortcut) {
462-
var matches = true
463-
var matchedKeys = 0
464-
shortcut.keys.forEach(function (key) {
465-
if (! (
466-
key === input.key.toLowerCase() ||
467-
key === input.code.replace('Digit', '') ||
468-
(key === 'left' && input.key === 'ArrowLeft') ||
469-
(key === 'right' && input.key === 'ArrowRight') ||
470-
(key === 'up' && input.key === 'ArrowUp') ||
471-
(key === 'down' && input.key === 'ArrowDown') ||
472-
(key === 'alt' && (input.alt || input.key === "Alt")) ||
473-
(key === 'shift' && (input.shift || input.key === "Shift")) ||
474-
(key === 'ctrl' && (input.control || input.key === "Control")) ||
475-
(key === 'mod' && window.platformType === 'mac' && (input.meta || input.key === "Meta")) ||
476-
(key === 'mod' && window.platformType !== 'mac' && (input.control || input.key === "Control"))
477-
)
478-
) {
479-
matches = false
480-
} else {
481-
matchedKeys++
482-
}
483-
})
484-
485-
if (matches && matchedKeys === expectedKeys) {
486-
shortcut.fn(null, shortcut.combo)
461+
shortcutsList.forEach(function (shortcut) {
462+
if ((shortcut.keyUp && input.type !== "keyUp") || (!shortcut.keyUp && input.type !== "keyDown")) {
463+
return
464+
}
465+
var matches = true
466+
var matchedKeys = 0
467+
shortcut.keys.forEach(function (key) {
468+
if (! (
469+
key === input.key.toLowerCase() ||
470+
key === input.code.replace('Digit', '') ||
471+
(key === 'left' && input.key === 'ArrowLeft') ||
472+
(key === 'right' && input.key === 'ArrowRight') ||
473+
(key === 'up' && input.key === 'ArrowUp') ||
474+
(key === 'down' && input.key === 'ArrowDown') ||
475+
(key === 'alt' && (input.alt || input.key === "Alt")) ||
476+
(key === 'shift' && (input.shift || input.key === "Shift")) ||
477+
(key === 'ctrl' && (input.control || input.key === "Control")) ||
478+
(key === 'mod' && window.platformType === 'mac' && (input.meta || input.key === "Meta")) ||
479+
(key === 'mod' && window.platformType !== 'mac' && (input.control || input.key === "Control"))
480+
)
481+
) {
482+
matches = false
483+
} else {
484+
matchedKeys++
487485
}
488486
})
489-
}
487+
488+
if (matches && matchedKeys === expectedKeys) {
489+
shortcut.fn(null, shortcut.combo)
490+
}
491+
})
490492
})

0 commit comments

Comments
 (0)