【问题标题】:Exit full screen when "esc" is pressed (using javascript with electron)按下“esc”时退出全屏(使用带有电子的javascript)
【发布时间】:2016-06-22 12:04:38
【问题描述】:

我正在开发一个应用程序(使用电子),我希望用户能够在按下“esc”时退出全屏模式。我尝试了不同的方法,徒劳无功。 以下是在新浏览器窗口中以全屏模式启动应用程序并显示 HTML/CSS 内容的代码:

    '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();
  }
});

这是我试图添加到此代码中以便能够退出全屏但它不起作用的内容:

app.on('keydown', function(e){
    if(e.keyCode === 27){
      app.exitFullscreen();
    }
});

我也尝试过使用“cancelFullscreen”。和“全屏:假”。不工作。

非常感谢您的关注!

【问题讨论】:

    标签: javascript escaping fullscreen electron


    【解决方案1】:

    这很容易。在渲染过程中添加键盘监听,然后通过remote模块操作窗口。

        const remote = require("electron").remote;
    
        document.addEventListener("keydown", event => {
    
            switch (event.key) {
                case "Escape":
                    if (remote.getCurrentWindow().isFullScreen()) {
                        remote.getCurrentWindow().setFullScreen(false);
                    }
                    break;
                 }
        });
    

    【讨论】:

    • 感谢禅宗的回复。我尝试将此代码插入到我的 main.js 中,但我收到一条错误消息,并且应用程序变为黑屏,直到我退出。
    • @G.Delvigne 你应该将它插入到 html 文件而不是 main.js
    • 再次感谢您。现在,我将代码放在 index.html 文件中的“脚本”html 标记之间,但没有任何反应。 :/ 启动应用后按空格键没有结果。
    • 控制台输出错误吗?你如何使窗口全屏?@G.Delvigne
    • 没有错误。为了制作全屏,我更改了 main.js 中的默认设置。像这样: function createWindow () { // 创建浏览器窗口。 mainWindow = new BrowserWindow({fullscreen:true});默认设置为:mainWindow = new BrowserWindow({width: 900, height: 600});
    【解决方案2】:

    全局快捷方式捕获所有事件,最好使用隐藏菜单按钮。 例如,您可以使用此菜单项添加菜单:

    {
        label: "Exit full screen",
        visible: false,
        accelerator: "Esc",
        click(item, focusedWindow) {
            if (focusedWindow.isFullScreen()) {
                focusedWindow.setFullScreen(false);
            }
        },
    },
    

    您可以使用 electron 的 setApplicationMenu 命令应用新的菜单项: https://www.electronjs.org/docs/api/menu#menusetapplicationmenumenu

    【讨论】:

      【解决方案3】:

      这是对我有用的解决方案:

      function minimizeWindow () {
          mainWindow.setFullScreen(false);
          console.log(mainWindow);
      }
      
      app.on('ready', function() {
      
        const ret = electron.globalShortcut.register('Escape', function(){
          console.log('Escape is pressed');
      
              minimizeWindow();
        });
      
      console.log(electron.globalShortcut.isRegistered('Escape'));
      });
      
      app.on('will-quit', function(){
      
        electron.globalShortcut.unregister('Escape');
      
        electron.globalShortcut.unregisterAll();
      });
      

      【讨论】:

      • 全局快捷方式将全局注册,因此无论应用程序是否具有焦点或最大化,快捷方式处理程序都会触发。这也覆盖了其他应用程序的注册快捷方式(Escape 不再适用于它们)。要仅在应用程序聚焦时对快捷方式做出反应,应该查看localshortcuts
      【解决方案4】:

      对此也有一些问题,只需为转义键创建一个侦听器并将其放入其中,我建议不要将其放入电子的主文件或渲染器文件中。

       
      const { remote } = require('electron');
      const { BrowserWindow } = remote;
      
      document.addEventListener("keydown", event => {
      
              switch (event.key) {
                  case "Escape":
                      var window = BrowserWindow.getFocusedWindow();
                  if(window.isMaximized()){
                   window.unmaximize();
                 }else{
                   window.maximize();
                 };
                      break;
                   }
          });

      【讨论】:

      • 谢谢!我在其中创建了一个 script.js 文件,并放入了 sn-p。然后我将此文件链接到我的 index.html。 。但它不起作用。
      • 如何为转义键创建监听器?
      猜你喜欢
      • 2020-03-03
      • 1970-01-01
      • 2014-10-21
      • 2023-04-03
      • 2014-09-27
      • 2019-07-23
      • 2016-03-30
      • 1970-01-01
      • 2013-11-02
      相关资源
      最近更新 更多