Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 2, 2011, 09:39 PM   #1
Blakeasd
macrumors 6502a
 
Join Date: Dec 2009
Make NSWindow Follow Mouse Cursor

Hello,
How can I make an NSWindow follow my mouse cursor in Objective-C and Cocoa.
Thanks
Blakeasd is offline   0 Reply With Quote
Old Apr 3, 2011, 12:06 AM   #2
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Subclass NSWindow with the following:

Code:
- (id)initWithContentRect:(NSRect)contentRect
                styleMask:(NSUInteger)windowStyle
                  backing:(NSBackingStoreType)bufferingType
                    defer:(BOOL)deferCreation
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    self = [super initWithContentRect:contentRect styleMask:windowStyle backing:bufferingType defer:deferCreation];
    if (self) {
        [self setAcceptsMouseMovedEvents:YES];
    }
    return self;
}


- (void)mouseMoved:(NSEvent *)event
{
    NSPoint p = [NSEvent mouseLocation];
    NSLog(@"%s (%.1f, %1.f)", __PRETTY_FUNCTION__, p.x, p.y);

    NSRect f = [self frame];
    if (!NSPointInRect(p, f)) {
        p.x -= f.size.width / 2.0;
        p.y -= f.size.height / 2.0;
        [self setFrameOrigin:p];
        NSLog(@"%s Moved window to (%.1f, %1.f)", __PRETTY_FUNCTION__, p.x, p.y);
    }
    
    [super mouseMoved:event];
}
You can probably do the same thing with a window delegate as well.

You might want to tweak exactly how you move the window and add logic to keep the window fully on screen.
jiminaus is offline   0 Reply With Quote
Old Apr 3, 2011, 08:31 AM   #3
Blakeasd
Thread Starter
macrumors 6502a
 
Join Date: Dec 2009
How exactly would I tweak the windows location relevant to the mouse? Also how can I keep the window moving when the mouse clicks on something other than the window. Thanks for everything so far, you've been extremely helpful.
Blakeasd is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Jumpy Mouse Cursor TWO2SEVEN MacBook Pro 5 Apr 25, 2014 04:56 PM
Jumping mouse cursor macmesser Mac Pro 1 Aug 15, 2013 08:20 AM
Gamepad controls mouse cursor jpenguin OS X 10.8 Mountain Lion 0 Jun 25, 2013 02:11 PM
Mouse Cursor too small tommy0614 iMac 4 Feb 9, 2013 05:07 PM
How to adjust Mouse Cursor in ML WizardHunt OS X 10.8 Mountain Lion 5 Dec 20, 2012 10:30 AM

Forum Jump

All times are GMT -5. The time now is 01:57 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC