在 Atom-shell 中禁用退格键

Disable backspace in Atom-shell(在 Atom-shell 中禁用退格键)
本文介绍了在 Atom-shell 中禁用退格键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I've been scouring the interwebz and Atom-shell documentation trying to find out how to disable the back() functionality of the backspace key within a browser window.

我宁愿不必求助于 javascript onkeydown 侦听器(它可以工作),而是使用更原生的东西,在更多的应用程序级别而不是浏览器窗口级别.

I would prefer not to have to resort to a javascript onkeydown listener (which works) and rather use something more native and at more of the application level instead of the browser window level.

推荐答案

我想出在没有 onkeydown 监听器的情况下做到这一点的唯一方法是使用全局快捷方式和 ipc 事件电子接口.

The only way I have figured out to do this without the onkeydown listener is with a global-shortcut and the ipc events in the Electron api.

首先声明...

使用全局快捷方式禁用任何键确实会在您的计算机上全局禁用它!使用全球快捷方式时请小心!如果您忘记取消注册您的快捷方式,或者没有正确处理它,您会发现如果没有退格,您将很难修复您的错误!

Disabling any key with a global shortcut really does disable it GLOBALLY on your computer! PLEASE BE CAREFUL WHEN USING GLOBAL SHORTCUTS! If you forget to unregister your shortcut, or do not handle it properly, you will find it difficult to fix your mistake without backspace!

也就是说这对我有用...

That said this is what worked for me...

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.
    });
});

或者,您可以像这样在 ipcToggle"事件处理程序中添加快捷方式...

Alternatively you could add the shortcut inside an ipc "Toggle" event handler like this...

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

这篇关于在 Atom-shell 中禁用退格键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)