How to calculate if the cursor position is at the edge of the window

I choose mouse long press to trigger the action, so when I resize the window, it triggers the macro. I want to avoid this by detecting if the cursor is at the edge of the window. How to do this?

The edge of a window isn't the target -- you want to avoid strip "covering" the edge, the area that is x pixels outside through y pixels inside it. I don't know what x and y are so you'll have to try some (small) numbers and see what works.

So the theory is:

  1. Get the bounds of the window
  2. Define an "outside frame" that is x pixels bigger in each direction
  3. Define an "inside frame" that is y pixels smaller in each direction
  4. Detect if the pointer is positioned inside the outside frame and outside the inside frame
  5. Decide that's too much work and pick another trigger! :wink:

The following gives you 5 pixels inside and 5 out, for a 10 pixel wide "activation area" overlaying the from window's edge -- that's too big, but makes it easy for you to see what's going on:

Test Window Edge.kmmacros (4.4 KB)


Set the trigger to a hotkey that suits you, put the pointer near the the frontmost window's edge, and you'll get a brief displayed text reporting the pointer's position with respect to the window's edges' "activation areas".

I'm sure there are more concise ways of doing this, but this method makes it easy to see how it is done so you can do your own adjusted version for your macro.


Is it possible, with a modification to this macro, to get a mouse click, control click to be exact, on the window title, regardless of the window size? Thanks in advance.

Not with this method. While the vertical dimensions could be derived from the window bounds -- a constant offset from the top edge and a constant height -- and you could find the horizontal middle -- half the window width -- the width of the element will vary depending on how much text there is.

You could probably do it with AppleScript, using System Events to return the bounds of the UI element to KM and then testing mouse position against that. But you should start a new thread, explaining what you are trying to achieve -- it may be that someone's already done similar but skips this thread because the title's not relevant.

Thank you very much, I’ll do it.

Thank you very much. It works very well. You always find the problem clearly and solve it quickly, I learn a lot from you. Have a good day!

1 Like