Skip to content

Instantly share code, notes, and snippets.

@LogIN-
Created September 30, 2014 11:49
Show Gist options
  • Save LogIN-/42ee42b8c52ea996f571 to your computer and use it in GitHub Desktop.
Save LogIN-/42ee42b8c52ea996f571 to your computer and use it in GitHub Desktop.
Node native module control mouse position coordinates
#include <node.h>
#include <v8.h>
#include <stdio.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
using namespace node;
using namespace v8;
Handle<Value> setXY(const Arguments& args) {
HandleScope scope;
if (args.Length() != 2) {
ThrowException(Exception::TypeError(String::New("This function accepts two argument!")));
return scope.Close(Undefined());
}
if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
ThrowException(Exception::TypeError(String::New("Input must be a number!")));
return scope.Close(Undefined());
}
//Get system window
Display *display;
Window root_window;
// Open Display
display = XOpenDisplay(NULL);
root_window = XRootWindow(display, 0);
XSelectInput(display, root_window, KeyReleaseMask);
XWarpPointer(display, None, root_window, 0, 0, 0, 0, args[0]->NumberValue(), args[1]->NumberValue());
XFlush(display);
// Close Display
XCloseDisplay(display);
return scope.Close(Undefined());
}
void init(Handle<Object> target) {
target->Set(String::NewSymbol("setXY"),
FunctionTemplate::New(setXY)->GetFunction());
}
NODE_MODULE(mouse, init)
@LogIN-
Copy link
Author

LogIN- commented Sep 30, 2014

add "-lX11" to binding.gyp libraries:

{
  "targets": [
    {
      "target_name": "mouse",
      "sources": [ "mouse.cc" ],

      "libraries": ["-lX11"]
    }
  ]
}

simple usage (to draw circle with cursor on screen):

var mouse = require('./native/build/Release/mouse');
var repeat = 1;

var radius = 50;
var centerX = 300;
var centerY = 300;
var steps = 1000;

var x, y;
for (var a = 0; a < repeat; a++) {
    for (var i = 0; i < steps; i++) {
        x = (centerX + radius * Math.cos(2 * Math.PI * i / steps));
        y = (centerY + radius * Math.sin(2 * Math.PI * i / steps));

        mouse.setXY(x, y);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment