iDevGames Forums
Cocoa: Mouse location while dragging window - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Cocoa: Mouse location while dragging window (/thread-5303.html)

Cocoa: Mouse location while dragging window - Fenris - Jul 14, 2005 03:30 AM

I'm trying to re-implement my mouse handling code, and I've stumbled upon a very strange kind of bug. I grab the mouse coordinates from [NSEvent mouseLocation] (but I've tried using the mouse position of just any event, and it exhibits the same glitch) and transform them to fit into my GL window. (The same problem appears even when I don't transform them, by the way)

Just to try it out, I'm drawing a little quad on the window that represents the mouse location, so I have a sort of "cursor". The problem is, when I drag the window (moving it onscreen) the little quad "drifts" - it moves too quickly. I've done a lot of research, and it isn't my drawing code at fault - just bumbling in with straight [NSEvent mouseLocation] still brings the same error. Now, what seems to be happening is that the window's screen coordinates aren't getting updated properly while dragging the window - only when the drag is completed, and sometimes not even then.

I guess my bottom line is: does anyone of you have some clever mouse location code for Cocoa that holds up even when dragging the window? (I suppose you have, or I would've heard about it, right?)

Cocoa: Mouse location while dragging window - Fenris - Jul 14, 2005 06:20 AM

I've narrowed the problem down even further with a new 20-line project. I create a window, and dump its screen location twice every second. When I drag the window, those positions go horribly out of sync. So, I tried to fudge it by listening for the NSWindowDidMove notification, but no beef there either - it seems to get sent haphazardly.

Is there a way of accomplishing this? It has to be possible, right? How do you guys handle this?

Cocoa: Mouse location while dragging window - TomorrowPlusX - Jul 14, 2005 07:37 AM

I haven't handled it, but I have done some work trying to override mouse events ( for purpose of implementing a "snapping" window border effect for palettes ).

My best suggestion to you would be to detect when window dragging starts ( say by catching a notification, or by tracking window position and if it changes ). When you know the window's moving, stop listening to normal mouse moved events and instead propagate your own using screen coordinates for the mouse, and create deltaX deltaY values by coparing the mouse positions to the window movement.

This sounds like a PITA. I've been flummoxed too many times by Cocoa's mouse movement weirdnesses with regard to moving windows.

Cocoa: Mouse location while dragging window - unknown - Jul 14, 2005 09:10 AM

If you know the mouse coord to start with
you could find the dx and dy of the window while its being dragged and change you mouse position varible accordinly.

in your window dragged update proc
newx = [getwindowlocation].x
newy = [getwindowlocation].y

mousex += newx-oldx
mousey += newy-oldy

oldx = newx
oldy = newx

Alternitivly you could get the global mouse coordinates and subtract the window position then the NSView position to convert them into that scheme.

Cocoa: Mouse location while dragging window - Fenris - Jul 14, 2005 04:46 PM

Thanks, both of you. I found my holy grail tonight, however. [NSWindow mouseLocationOutsideOfEventStream]. (Now that's a mouthful...)

Morrow+X: the notifications seem to get sent very haphazardly...