我一直在搜索interwebz和Atom-shell文档,试图找出如何在浏览器窗口中禁用back()
键的backspace
功能。
我宁愿不必使用javascript onkeydown
侦听器(可以正常工作),而是使用更本地的,更多的应用程序级别而不是浏览器窗口级别。
答案 0 :(得分:2)
我在没有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');