Electron BrowserView无法捕获鼠标事件 [英] Electron BrowserView not capturing mouse events

查看:1100
本文介绍了Electron BrowserView无法捕获鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于电子的浏览器之类的应用程序,需要呈现客户端应用程序。我很想使用electronic的webivew来呈现我的应用程序,但不建议使用它们,并且默认情况下也将其禁用。同样由于Webview背后的铬制OOPIF(Out of process IFrames)体系结构,它不再可能捕获对我的应用程序至关重要的键盘和鼠标事件。

I have a Electron based browser like application which requires rendering of client applications. I was tempted to use electron's webivew to render my apps but they are not recommended and also disabled by default. Also because of chromiums OOPIF (Out of process IFrames) architecture behind webviews its no longer possible to capture keyboard and mouse events which are critical to my application.

所以我正在使用更新的BrowserView api并使用它来呈现我的客户端Web应用程序。但是可悲的是,我只能使用 before-input-event 事件捕获键盘事件。

So I am using the newer BrowserView api and using it to render my client web applications. But sadly I could only capture keyboard events using before-input-event event.

这是我的示例

let mainWindow = null;

app.on('ready', () => {
  mainWindow = new BrowserWindow({ show: false });
  mainWindow.setBounds({ x: 0, y: 0, width: 800, height: 600 })
  mainWindow.once('ready-to-show', () => {
    mainWindow.show();
  });

  let view = new BrowserView()
  mainWindow.setBrowserView(view)
  view.webContents.loadURL('https://electronjs.org')
  view.webContents.on('before-input-event', (event, input) => {
    console.log(event, input);
  });
});

我也研究了electronic的github问题和官方文件,但找不到任何东西。有没有人找到一种从BrowserView内部捕获鼠标事件的方法?任何帮助将不胜感激。

I looked into electron's github issues and the official documents as well but couldn't find anything. Has anyone found a way to capture the mouse events as well from inside of a BrowserView ? Any help would be very appreciated.

推荐答案

通过在浏览器视图中使用preload webPreferences,您可以在ipcRenderer中使用preload.js脚本

by using preload webPreferences for browserview where you can use ipcRenderer where the preload.js script will be

document.addEventListener('click', (event) => {
  ipcRenderer.send('something', event);
});

在主电子js中,您必须使用预加载并调用ipc main来捕获鼠标数据

and in the main electron js you have to use preload and call ipc main to capture the mouse data

let view = new BrowserView({
 webPreferences: {
  preload: path.join(__dirname, 'preload.js'),
 }
});

ipcMain.on('something', function (event, arg) {
  // your code here
})

这篇关于Electron BrowserView无法捕获鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆