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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Mouse cursor refuses to leave center of monitor after awakening. RonArt OS X 3 Jan 8, 2011 12:27 PM
How to make mouse cursor movement like windows Molopo Mac Basics and Help 1 Aug 6, 2010 11:35 AM
How to fix mouse cursor and key cursor lag theRacerR8 MacBook Pro 1 May 7, 2010 07:33 PM
Making NSWindow hidden at start Samppaa Mac Programming 2 Mar 26, 2010 10:12 AM
Mouse cursor skipping with new mouse. DELTAsnake Mac Basics and Help 0 May 6, 2009 12:46 AM


All times are GMT -5. The time now is 10:08 AM.

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

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