FORUMS: list search recent posts

get mouse cursor x and y location in script

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
sheikh ghanee
get mouse cursor x and y location in script
on Apr 22, 2020 at 11:38:56 am

Hi,

I'm doing scripting for a small tool with a UI. I have bound it with a shortcut key. I'm using these lines to position the window and show the window,


MyToolPanel.frameLocation = [50, 150];
MyToolPanel.show();


now i need to get the cursor x and y coordinates, so that I can pass it to 'myToolsPanel.frameLocation' using a variable.

I'm new to scripting tools. I couldnt find any references on web.

need help to get cursor x and y coordinates inside the script.


Return to posts index

Constantin Maier
Re: get mouse cursor x and y location in script
on May 29, 2020 at 10:30:53 am

Hey, from all that I've read, unfortunately, there is no way to true way to do this within ExtendScript. However, I found a workaround to get the XY mouse coordinates by creating an invisible dialog before the actual one that fills the whole screen, then read the mouse position via mouseover EventListener and close it right away. After that show the actual dialog.
If the cursor is very close to the edge of the screen, the mouse position won't be read through and everything is in limbo. However, if you move the mouse a little bit, the actual dialog will show. I think it's a good compromise still, especially if your script is accessed very frequently it is quite annoying to always have to move your mouse to some other place.
I have tested this workaround with multiple screens and it seems to work fine. However, I haven't done extensive testing. If there is something problematic about the code, please prove me wrong!

Here's my code:

// Mouse Position Variables
var scrPosX;
var scrPosY;

// Get Min-Max Points from all available Screens
var posTop = 0;
var posBottom = 0;
var posLeft = 0;
var posRight = 0;
for (i = 0; i &lt; $.screens.length; i++){
if ($.screens[i].top &lt; posTop){
posTop = $.screens[i].top;
}
if ($.screens[i].bottom > posBottom){
posBottom = $.screens[i].bottom;
}
if ($.screens[i].left &lt; posLeft){
posLeft = $.screens[i].left;
}
if ($.screens[i].right > posRight){
posRight = $.screens[i].right;
}
}

// Calculate Size for Position Checking Window from Points
var sizeX = posRight - posLeft;
var sizeY = posBottom - posTop;

// Create the Window
var mouseCapWin = new Window("dialog", undefined, undefined, {borderless: true})
mouseCapWin.preferredSize.width = sizeX;
mouseCapWin.preferredSize.height = sizeY;
mouseCapWin.location = [posLeft, posTop];
mouseCapWin.opacity = 0.01;

// ...and a Group (so the Window won't be empty)
var mouseCapGroup = mouseCapWin.add("group", undefined, {name: "mouseCapGroup"})
mouseCapGroup.preferredSize.width = sizeX;
mouseCapGroup.preferredSize.height = sizeY;
mouseCapGroup.spacing = 0;
mouseCapGroup.margins = 0;

// Check Position via Event Listener
mouseCapWin.addEventListener("mouseover", grabMousePos);
function grabMousePos(m){
scrPosX = m.screenX;
scrPosY = m.screenY;
mouseCapWin.close();
}

// ...and now "show" the Window
mouseCapWin.show();


http://www.constantin-maier.com


Return to posts index

Constantin Maier
Re: get mouse cursor x and y location in script
on May 29, 2020 at 10:42:23 am

...just noticing that the screen min-max variables should better start with the ones of the first screen. I don't know whether it's possible for a screen to not start at 0 but it's certainly safer that way.

var posTop = $.screens[0].top;
var posBottom = $.screens[0].bottom;
var posLeft = $.screens[0].left;
var posRight = $.screens[0].right;
for (i = 1; i &lt; $.screens.length; i++){
if ($.screens[i].top &lt; posTop){
posTop = $.screens[i].top;
}
if ($.screens[i].bottom > posBottom){
posBottom = $.screens[i].botto
}
if ($.screens[i].left &lt; posLeft){
posLeft = $.screens[i].left;
}
if ($.screens[i].right > posRight){
posRight = $.screens[i].right;
}
}


http://www.constantin-maier.com


Return to posts index


sheikh ghanee
Re: get mouse cursor x and y location in script
on May 29, 2020 at 1:55:32 pm

I did not consider of possibility of a display to start at non zero value.
Thanks for pointing out.
I should test it out.
my code goes like this.

var screenSizeX = 0;
var screenSizeY = 0;
var numArgs = $.screens.length;
//Get combined resolution of the screen including multiple display setups
for (i = 0; i &lt; numArgs; i++) {
var scnSize = $.screens[i];
if (scnSize.right > screenSizeX) {
screenSizeX = scnSize.right;
}
if (scnSize.bottom > screenSizeY) {
screenSizeY = scnSize.bottom;
}
}


Return to posts index

Constantin Maier
Re: get mouse cursor x and y location in script
on May 29, 2020 at 3:19:13 pm

Yeah, I'm not sure... It seems that the first screen always starts at 0. But who knows... However, you can certainly get other values for the second screen. Let's say your second screen is set up so that it's position is higher than the first screen, you will get a negative value for its Y coordinate. So I'd say it's better to get top and left coordinates as well.

http://www.constantin-maier.com


Return to posts index

sheikh ghanee
Re: get mouse cursor x and y location in script
on May 29, 2020 at 1:08:42 pm

Hi,
Thanks for your solution. I really appreciate it.
I had to solve it in couple of days, so I already did it.
This is quite literally the technique I used to solve the issue.

Creating a dummy window, borderless with zero opacity did the trick.
Yes, if we calculate the maximum resolution from the primary display, It works very well with multiple displays.
I used multiple event listeners for this dummy window like "blur" and "mouse down" to dismiss the window, and to avoid bugs if the user moves the focus away from this dummy window.

Waiting for the tool to be posted online so that I can share the code.
But thanks again.


Return to posts index


Constantin Maier
Re: get mouse cursor x and y location in script
on May 29, 2020 at 3:07:12 pm

Cool, would be interesting to get a look at your code! I also thought caring a bit more about exceptions would be nice.

http://www.constantin-maier.com


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2020 CreativeCOW.net All Rights Reserved
[TOP]