When cursor reaches the end of the screen how to pause the script?

Hi,

So basically when the cursor reaches one of 4 ends of the my screen I need to stop (or pause) the script. As soon as the cursor moves back (even if it’s just 1px), the script continues executing.

Is it possible to do this?
Thanks.