gpt4 book ai didi

javascript - 在 Atom-shell 中禁用退格键

转载 作者:搜寻专家 更新时间:2023-11-01 04:31:21 26 4
gpt4 key购买 nike

我一直在搜索 interwebz 和 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“切换”事件处理程序中添加快捷方式...

// 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');

关于javascript - 在 Atom-shell 中禁用退格键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27829839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com