当“ esc”出现时,退出全屏被按下(使用带电子的javascript) [英] Exit full screen when "esc" is pressed (using javascript with electron)
问题描述
我正在开发一个带有电子的应用程序,我希望用户能够在按 esc时退出全屏模式。我徒劳地尝试了不同的方法。
这是在全屏模式下在新浏览器窗口中启动应用程序并显示和HTML / CSS内容的代码:
I'm working on an app (with electron) and I would like users to be able to exit fullscreen mode when pressing "esc". I tried different methods, in vain. Here is the code who launch the app in a new browser window in fullscreen mode and display and HTML/CSS content :
'use strict';
const electron = require('electron');
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({fullscreen:true});
// and load the index.html of the app.
mainWindow.loadURL('file://' + __dirname + '/index.html');
// Emitted when the window is closed.
mainWindow.on('closed', function() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
这是我想添加到此代码中的内容,以便能够退出全屏显示,但是它不起作用:
And here is what I'm trying to add to this code to be able to exit full screen but it doesn't work :
app.on('keydown', function(e){
if(e.keyCode === 27){
app.exitFullscreen();
}
});
我也尝试过使用 cancelFullscreen。和全屏:假。不能正常工作。
I also tried with "cancelFullscreen". And "fullscreen:false". Not working.
非常感谢您的关注!
推荐答案
这很容易。在渲染过程中添加键盘侦听器,然后通过远程
模块操作窗口。
It's quite easy. Add a keyboard listener in render process, and then manipulate the window through the remote
module.
const remote = require("electron").remote;
document.addEventListener("keydown", event => {
switch (event.key) {
case "Escape":
if (remote.getCurrentWindow().isFullScreen()) {
remote.getCurrentWindow().setFullScreen(false);
}
break;
}
});
这篇关于当“ esc”出现时,退出全屏被按下(使用带电子的javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!