-
Notifications
You must be signed in to change notification settings - Fork 38
/
example.js
176 lines (162 loc) · 3.55 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'use strict';
const {BrowserWindow, app, Menu} = require('electron');
const electronLocalshortcut = require('.');
app.on('ready', () => {
const win = new BrowserWindow({});
const win2 = new BrowserWindow({});
// Should raise a warning in console
try {
electronLocalshortcut.register(win, 'C+C', () => {});
// eslint-disable-next-line no-unused-vars
} catch (error) {
}
electronLocalshortcut.register(win, 'CmdOrCtrl+Z', () => {
process.stdout.write('A\n');
});
electronLocalshortcut.register(win, 'Ctrl+B', () => {
process.stdout.write('B\n');
});
electronLocalshortcut.register(win2, 'Ctrl+A', () => {
process.stdout.write('A2\n');
});
electronLocalshortcut.register(win2, 'Ctrl+B', () => {
process.stdout.write('B2\n');
});
electronLocalshortcut.register(win2, 'Ctrl+C', () => {
process.stdout.write('C2\n');
});
electronLocalshortcut.register(win2, ['Ctrl+R', 'F5'], () => {
process.stdout.write('Refresh\n');
});
const template = [
{
label: 'test',
submenu: [
{
label: 'Unregister all shortcuts',
click() {
electronLocalshortcut.unregisterAll(win);
}
}
]
}
];
win.setMenu(Menu.buildFromTemplate(template));
win.loadURL('about://blank');
win.show();
const template2 = [
{
label: 'test',
submenu: [
{
label: 'Unregister C2',
click() {
electronLocalshortcut.unregister(win2, 'Ctrl+C');
}
},
{
label: 'Register unvalid shortcut',
click() {
electronLocalshortcut.unregister(win2, 'Ctrl+Alt');
}
},
{
label: 'Register C2',
click() {
electronLocalshortcut.register(win2, 'Ctrl+C', () => {
process.stdout.write('C2\n');
});
}
},
{
label: 'Disable shortcuts',
click() {
electronLocalshortcut.disableAll(win2);
}
},
{
label: 'Enable shortcuts',
click() {
electronLocalshortcut.enableAll(win2);
}
},
{
label: 'Check C2',
click() {
const isRegistered = electronLocalshortcut.isRegistered(
win2,
'Ctrl+C'
);
process.stdout.write(`${isRegistered}\n`);
}
},
{
label: 'Check existing global shortcut',
click() {
electronLocalshortcut.register(win2, 'Ctrl+E', () => {
console.log('Control E !');
});
}
},
{
label: 'Check Space',
click() {
electronLocalshortcut.register(win2, 'Ctrl+Space', () => {
console.log('Ctrl+Space!');
});
}
},
{
label: 'Cmd+S & Ctrl+S',
click() {
electronLocalshortcut.register(win2, ['Alt+S', 'Ctrl+S'], () => {
console.log('use array');
});
}
}
]
},
{
label: 'test all window',
submenu: [
{
label: 'Unregister ALL',
click() {
electronLocalshortcut.unregister('Alt+A');
}
},
{
label: 'Unregister any ALL',
click() {
electronLocalshortcut.unregisterAll();
}
},
{
label: 'Register ALL',
click() {
electronLocalshortcut.register('Alt+A', () => {
process.stdout.write('ALL\n');
});
}
},
{
label: 'Check ALL',
click() {
const isRegistered = electronLocalshortcut.isRegistered('Alt+A');
process.stdout.write(`${isRegistered}\n`);
}
}
]
}
];
win2.setMenu(Menu.buildFromTemplate(template2));
win2.loadURL('about://blank');
win2.show();
win2.openDevTools();
win2.webContents.executeJavaScript(`
document.body.innerHTML = '<textarea></textarea>';
document.addEventListener('keydown', e => {
console.log(e);
});
`);
});