【问题标题】:Disable backspace in Atom-shell在 Atom-shell 中禁用退格键
【发布时间】:2015-03-05 23:03:07
【问题描述】:

我一直在搜索 interwebz 和 Atom-shell 文档,试图找出如何在浏览器窗口中禁用 backspace 键的 back() 功能。

我宁愿不必求助于 javascript onkeydown 侦听器(可行),而是使用更本机的东西,在更多的应用程序级别而不是浏览器窗口级别。

【问题讨论】:

    标签: javascript electron


    【解决方案1】:

    我想出在没有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'); 
    

    【讨论】:

    • 为什么要阻止整个应用程序的退格键?您可以使用“普通”javascript 在您的前端/渲染器中阻止它,描述为here on stackoverflow(或该答案下方的类似答案)。 :) 我知道主题开始者要求使用 nodejs 方式......仍然不明白为什么 - 人们应该记住这种方式......(:
    • 我同意,这通常是最简单的方法。但是,这个问题询问了如何使用 Electron 和 not Javascript 来做到这一点。我想一个更适用的用例是:如果你打开一个窗口,一个不同的应用程序或一个系统对话框,你想继续拦截(而不是阻止)按键。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 2015-05-24
    • 2015-05-29
    • 2020-08-10
    • 1970-01-01
    • 2017-02-20
    相关资源
    最近更新 更多