Get mouse coordinates from non-mouse events

Just as it says in the title. Possible? I don’t think it can be done through javascript alone, to get the pointer position at the time of a keypress event for instance. But I wonder if anyone has any trick on how to do it from within an add-on.

Keeping constant track of mouse movement by setting a mousemove listener is not really an option. :wink:

1 Like

Mouse position is the example we decided to use when showing off js-ctypes. A solution for all the different platforms is here:

https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/Standard_OS_Libraries

All there :slight_smile:

One note about x11 though, x11 is only meant to be used on the main thread. If you want to use it from a ChromeWorker, then you’ll need to use XCB. I did a lot of XCB work so if you need it let me know and I’ll hook you up. :slightly_smiling:

On Mac some functions need to be run from mainthread. I think mouse position via CoreGraphics is thread safe though, pretty sure.

ChromeWorker is definitely the recommend way to run js-ctypes. If you use PromiseWorker then you can make a quick getMostPosition function that returns a promise, really nice and simple - https://developer.mozilla.org/en-US/docs/Web/API/PromiseWorker

1 Like

Perfect! Exactly what I was looking for. :smiley:

Except for the fact that I’ve never used ctypes or any external libs/resources myself, so it’s a lot to learn just to open a panel next to the mouse… My procrastinating alter-ego is screaming in my head “just anchor the thing somewhere” right now. :stuck_out_tongue_winking_eye:

I’ll create a module for you then. Will async api be ok?

So you would do:

var promise_getMouse = MouseWorker.getCurrent();
promise_getMouse.then(function(aCoord) {
    console.log(aCoord.x, aCoord.y);
});

Do you have a repo i can make a PR too? what’s your timeframe ?

Dude you’re awesome! But you really don’t need to. I can’t even test it properly across all systems right now. :’( And it’s not urgent at all, just a curiosity I thought up this morning. Tell you what, in a few weeks (well, realistically, months), I’m going to give js-ctypes a go anyway because eventually I’ll have to. This will be a good place to start, so by then if I can ask you for help with it that’d be just awesome. :slight_smile:

My pleasure! It’s really a not too much, I have the types already typed up:

I just plop it in, cut out the stuff you aren’t using from ostypes and done. Probably half hour to 1 hour of work max.

Here’s a ostypes playground: