I am making an image editor (just a simple editor for a program I am making), and I need to find the position of the mouse. Is it possible to do this in Objective-C? If so, how?
EDIT: I just thought I should mention that I have done some research on this and I haven't found anything that works. The code I have in my header file is as follows:
#import <Cocoa/Cocoa.h>
@interface test : NSWindow <NSWindowDelegate> {
}
@end
I can handle any outlets and actions that are needed; I just need to know how to find the position of the mouse.
If you are catching it through an event, such as mouseDown
, it will look like this:
- (void)mouseDown:(NSEvent *)theEvent {
NSPoint mouseDownPos = [theEvent locationInWindow];
}
Otherwise, use:
[NSEvent mouseLocation];
EDIT: (Sorry, I wrote NSPoint *
, which is wrong, since it's a struct)
Inside a mouse event handler (mouseDown:
, mouseUp:
, mouseMoved:
, etc.), you can ask the event for its locationInWindow
. If you need the mouse location at some arbitrary time (generally you won't want to do that, since it's rare for a program to have a one-off need to discover the mouse location), you can do [NSEvent mouseLocation]
and it will return the mouse's location at the time in screen coordinates.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With