0

I'm looking for a method to define an area of the screen that the mouse cannot leave. I have been directed by rob mayoff, the answerer of this question, that I can use a Quartz Event Tap to detect mouse events. This puts me part of the way to the solution to THIS question.

I need to define an irregular area of the screen, not just a rectangle, that the mouse cannot leave. I have been researching this and the only idea I can come up with is using a bitmap to define the irregular area, as it will be very oddly shaped. However, I am open to solutions other than using a bitmap.

Since this must be done on a Mac, I've determined that I will have to use objective C / Cocoa .

I need to know how to define the area and, equally importantly, how to find the closest point in the area to the mouse (so that I can move the mouse to it if the mouse tries to move outside of the area).

This is similar to what the restricted area will look like:

screenshot

Community
  • 1
  • 1
BumbleShrimp
  • 2,150
  • 23
  • 42
  • 1
    The method you describe will find the point of the area, intersected with the line segment from T to P, that is closest to T. That will not necessarily be the point of the area that is closest to T. – JWWalker Nov 20 '11 at 09:22
  • Yes, if you have a better algorithm/equation, please share it! I am hoping this will work just fine since I will be moving the mouse back to that point, so T will never be more than a few pixels outside the area to begin with. However, if the user moved the mouse very fast, it could end up something like 100 pixels away from the edge of the area, and this would make my algorithm not as pretty. However, right now, the only thing I could think of that would be better is a pathing algorithm such as A*, and I don't think that would be worth the effort. – BumbleShrimp Nov 20 '11 at 17:02

2 Answers2

2

Load your mask into an NSBitmapImageRep. For example:

    mask = [NSBitmapImageRep imageRepWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForImageResource:@"mask"]]];

Test your position against the mask like this:

- (BOOL)isAllowablePoint:(CGPoint)point
{
    if (point.x < 0 || point.y < 0 || point.x >= mask.pixelsWide || point.y >= mask.pixelsHigh)
        return NO;
    NSUInteger pixel[4];
    [mask getPixel:pixel atX:point.x y:point.y];
    return pixel[0] == 0;
}

Make sure your mask image is saved at 72 dpi. You can check/fix this by opening the image in Preview. Choose Tools > Adjust Size, and make sure the resolution box says 72.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
0

If you could express the area as a union of rectangles, you could find the point in each rectangle closest to the mouse point, and then one of those points will be the closest point of the area.

JWWalker
  • 22,385
  • 6
  • 55
  • 76