I try to send a mouseWheel
event to a BrowserWindow
webContent
with electron with the following code:
const wheelEvent = {
type: 'mouseWheel',
x: 100, // X-coordinate (optional)
y: 100, // Y-coordinate (optional)
deltaX: 0, // Horizontal scroll amount
deltaY: 120, // Vertical scroll amount (e.g., 120 for one notch of scrolling)
};
// Send the event to the webview
win.webContents.sendInputEvent(wheelEvent, (result) => {
if (!result.success) {
console.log('Error sending input event:', result.error);
}
});
but nothing happen, and I don’t get any error. Is it because wheel events are not trusted ? https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent/WheelEvent
In that case mouseWheel events are no longer working with electron ?
I am sure that the browser window has the focus before running the code.
2
Answers
Have you checked the status of the events using the DevTools?
By the way Pupeeteer might help. It generates trusted events.
In your code
sendInputEvent
method does not support synthesizing mouse wheel events directly becausesendInputEvent
method can only be used to simulate certain types of input events that are allowed to be synthesized by the operating system.To simulate mouse wheel events in
Electron
, you can use thewebContents.executeJavaScript
method to execute js code within the web page contextAs per requirement replace
win
with yourBrowserWindow
instance.