Get Mouse Position, Run Other Actions, Then Return Mouse Pointer to Same Position

I'd like to know to how create an action that gets the position of the mouse, and then later, in another action, returns the mouse to that same position.

I am not a programmer. I have never taken a programming class. If you could make your answer as simple as possible for a non-programmer, that would be wonderful.

Thank you

1 Like

I wished there were two actions to create for this.

  1. save current mouse position.
  2. restore saved mouse position.

It could be that simple.

But as a non-programmer like you, I still don't know exactly how to do that.

This way you save the current mouse position.

And probably this is how the old position is restored. But fortunately there are many who know exactly how to do that. :slightly_smiling_face:


Yes, it really is as simple as that! Before reading @Frankb's reply, I made two test macros to work it out for myself, and they look the same, and they work.

So, @Donald, you might want to make two macros, one with the action to set the global variable (as shown in the first image), and one with the action to read the variable (as shown in the second). Then you can assign a different trigger to each macro.

It would be possible to combine them into one macro but that would add unnecessary complexity, I think.

More usually it's the other way round -- in a single macro you'd:

  1. Save the mouse position
  2. Do some stuff that involves moving the pointer around
  3. Return the mouse to the previously-saved position

...if only so you don't have to play "hunt the pointer" after the macro has finished!

1 Like

Yes, fair comment if step 2 is needed. Your interpretation of @Donald's aims is different from how I read it. I took the pointer question to be the full extent of the task but indeed he may well have a greater ambition for his macro than the second paragraph encouraged me to believe. :wink:

1 Like

Thank you all so very much for everyone's prompt reply. I really appreciate that.

I was able to get the actions to work, but the variable %CurrentMouse% didn't work for me, even though it seems like it should. Possibly it didn't work because I set the mouse a different position within the macro and the variable mouse_position was updated automatically to the new mouse position.

To keep that from happening I'd probably have to add an action something like mouse_original=mouse_position and then at the end of the macro use mouse_original to set the mouse position back to the original position. Because I don't know the syntax for basic math in this program, I changed %CurrentMouse% to %Calculate%MOUSEX()%, %Calculate%MOUSEY()% and then the macro worked for me.

Here is my macro. I use it change the zoom level to 400% in Lightroom Classic. I created similar macros to go to different zoom sizes, 100%, 200% etc

Thank you so very much for your quick help. I really appreciate it. I learned a lot playing with this simple macro. You guys got me started in the mysterious and wonderful world of Keyboard Maestro.

It's worth remembering that the Move and Click action can restore the original mouse pointer position automatically if you choose that option from the gear menu...



So there is no other way to change the zoom level in Lightroom? A menu, a shortcut?

This is really something I wish I had known years ago. :joy: Thank you @tiffle. This question has also been asked so many times, but I have never read this simple answer.

You're very welcome :grinning: That gear menu hides in plain sight a lot of the time!

Frankb, there is no other way to change an exact zoom level in Lightroom that I know of, other than pointing and clicking (but I am no expert). I like the idea of a macro because I tire of the eye-hand exercise that Lightroom forces me to do so much. I am going to try these macros on a stream-deck or a tour box.

tiffle, I didn't expect the restore mouse position would work, but it did! Wow. What a cool thing to learn. Thank you!

1 Like

I have found this explanation...

To zoom in quickly and easily, you can use this handy keyboard shortcut: CMD and + key . To zoom out, use the CMD and the - keys. If your first zoom doesn't get you to the magnification you desire, do it again; it will zoom in closer each time.

Ah, but it probably doesn't give you the exact zoom level you want.

Wow Frankb, you are right! What an easy way to change the zoom level.

I guess I should google things, before I make things harder than they need to be. (Kind of typical for me. :yum:)

I know I'll need Keyboard Maestro in other situations where I may not have a keyboard shortcut, so this exercise was still very useful for me. Thank you for your help.

Probably because these questions often involve multiple "move and click"s -- while you can do each of those as a "move, click, restore" you get annoying extra pointer movements.

It doesn't work like that. Your mouse_position variable is set to the evaluation of the %CurrentMouse% token at the moment the action is executed -- since there's no "auto-update" and you don't set mouse_position to another value during your macro it should work...

But it looks like you've extra text in the field, after %CurrentMouse% -- see how the action's name has ... after the %, indicating that there's more to show than there's room for? So mouse_position is an invalid value for coordinate-setting when you come to use it in the last action. Make sure you've no rogue spaces or linefeeds in there and that should solve your problem.

I do not know many apps. But I don't know any app that doesn't have a shortcut to zoom.

This reminds me of what @Nige_S and @noisneil always say, "Don't just explain what you want to do at the beginning, explain also why you need it." Sometimes there are very simple solutions. :slightly_smiling_face:



You are correct. I had a "space" and a line feed after %CurrentMouse%. I removed them and the macro worked.

I appreciate your expertise. You are amazing!

This another lesson learned for me.

1 Like

Great advice. Thanks. I'll try to keep it simple and explain my need. It would make my life easier. :upside_down_face:

It remains to mention that here once again the fundamental problem of the KM becomes visible.

If you search for "mouse" in the actions, you get this result

If there were additionally these two actions, no questions would arise also with beginners

  1. save current mouse position.
  2. restore saved mouse position.

And this is only one example of many. The developer of KM has asked several times to be told what would make his app easier for beginners, but he apparently finds such things pointless. :slightly_smiling_face: :man_shrugging:

1 Like

Don't stop there!

There are few fundamentals that will make life in KM easier...

The manual is very good. You don't have to learn it word for word, but have a quick skim so you can understand the concepts and, importantly, know where to go back to later when you have questions.

You use Actions to do things. There are a lot of Actions, most of them have multiple options, and most of them can use values that you can provide in some way.

You use Tokens and Functions to get values. You can store these values in Variables, save them for later or manipulate them with Filters or Search and Replace (text), Calculations (numbers), and in other ways -- and then use them in later Actions.

Branching, like "If Then Else" or "Switch/Case", and the various looping actions like "Repeat" and "While" add a lot of power, but using them relies on everything above, so concentrate on the basics first.

As a wise person never actually said: "Give a man a 'Save current mouse position' action and he'll be able to save his mouse position. Teach him how to roll his own using KM fundamentals and he'll be able to automate anything [1]".

But the most important fundamentals of all are:

  1. Work out what you want to do
  2. Break that down to the simplest steps you can
  3. Try and work out how to do each step using KM's Actions, Tokens, etc
  4. If stuck for more than 5 minutes, don't beat yourself up -- we've all been there. Post your problem here, following these guidelines and ideally uploading a sample macro, and help will soon be at hand.

Welcome to KM, and welcome to the Forum!

[1] Some hyperbole may have been employed in the making of this statement. The value of your macro can go down as well as up, etc, etc...

1 Like

Frankb, I was surprised that the actions you mentioned aren't a part of KM. Probably too basic for experienced programmers, but us beginners would appreciate them.

The last time I tried to program a computer was about 45 years ago, on an Apple ][ computer in basic. I never took a class, but I could do For Next Loops and print "Hello World" on the screen. Applesoft Basic made my head spin, but I did learn some of the logic of programming. I knew I could never be a programmer.

If the developer of KM could make it easier for non-programmers to create simple macros by providing some simple pre-made actions (even knowing that experienced programmers don't need them), I think the appeal for KM would grow, as not everybody is a programmer. Many people have some simple programming skills and they might be excited to try KM if it was a little easier to get started.

I have found your help and those of others really terrific, and I will plod along knowing there are helpful people here willing to lend a hand. Hopefully KM can be made a little more user friendly for beginners in the future :slightly_smiling_face: