Search code examples
javascriptnode.jswindowsnode-ffi

How can I click using ffi in NodeJS?


I am creating a robot for Windows. To move the mouse, this is my code so far:

var ffi = require('ffi'),
    user32 = ffi.Library('user32', {
        'SetCursorPos': ['long', ['long', 'long']]
    });;


user32.SetCursorPos(100,100);

I need a function that using ffi (or any other way) will click to given coordinates like

click(100,100);

Solution

  • This did the trick for me:

    var ffi = require('ffi'),
        user32 = ffi.Library('user32', {
            'SetCursorPos': ['long', ['long', 'long']],
            'mouse_event': ['void', ['int', 'int', 'int', 'int', 'int']]
        });;
    
    MOUSEEVENTF_LEFTDOWN = 2;
    MOUSEEVENTF_LEFTUP = 4;
    
    user32.SetCursorPos(3, 3);
    
    user32.mouse_event(MOUSEEVENTF_LEFTDOWN, 0 ,0 ,0 ,0);
    user32.mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);