Skip to content

Commit bff8e92

Browse files
authoredJan 4, 2025
Merge pull request #2543 from minbrowser/linux-super-key
Remove mousetrap; allow creating shortcuts containing the super key
2 parents 8fd5f36 + d1518db commit bff8e92

File tree

7 files changed

+70
-70
lines changed

7 files changed

+70
-70
lines changed
 

‎index.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
</div>
137137
<div id="tabs">
138138
<div id="tab-editor" hidden>
139-
<input id="tab-editor-input" class="mousetrap" spellcheck="false" />
139+
<input id="tab-editor-input" spellcheck="false" />
140140
</div>
141141
<div id="tabs-inner" role="tablist" class="has-thin-scrollbar"></div>
142142
</div>
@@ -216,7 +216,7 @@
216216
hidden
217217
class="theme-background-color theme-text-color"
218218
>
219-
<input id="findinpage-input" class="mousetrap" />
219+
<input id="findinpage-input" />
220220
<span id="findinpage-count" class="inline-text"></span>
221221
<div class="divider"></div>
222222
<button id="findinpage-previous-match">
@@ -238,7 +238,7 @@
238238
>
239239
<div class="task-search-input-container">
240240
<i class="i carbon:search"></i>
241-
<input type="search" id="task-search-input" class="mousetrap"/>
241+
<input type="search" id="task-search-input"/>
242242
</div>
243243
</div>
244244
<div id="task-area"></div>

‎js/keybindings.js

+59-64
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/*
22
There are three possible ways that keybindings can be handled.
33
Shortcuts that appear in the menubar are registered in main.js, and send IPC messages to the window (which are handled by menuRenderer.js)
4-
- If the browser UI is focused, shortcuts are handled by Mousetrap.
5-
- If a BrowserView is focused, shortcuts are handled by the before-input-event listener.
4+
- If the browser UI is focused, a before-input-event is generated in the main process and forwarded to here.
5+
- If a BrowserView is focused, a before-input-event is generated from the webContents and forwarded to here.
66
*/
77

8-
const Mousetrap = require('mousetrap')
98
const keyMapModule = require('util/keyMap.js')
109

1110
var webviews = require('webviews.js')
@@ -15,7 +14,6 @@ var settings = require('util/settings/settings.js')
1514
var keyMap = keyMapModule.userKeyMap(settings.get('keyMap'))
1615

1716
var shortcutsList = []
18-
var registeredMousetrapBindings = {}
1917

2018
/*
2119
Determines whether a shortcut can actually run
@@ -96,18 +94,6 @@ function defineShortcut (keysOrKeyMapName, fn, options = {}) {
9694
fn: shortcutCallback,
9795
keyUp: options.keyUp || false
9896
})
99-
if (!registeredMousetrapBindings[keys + (options.keyUp ? '-keyup' : '')]) {
100-
// mousetrap only allows one listener for each key combination (+keyup variant)
101-
// so register a single listener, and have it call all the other listeners that we have
102-
Mousetrap.bind(keys, function (e, combo) {
103-
shortcutsList.forEach(function (shortcut) {
104-
if (shortcut.combo === combo && (e.type === 'keyup') === shortcut.keyUp) {
105-
shortcut.fn(e, combo)
106-
}
107-
})
108-
}, (options.keyUp ? 'keyup' : null))
109-
registeredMousetrapBindings[keys + (options.keyUp ? '-keyup' : '')] = true
110-
}
11197
})
11298
}
11399

@@ -117,58 +103,67 @@ navigator.keyboard.getLayoutMap().then(map => {
117103
keyboardMap = map
118104
})
119105

120-
function initialize () {
121-
webviews.bindEvent('before-input-event', function (tabId, input) {
122-
var expectedKeys = 1
123-
// account for additional keys that aren't in the input.key property
124-
if (input.alt && input.key !== 'Alt') {
125-
expectedKeys++
126-
}
127-
if (input.shift && input.key !== 'Shift') {
128-
expectedKeys++
129-
}
130-
if (input.control && input.key !== 'Control') {
131-
expectedKeys++
106+
function beforeInputEventHandler (input) {
107+
var expectedKeys = 1
108+
// account for additional keys that aren't in the input.key property
109+
if (input.alt && input.key !== 'Alt') {
110+
expectedKeys++
111+
}
112+
if (input.shift && input.key !== 'Shift') {
113+
expectedKeys++
114+
}
115+
if (input.control && input.key !== 'Control') {
116+
expectedKeys++
117+
}
118+
if (input.meta && input.key !== 'Meta') {
119+
expectedKeys++
120+
}
121+
122+
shortcutsList.forEach(function (shortcut) {
123+
if ((shortcut.keyUp && input.type !== 'keyUp') || (!shortcut.keyUp && input.type !== 'keyDown')) {
124+
return
132125
}
133-
if (input.meta && input.key !== 'Meta') {
134-
expectedKeys++
126+
var matches = true
127+
var matchedKeys = 0
128+
shortcut.keys.forEach(function (key) {
129+
if (!(
130+
key === input.key.toLowerCase() ||
131+
// we need this check because the alt key can change the typed key, causing input.key to be a special character instead of the base key
132+
// but input.code isn't layout aware, so we need to map it to the correct key for the layout
133+
(keyboardMap && key === keyboardMap.get(input.code)) ||
134+
(key === 'esc' && input.key === 'Escape') ||
135+
(key === 'left' && input.key === 'ArrowLeft') ||
136+
(key === 'right' && input.key === 'ArrowRight') ||
137+
(key === 'up' && input.key === 'ArrowUp') ||
138+
(key === 'down' && input.key === 'ArrowDown') ||
139+
(key === 'alt' && (input.alt || input.key === 'Alt')) ||
140+
(key === 'option' && (input.alt || input.key === 'Alt')) ||
141+
(key === 'shift' && (input.shift || input.key === 'Shift')) ||
142+
(key === 'ctrl' && (input.control || input.key === 'Control')) ||
143+
(key === 'mod' && window.platformType === 'mac' && (input.meta || input.key === 'Meta')) ||
144+
(key === 'mod' && window.platformType !== 'mac' && (input.control || input.key === 'Control')) ||
145+
(key === 'super' && (input.meta || input.key === 'Meta'))
146+
)
147+
) {
148+
matches = false
149+
} else {
150+
matchedKeys++
151+
}
152+
})
153+
154+
if (matches && matchedKeys === expectedKeys) {
155+
shortcut.fn(null, shortcut.combo)
135156
}
157+
})
158+
}
136159

137-
shortcutsList.forEach(function (shortcut) {
138-
if ((shortcut.keyUp && input.type !== 'keyUp') || (!shortcut.keyUp && input.type !== 'keyDown')) {
139-
return
140-
}
141-
var matches = true
142-
var matchedKeys = 0
143-
shortcut.keys.forEach(function (key) {
144-
if (!(
145-
key === input.key.toLowerCase() ||
146-
// we need this check because the alt key can change the typed key, causing input.key to be a special character instead of the base key
147-
// but input.code isn't layout aware, so we need to map it to the correct key for the layout
148-
(keyboardMap && key === keyboardMap.get(input.code)) ||
149-
(key === 'esc' && input.key === 'Escape') ||
150-
(key === 'left' && input.key === 'ArrowLeft') ||
151-
(key === 'right' && input.key === 'ArrowRight') ||
152-
(key === 'up' && input.key === 'ArrowUp') ||
153-
(key === 'down' && input.key === 'ArrowDown') ||
154-
(key === 'alt' && (input.alt || input.key === 'Alt')) ||
155-
(key === 'option' && (input.alt || input.key === 'Alt')) ||
156-
(key === 'shift' && (input.shift || input.key === 'Shift')) ||
157-
(key === 'ctrl' && (input.control || input.key === 'Control')) ||
158-
(key === 'mod' && window.platformType === 'mac' && (input.meta || input.key === 'Meta')) ||
159-
(key === 'mod' && window.platformType !== 'mac' && (input.control || input.key === 'Control'))
160-
)
161-
) {
162-
matches = false
163-
} else {
164-
matchedKeys++
165-
}
166-
})
160+
function initialize () {
161+
webviews.bindEvent('before-input-event', function (tabId, input) {
162+
beforeInputEventHandler(input)
163+
})
167164

168-
if (matches && matchedKeys === expectedKeys) {
169-
shortcut.fn(null, shortcut.combo)
170-
}
171-
})
165+
ipc.on('before-input-event', function (e, input) {
166+
beforeInputEventHandler(input)
172167
})
173168
}
174169

‎js/searchbar/bookmarkEditor.js

-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ const bookmarkEditor = {
177177
var newTagInput = document.createElement('input')
178178
newTagInput.className = 'tag-input'
179179
newTagInput.placeholder = l('bookmarksAddTag')
180-
newTagInput.classList.add('mousetrap')
181180
newTagInput.spellcheck = false
182181
tagArea.appendChild(newTagInput)
183182

‎js/taskOverlay/taskOverlayBuilder.js

-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ var TaskOverlayBuilder = {
6464
nameInputField: function (task, taskIndex) {
6565
var input = document.createElement('input')
6666
input.classList.add('task-name')
67-
input.classList.add('mousetrap')
6867

6968
var taskName = l('defaultTaskName').replace('%n', taskIndex + 1)
7069

‎main/main.js

+4
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ function createWindowWithBounds (bounds, customArgs) {
327327
}
328328
})
329329

330+
newWin.webContents.on('before-input-event', function(e, input) {
331+
sendIPCToWindow(newWin, 'before-input-event', input)
332+
})
333+
330334
newWin.setTouchBar(buildTouchBar())
331335

332336
windows.addWindow(newWin)

‎package.json

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"dragula": "github:minbrowser/dragula",
3636
"electron-squirrel-startup": "^1.0.0",
3737
"expr-eval": "^2.0.2",
38-
"mousetrap": "^1.5.3",
3938
"node-abi": "^3.8.0",
4039
"pdfjs-dist": "4.2.67",
4140
"quick-score": "^0.2.0",

‎pages/settings/settings.js

+4
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,9 @@ function formatKeyValue (value) {
493493
value = value.replace(/\bmod\b/g, 'ctrl')
494494
value = value.replace(/\boption\b/g, 'alt')
495495
}
496+
if (navigator.platform === 'Win32') {
497+
value = value.replace(/\bsuper\b/g, 'win')
498+
}
496499
return value
497500
}
498501

@@ -509,6 +512,7 @@ function parseKeyInput (input) {
509512
} else {
510513
e = e.replace(/\b(control)|(ctrl)\b/g, 'mod')
511514
e = e.replace(/\balt\b/g, 'option')
515+
e = e.replace(/\bwin\b/g, 'super')
512516
}
513517
return e
514518
})

0 commit comments

Comments
 (0)