我一直在搜索互联网和Atom-shell文档,试图找出如何禁用浏览器窗口中backspace
键的back()
功能。
我希望不必使用javascript的onkeydown
监听器(虽然可以工作),而是使用更本地且更应用程序级别的方法,而不是浏览器窗口级别的方法。
我一直在搜索互联网和Atom-shell文档,试图找出如何禁用浏览器窗口中backspace
键的back()
功能。
我希望不必使用javascript的onkeydown
监听器(虽然可以工作),而是使用更本地且更应用程序级别的方法,而不是浏览器窗口级别的方法。
onkeydown
监听器的方法,可以通过Electron API中的全局快捷键和ipc事件来实现。const { app, ipcMain,
globalShortcut,
BrowserWindow,
} = require('electron');
app.on('ready', () => {
// Create the browser window
let mainWindow = new BrowserWindow({width: 800, height: 600});
// and load the index.html of the app
mainWindow.loadUrl('file://' + __dirname + '/index.html');
// Register a 'Backspace' shortcut listener when focused on window
mainWindow.on('focus', () => {
if (mainWindow.isFocused()) {
globalShortcut.register('Backspace', () => {
// Provide feedback or logging here
// If you leave this section blank, you will get no
// response when you try the shortcut (i.e. Backspace).
console.log('Backspace was pressed!'); //comment-out or delete when ready.
});
});
});
// ** THE IMPORTANT PART **
// Unregister a 'Backspace' shortcut listener when leaving window.
mainWindow.on('blur', () => {
globalShortcut.unregister('Backspace');
console.log('Backspace is unregistered!'); //comment-out or delete when ready.
});
});
或者您可以像这样在ipc“Toggle”事件处理程序内添加快捷方式...
// In the main process
ipcMain.on('disableKey-toggle', (event, keyToDisable) => {
if (!globalShortcut.isRegistered(keyToDisable){
globalShortcut.register(keyToDisable, () => {
console.log(keyToDisable+' is registered!'); //comment-out or delete when ready.
});
} else {
globalShortcut.unregister(keyToDisable);
console.log(keyToDisable+' is unregistered!'); //comment-out or delete when ready.
}
});
// In the render process send the accelerator of the keyToDisable.
// Here we use the 'Backspace' accelerator.
const { ipcRenderer } = require('electron');
ipcRenderer.send('disableKey-toggle', 'Backspace');