简体   繁体   中英

Input focus not working in electron properly

So far I build a simple Electron application. My problem is the input.focus() is not working on displaying an alert box. I tried to solve the problem, and I came up with a solution: when I minimize and maximize the window, the input.focus() is working well. So when I try to show an alert box, the input.focus() isn't working, except minimize and maximizing. I try to open the code in Chrome, and all functionalities are working very well, so the problem is in the Electron renderer.

Before minimizing and maximizing the window

在最大化窗口之前

After minimizing and maximizing the window

在此处输入图像描述

My Electron renderer

const path = require("path");
const { app, BrowserWindow } = require("electron");

const createWindow = () => {
  const win = new BrowserWindow({
    width: 780,
    height: 600,
    minWidth: 780,
    minHeight: 600,
    icon: path.join(__dirname, "assets/favicon.ico"),
    webPreferences: {},
  });
  win.maximize();
  // win.removeMenu();
  win.loadFile("index.html");
};
app.whenReady().then(() => {
  createWindow();
  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0)
      createWindow();
  });
});
app.on("window-all-closed", () => {
  if (process.platform !== "darwin")
    app.quit();
});

The Input focus will lose if the app window not focused.

When you minimize and maximize the app window the input focus come back.

And When you send alert box the alert open in another window so the main app window blur and its input focus blur.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM