我想让我的 Electron 应用程序切换开发人员工具以响应 F12。
在渲染器页面中,我添加了:
const currentWebContents = require("electron").remote.getCurrentWebContents();
window.addEventListener("keydown", (e: KeyboardEvent) => {
if (e.keyCode === 123) { // F12
currentWebContents.toggleDevTools();
}
});
This works when I'm focused on the main page. However, immediately after the dev tools opens up, focus goes to the dev tools, so F12 is no longer detected.
我尝试通过在调用后立即向 devtools webcontents 添加监听器来解决此问题toggleDevTools()
像这样:
if (currentWebContents.devToolsWebContents) {
currentWebContents.devToolsWebContents.on("before-input-event", (event: Electron.Event, input: Electron.Input) => {
if (input.type === "keyDown" && input.key === "F12") {
currentWebContents.toggleDevTools();
}
});
}
然而,currentWebContents.devToolsWebContents
is null
打开后。我的第一个问题是如何确保它不是null
- 有没有办法等到它完全打开?
我通过将if (currentWebContents.devToolsWebContents)
代码写入一个setTimeout(..., 1000);
然而,在这样做之后,我的before-input-event
当专注于开发工具时按下按键时,处理程序不会被触发。
有人知道这是为什么吗?