How to Store the Mouse Position and Restore it Later

I want to create a series of mouse clicks. I need to bring the mouse pointer to where it was before the macro is executed. I can't figure this one out.

I use Variable to store the mouse position before the first macro step is executed, but I can't find the way to move the mouse to that Variable after all the steps has beed executed.

I feel like I am missing something.


Use a “Set Variable to Text” action, and set its body to:

%Calculate%MOUSEX()%, %Calculate%MOUSEY()%

Then use a “Use Variable” action to “set the mouse location” to the variable you created in the first action.


It works! Thank you! I tried to search in the documentation for hours but the documentation isn’t too intuitive. I’d appreciate a pointer where I should dig in to learn these custom macros.

1 Like

Well, a good starting point is having a look in the Edit > Insert Token and Edit > Insert Function menus.

Maybe you could also get some tips from my Tips & Tricks for KM page (a similar method is used to display the cursor location, there, and many other stuff).

1 Like

For the most part, the documentation aims to be a reference. It is relatively good at that, but relatively weak at answering “how do I” questions (other than the explicit How do I section). In part its weak at this because people use Keyboard Maestro for such a wide variety of tasks that documentation of this sort is pretty much a hopeless task - 90+% of it would be useless to any given reader.

As @philippe says, the best way to amp up your use of Keyboard Maestro is to look through the text tokens and function section to see what is in there, as well as looking through the various lists of example macros:


Super helpful. Thanks. I solved my problem with your help.

And how can I offset the mouse coordinates after I have captured them into a variable?


Just add the offset in the coordinates fields in the Move or Click Mouse Action.

1 Like

Thanks Michael.

This is pretty obvious. Sometimes I get brain knots when dealing with long macros!

1 Like

This thread has some long history but in the current version of KM (v 9.2 as of this writing) there is a "shorter" way:

  1. Set variable to text with %CurrentMouse%
  2. Use variable to set mouse location to <whatever variable name you used in step 1>

Hey jasonekratz!

Thanks for your interesting approach and update to the issue, but now after storing the actual mouse position in variable "x" how do I move the cursor back / recall this position?

Can't figure out how to transfer the variable's content to a movement action...

Hi @uliauer - welcome to the KM forum!

Just in case you don't get a reply from @jasonekratz after all this time, you just need to use the KM action Use Variable like this:

KM 0 2022-04-11_22-58-51

Hope that helps.

You can read about this action in the KM wiki here: action:Use Variable [Keyboard Maestro Wiki]

1 Like

OK. GREAT! Thanks for your help, tiffle !
Just some other questions (sorry, absolute beginner here) - is there any simple and compact way of programming one macro for "do x once a key is pressed and holded (maybe a pretty long time, like a modifier key while tab-stepping through applications) and then do y once the key is released" ? Or do I always need to setup two macros separately, one for "key is down" and one for "key is released" ? The problem is, that when I use the "Execute Actions Until Conditions Met" action there's no way of choosing the USB device key (X-Keys USB module) I want to use as condition target - compared to the initial trigger selection menu (FEATURE REQUEST!) ...;-(
I also have the problem that I need to keep KM from interpreting the automatic trigger repeats once a key is down for longer. For example if I want to move the cursor to a certain location as long as a key on a USB controller is pressed down and want to store the initial mouse position in order to get back there after release the auto-repeated key trigger causes the store action to immediately pick up the new location with the first auto-trigger - making this step useless....sure I can find a workaround for this with setting a variable flag definition and a if/then loop definition, but that's really bulky....
And one big issue: I'm using Parallels Desktop and want to use macros there too. I've found though that there seems to be an issue that the keyboard presses don't go through to Windows consistently ! For example I've been setting up a simple macro for quickly jump from one application to the one last used with a key on my USB device and firing off only the simple action "Type the alt-Tab key". This only works about 70% of the time, every 3-5 presses it won't work once, sometimes 2 times in a row....but when I stay in the Mac domain and use cmd-Tab instead it works perfectly fine everytime.....I already tried to limit the macro group to being used with the Parallels app only - without success.....I also observed the macro in Debugging mode - here every action is showing up without this a known issue ? Or even better - is there a way of fixing it ? Like that I can't use KM at all.....

Sorry for getting this long - any help/comments deeply appreciated !

Anybody ?

For getting one macro to do two things for press and release: You can set two triggers; one pressed, one released, then have an If/Else using the text condition %Trigger% ends with "pressed" or "released".

You may also find this helpful, specifically the repeat macros:

Thanks for your ideas ! I'll definitely check those suggestions out once I have time to do so...!
But I didn't fully understand your first statement: If I add another Trigger (=2 in total) and use one for let's say "key is down" and one for "key is released"...would %Trigger% always refer to that particular trigger ONLY inside this actual macro ? Or am i supposed to subtitute "Trigger" by lets say "Apple Keyboard Key Pad 5" in case I'm using that numpad key ?

And: I sometimes have the problem of automatic re-triggering once I hold certain keys (and holding for some time always is the case, otherwise my distinction between "action A for initial press(down)" and "action B for release" wouldn't make much sense)....
So once I simply have KM insert two different text phrases for the two cases - I chose to use "BUTTON A (PRESS)" for Trigger pressed and "BUTTON B (RELEASE)" for Trigger released - and I use the Asterisk key on the numpad it keeps
a) writing the "original" key, in this case the printout in MS Word would look like:
BUTTON A (PRESS)***************************[etc. and after releasing] BUTTAN B (RELEASE)
b) repeating the whole phrase like:
BUTTON A (PRESS)BUTTON A (PRESS)BUTTON A (PRESS)BUTTON A (PRESS) [etc] when I use the trigger condition "is down" instead of "is pressed"....

Do you also have a trick for this auto-re-triggering problem ?

See the Semaphore Lock action.

Semaphore Lock for Dummies

You can use it to prevent a macro from being repeated.

Unfortunately/fortunately I'm on holiday and don't have my mac with me so, as much as I'd love to give an example to show what I mean, I can't. I'm back on Monday, so will try to help with examples if someone else hasn't already.

The best I can do right now is point you to this, which demonstrates the use of %Trigger% as mentioned before:

Just got back. Right, where were we...?

See 4 - Repeat.


  1. Set both the pressed and released triggers to the same USB Key.
  2. Replace the red actions as desired.
  3. Set repeat speed in the green action.

In use:

While the trigger is pressed the mouse location will be stored and the first action will repeat. Upon release, the second action will run and the mouse position will be recalled.

I don't use Parallels, and so can't be of much help I'm afraid.

Hopefully the Repeat macro (linked to above) helps answer these...?

Let me know if you need any more help. :blush: