1
1
/*
2
2
There are three possible ways that keybindings can be handled.
3
3
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 .
6
6
*/
7
7
8
- const Mousetrap = require ( 'mousetrap' )
9
8
const keyMapModule = require ( 'util/keyMap.js' )
10
9
11
10
var webviews = require ( 'webviews.js' )
@@ -15,7 +14,6 @@ var settings = require('util/settings/settings.js')
15
14
var keyMap = keyMapModule . userKeyMap ( settings . get ( 'keyMap' ) )
16
15
17
16
var shortcutsList = [ ]
18
- var registeredMousetrapBindings = { }
19
17
20
18
/*
21
19
Determines whether a shortcut can actually run
@@ -96,18 +94,6 @@ function defineShortcut (keysOrKeyMapName, fn, options = {}) {
96
94
fn : shortcutCallback ,
97
95
keyUp : options . keyUp || false
98
96
} )
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
- }
111
97
} )
112
98
}
113
99
@@ -117,58 +103,67 @@ navigator.keyboard.getLayoutMap().then(map => {
117
103
keyboardMap = map
118
104
} )
119
105
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
132
125
}
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 )
135
156
}
157
+ } )
158
+ }
136
159
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
+ } )
167
164
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 )
172
167
} )
173
168
}
174
169
0 commit comments