Move Mouse Pointer to Current Window but Keep Relative Position Fixed

Hello Guys,

I just started using this app and still learning my way around it. Here is what I have done and what I would like to have...

My current setup

Right now, whenever I switch the window, I move move pointer to (1, 1) position from the center of the front window.

What I want:

I would like to keep the mouse pointer at the same location relative to the frontmost window.

Thanks,
Naman

The Action you have shown is already moving the mouse to a position relative to the front window. It is putting the mouse pointer just off-centre (1,1) to whatever is the front window's center. If you wanted to put the mouse pointer in the exact center of the window you would use (0,0) as the coordinates.

I am wondering exactly what you want to do. Do you want the mouse to be in the top left corner of the window? Or do you want the window itself to reposition itself centred on the pointer?

Do you mean "If the pointer was x pixels to the right and y pixels down from the top-left corner of window A, I want it the same distances from the the top-left corner of window B after I switch -- regardless of where window B is on screen?"

1 Like

Do you mean "If the pointer was x pixels to the right and y pixels down from the top-left corner of window A , I want it the same distances from the the top-left corner of window B after I switch -- regardless of where window B is on screen?"

Yes, this is correct. I'll add one more thing though. This (x, y) coordinate should take current window dimension into account if possible.

Taken into account how?

If you always want it the same distance from the top-left corner of the previous window, window size is irrelevant! So do you want "same distances, unless that's outside the window frame, in which case...", or "if it was 1/3rd in and 1/3rd down on the last window, put it 1/3rd in and 1/3rd down, regardless of window size", or something else?

Also, how are you switching between apps? There's no window- or pointer-position "history" -- if you are switching apps using a macro then grabbing the data needed before the app-switch should be easy, if you are using any other method it could be problematic...

If you always want it the same distance from the top-left corner of the previous window, window size is irrelevant! So do you want "same distances, unless that's outside the window frame, in which case...", or "if it was 1/3rd in and 1/3rd down on the last window, put it 1/3rd in and 1/3rd down, regardless of window size", or something else?

I think we both are talking about the same thing but just wanted to clarify. For example, current window size is 100 in height and 50 in width (in some units) and mouse pointer is say at (10, 10) units from top left corner of this window i.e. 1/10 down and 1/5 right from top left corner. So now, when I switch to a new window which is say 200 in height and 100 in width, so mouse position should still be 1/10 (of new height) down and 1/5 (of new width) right from top left corner of this new window. So that would be 20 down and 20 right from the top left corner.
Also, if the mouse pointer is outside the current window, then we can simply put the mouse pointer to (0, 0) from center of the new window. (I might be able to change it later own once I know how to implement the previous part :sweat_smile:)

Also, how are you switching between apps? There's no window- or pointer-position "history" -- if you are switching apps using a macro then grabbing the data needed before the app-switch should be easy, if you are using any other method it could be problematic...

I just use Cmd+Tab to switch between apps and Cmd+Tilde to switch between windows of a particular app. So in essence, whenever the focus changes to a new window (be of same app or different app), mouse pointer should move accordingly.

Right now, I just move the mouse pointer to (1, 1) from center of current window. I would like to do as I suggested above. So if that requires to store history of the current location, that's ok too. It's just that I am not sure how to do it and this, would be great if you could help me out.

Thanks,
Naman

So, given there's no history we have to keep track of window frames ourselves. Luckily we only need to remember the last frontmost window. Unluckily we'll have to update that every time the frontmost window changes, either entirely (window or application switch) or in size.

Luckily (again!) KM has triggers for when the focused window changes (switch) and when the focused window's frame changes (resize). We do have to add one more -- when I tested I found that an app that had no open windows didn't trigger a focused window change on switching, so we also need an "Any App activates" trigger.

I don't know if it's necessary, but I've used a semaphore lock and delay so KM doesn't spam multiple macro instances when doing a window drag. And I found that ⌘~ was alternating rather than cycling through windows until I added the "Pause Until ⌘ released" action.

After that, it's just maths! If the pointer is inside the "old" front window it will always be inside the "new" one because of relative positioning, which just leaves the edge cases. In order:

  • If "new" app has no window, put pointer in middle of screen
  • If "old" app had no window, put pointer in middle of "new" app's front window
  • If pointer was outside the bounds of the "old" front window (in the same or different app), put pointer in middle of "new" from window.

Biggest downside -- because this reacts to every window switch, no matter the method, it also triggers when clicking on another window, the Dock -- because your pointer will have been "outside" the old window in these cases it should always end up in the middle of the new front window. Making a new document with ⌘N is also a window change -- the resulting pointer position will depend on where it was relative to the previous window.

I don't have multiple screens on my iMac, and can't abide Spaces -- adjusting things to work in those situations is, as they say, "an exercise for the reader" :wink:

Try it and see how you get on:
Switch Maintaining Relative Pointer Position Within Front Window.kmmacros (16.6 KB)

Summary

2 Likes