octalmage / robotjs

Node.js Desktop Automation.
http://robotjs.io
MIT License
12.4k stars 970 forks source link

Electron combined with robotjs on Win10 and Win11 systems: scrollMouse method does not work #764

Closed zhangguanlei closed 3 months ago

zhangguanlei commented 3 months ago

Electron combined with robotjs on Win10 and Win11 systems: scrollMouse method does not work

On Windows 10 system: Executing the scrollMouse method does not work. Dragging with mouseToggle and moveMode will cause the mouse event to freeze, but it can respond normally on Mac OS and does not prompt error messages on Windows system. I don't know why this happened. I hope to know the reason and provide some tips or solutions. Thank you very much

zhangguanlei commented 3 months ago

I tried to rewrite some of the Windows system code under the scrollMouse method in the mouse. c file. After testing, I was able to use scrolling normally. The code is as follows:

Before modification:

mouseScrollInputs[0].type = INPUT_MOUSE;
mouseScrollInputs[0].mi.dx = 0;
mouseScrollInputs[0].mi.dy = 0;
mouseScrollInputs[0].mi.dwFlags = MOUSEEVENTF_HWHEEL;
mouseScrollInputs[0].mi.time = 0;
mouseScrollInputs[0].mi.dwExtraInfo = 0;
// Flip x to match other platforms.
mouseScrollInputs[0].mi.mouseData = -x;

mouseScrollInputs[1].type = INPUT_MOUSE;
mouseScrollInputs[1].mi.dx = 0;
mouseScrollInputs[1].mi.dy = 0;
mouseScrollInputs[1].mi.dwFlags = MOUSEEVENTF_WHEEL;
mouseScrollInputs[1].mi.time = 0;
mouseScrollInputs[1].mi.dwExtraInfo = 0;
mouseScrollInputs[1].mi.mouseData = y;

SendInput(2, mouseScrollInputs, sizeof(mouseScrollInputs));

After modification:

  INPUT scrollMouseY = {};
  scrollMouseY.type = INPUT_MOUSE;
  scrollMouseY.mi.dwFlags = MOUSEEVENTF_WHEEL;
  scrollMouseY.mi.mouseData = y;
  SendInput(1, &scrollMouseY, sizeof(scrollMouseY));

  INPUT scrollMouseX = {};
  scrollMouseX.type = INPUT_MOUSE;
  scrollMouseX.mi.dwFlags = MOUSEEVENTF_HWHEEL;
  scrollMouseX.mi.mouseData = x;
  SendInput(1, &scrollMouseX, sizeof(scrollMouseX));

I don't know the specific reason for this bug, but when I searched for relevant C++documentation and rewrote it as modified code, the test ran normally on Windows.

RockshowGamer2 commented 3 months ago

holy crap it actually worked for me! thank you so much

zhangguanlei commented 3 months ago

Electron combined with robotjs on Win10 and Win11 systems: scrollMouse method does not work

On Windows 10 system: Executing the scrollMouse method does not work. Dragging with mouseToggle and moveMode will cause the mouse event to freeze, but it can respond normally on Mac OS and does not prompt error messages on Windows system. I don't know why this happened. I hope to know the reason and provide some tips or solutions. Thank you very much

  • RobotJS version: 0.6.0
  • Node.js version: v14.17.5
  • npm version: 6.14.14
  • Operating System: win10、win11

I have identified the cause of the jam, which is due to the electron program setting -webkit-app-region: drag; through CSS; Draggable area. When using robotjs to operate dragging in the draggable area, there may be unresponsive situations. The solution is to change the drag event of electron from the original webkit app region: drag; Modify to the following code implementation

function electronDrag(dragDom){
    let dragging = false;
    let mouseX = 0;
    let mouseY = 0;
    let oldWidth = 0;
    let oldHeight = 0;
    dragDom.addEventListener('mousedown', (e) => {
        dragging = true;
        const { pageX, pageY } = e;
        oldWidth = outerWidth
        oldHeight = outerHeight
        mouseX = pageX;
        mouseY = pageY;
    });
    window.addEventListener('mouseup', () => {
        dragging = false;
    });
    window.addEventListener('mousemove', (e) => {
        if (dragging) {
            const { pageX, pageY } = e;
            const x = screenLeft + pageX - mouseX;
            const y = screenTop + pageY - mouseY;
            win.setBounds({ x, y, width: oldWidth, height: oldHeight })
        }
    });
}