NW.js How to make F11 hot key to switch between full screen window and standard window?
Asked Answered
S

1

1

The following code can use the F11 hot key, so that the browser window full screen, how to achieve the second press the F11 hot key, restore the original window size?

<!DOCTYPE html>
<html>
<head>
  <script>
nw.App.registerGlobalHotKey(new nw.Shortcut({
  key: "F11",
  active: function () {
    // decide whether to leave fullscreen mode
    // then ...
    nw.Window.get().enterFullscreen();
  }
}));
  </script>
</head>
<body>
</body>
</html>
Shirtmaker answered 10/12, 2017 at 23:30 Comment(1)
Remember the size and location of the window before entering full screen, then restore it after exiting.Circumspect
P
2

I was facing the same problem, found this question while doing some research.

You have two options:

Using the toggleFullScreen method:

nw.App.registerGlobalHotKey(new nw.Shortcut({
  key: "F11",
  active: function () {
    nw.Window.get().toggleFullscreen();
  }
}));

Or using an if/else statement:

window.isFullScreen = false;
nw.App.registerGlobalHotKey(new nw.Shortcut({
  key: "F11",
  active: function () {
    if (window.isFullScreen) {
      nw.Window.get().leaveFullscreen();
      window.isFullScreen = false;
    } else {
      nw.Window.get().enterFullscreen();
      window.isFullScreen = true;
    }
  }
}));

Both produce the same result, I personally prefer the toggleFullScreen method because the code looks cleaner.

Hope this helps.

Protuberance answered 21/12, 2017 at 18:8 Comment(1)
Thank you very much for your help. Can you help me to look at this problem? It has been a month for me. Thank you again. #47744034Shirtmaker

© 2022 - 2024 — McMap. All rights reserved.