Cocoa: Mouse location while dragging window

Posts: 834
Joined: 2002.09
Post: #1
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?)
Quote this message in a reply
Posts: 834
Joined: 2002.09
Post: #2
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?
Quote this message in a reply
Posts: 1,199
Joined: 2004.10
Post: #3
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.
Quote this message in a reply
Posts: 1,403
Joined: 2005.07
Post: #4
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.
Quote this message in a reply
Posts: 834
Joined: 2002.09
Post: #5
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...
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  ending location from angle and speed Kazooless 5 7,032 Apr 3, 2009 02:40 PM
Last Post: Gillissie
  polling mouse location? alloca 1 3,692 Jan 22, 2009 04:14 PM
Last Post: ThemsAllTook
  Window focus events in Cocoa Jar445 2 8,064 Jan 18, 2009 06:02 PM
Last Post: Jar445
  changing mouse icon in cocoa Leroy 13 11,226 Sep 4, 2007 07:10 PM
Last Post: SethWillits
  Mouse coordinates with Cocoa vnvrymdreglage 13 10,271 Apr 8, 2007 07:26 AM
Last Post: vnvrymdreglage