title: Cocoa window dragging
body: <p>I'm using addGlobalMonitorForEventsMatchingMask to listen to events in Cocoa:</p>
  
  <pre><code>[NSEvent addGlobalMonitorForEventsMatchingMask:NSLeftMouseDraggedMask 
                                         handler:^(NSEvent *event) {
      NSLog(@"Dragged...");
  }];
  </code></pre>
  
  <p>Though I would like to know if I'm dragging/moving a window (and which window that is, I can find the focused window though when holding command and dragging a window it doesn't get focus as far as I know.)</p>
  
  <p>So, can I detect whether or not I'm dragging a window?</p>
  
  <p>Update:</p>
  
  <p>I now have a class: "<code>SATest : NSObject &lt;NSWindowDelegate&gt;</code>" in which I implement the windowDidMove method (and later perhaps windowWillMove too.) Though, now the next step would be attaching this to a window, right? So my question now is: How do I attach the delegate to all windows of all apps?</p>
  
  <p>Update 2:</p>
  
  <p>I now can find a list of all open windows on the screen:</p>
  
  <pre><code>AXUIElementRef _systemWideElement;
  _systemWideElement = AXUIElementCreateSystemWide();
  
  CFArrayRef _windows;
  AXUIElementCopyAttributeValues(_systemWideElement, kAXWindowsAttribute, 0, 100, &amp;_windows);
  </code></pre>
  
  <p>Now I have to iterate over the windows, and of each get it's NSWindow so I can add my delegate to it: <code>[window setDelegate:self];</code></p>
  
  <p>Update 3: To be clear, this question is about detecting the dragging of ALL windows of ALL apps. Not only the windows of my own app.</p>
  
  <p>Also, I'm very new to this event and window management stuff, so no need to keep your answer short I'm happy to read a lot :P</p>
  
  <p>Thanks!
  -P</p>