Home > Net >  Pressing Win D causes an electron window that is not allowed to minimize to minimize on Windows 10
Pressing Win D causes an electron window that is not allowed to minimize to minimize on Windows 10

Time:01-03

Using electron 16.9.1, I have built an app whose window is not allowed to minimize and whose alwaysOnTop property is set to false. To put it another way, the idea is to have a window that cannot be minimized and which is always "underneath" other windows.

The configuration of the window looks like this:

const win = new BrowserWindow({
    resizable: false,
    movable: false,
    minimizable: false,
    maximizable: false,
    alwaysOnTop: true,
    fullscreenable: false,
    show: false,
    frame: false,
    backgroundColor: "#00FFFFFF",
    transparent: true,
    webPreferences: {
        nodeIntegration: true,
        contextIsolation: false
    }
});

It works fine, the functions of the app function as I have planned, with but one tiny flaw: once I use the Win D shortcut, the window is minimized. I am aware that many apps behave this way, but what I really want to know is whether there is a way to avoid this.

[Update] I realized the impossibility of what I am trying to ask for, and instead, I am now trying another approach: to listen to a "show the desktop" event in electron. Every time that event is triggered, I will show the window again. And so, the problem has now changed to: how do I monitor such an event?

CodePudding user response:

What I really want to achieve is allowing any window to get over it, but not the desktop.

Every time that event is triggered, I will show the window again. And so, the problem has now changed to: how do I monitor such an event?

When minimizable: true is set, the BrowserWindow does fire minimize event when Win D and Win M are pressed. So, we can set minimizable: true and listen to those, including user minimize, events and restore the window. As frame:false is set user won't see minimize button only option of clicking on taskbar icon will be available.

let mainWindow;
let counter = 0;

function createWindow() {
 mainWindow = new BrowserWindow({
  width: 600,
  height: 300,
  show: false,
  resizable: false,
  movable: false,
  minimizable: true,   /* keep it true */
  maximizable: false,
  alwaysOnTop: false,
  fullscreenable: false,
  frame: false,
  backgroundColor: '#00FFFFFF',
  webPreferences: {
   preload: path.join(__dirname, 'preload.js'),
   contextIsolation: true,
   enableRemoteModule: false,
   nodeIntegration: false,
  },
 });

 mainWindow.loadURL(indexPath);
 
 //try to restore window on minimize
 mainWindow.on('minimize', (e) => {
  e.preventDefault();
  console.log('minimize',   counter);
  //try changing delay value
  setTimeout(() => mainWindow.restore(), 200);
 });

 mainWindow.on('restore', () => {
  console.log('restore', counter);
 });

 mainWindow.once('ready-to-show', () => {
  mainWindow.show();
  if (dev) {
   mainWindow.webContents.openDevTools();
  }
 });

 mainWindow.on('closed', function () {
  mainWindow = null;
 });
}


Desktop doesn't come infront immediately it takes time so we can't keep delay too low to restore our window. I've deliberately kept the delay high in the code for the demo. Once you call restore, next restore calls have no effect. Drawback is user will be able click on the application icon in taskbar and the application will bounce back.

CodePudding user response:

Now We have to deal with disabling keyboard shortcut Win-D,
so we use globalShortcut module.

const { app, globalShortcut } = require('electron')
globalShortcut.unregister('Super D')

try these keys INSTEAD of Super:

  • Meta

Also try Capital D and small d

PLEASE ALSO TRY:

globalShortcut.register('Super D', function() {
        console.log('Blocked');
        return false;
    })

Don't get confused that I am asking you to register the key that you actually asked to unregister, THIS IDEA OF REGISTERING KEY CAME AS someone in GitHub said

Registering key in electron app makes it not to work anywhere.

Refer documentation:https://www.electronjs.org/docs/latest/api/global-shortcut#globalshortcutregisteraccelerator-callback

P. S I am sure this won't mostly work as windows owns the right of Win key but try it, I ask you to try many times as I have not tried it

  • Related