3

As the titles says i wonder if it is possible to detect if the mouse button is down. I tried putting this code in my app delegate.m but with no success.

- (void)mouseDown:(NSEvent *)theEvent
{
    NSLog(@"hello world!");
}

A quick google search showed me that this method only works inside of NSWindows. However, there most be some way to detect if mouse position is pressed, and if so; how can i do it?

Freddy
  • 810
  • 1
  • 9
  • 19
  • You should consider if you really care about a mouse-down event or if what you're actually interested in is loss of active status for your app or key/main status for one of your windows. – Ken Thomases May 10 '14 at 09:02

3 Answers3

9

You can use NSEvent addGlobalMonitorForEventsMatchingMask:

define in your control:

id mouseEventMonitor;

-(id)init{

    mouseEventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:(NSLeftMouseDownMask | NSRightMouseDownMask | NSOtherMouseDownMask)
                                           handler:^(NSEvent *event){

        NSLog(@"theEvent->%@",event);

        //here you will receive the all mouse DOWN events
        if (event.modifierFlags & NSCommandKeyMask)
        {
          NSLog(@"theEvent1->%@",event);
        }else{
          NSLog(@"theEvent2->%@",event);
        }

    }];

    return self;
}
Sergey Neskoromny
  • 1,188
  • 8
  • 15
  • Any way read the Cocoa Event Handling Guide: link:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/EventOverview/Introduction/Introduction.html#//apple_ref/doc/uid/10000060i – Sergey Neskoromny May 10 '14 at 09:29
  • Is it possible to do something like this, to detect if command key and mouse button is down: NSLeftMouseDownMask & NSCommandKeyMask? – Freddy May 10 '14 at 09:39
  • No! you have to check inside the handler: mouseEventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:((NSLeftMouseDownMask | NSRightMouseDownMask | NSOtherMouseDownMask)) handler:^(NSEvent *event){ if (event.modifierFlags & NSCommandKeyMask) { NSLog(@"theEvent1->%@",event); }else{ NSLog(@"theEvent2->%@",event); } }]; – Sergey Neskoromny May 10 '14 at 09:55
  • maybe you can help me with this question too: [link](http://stackoverflow.com/questions/23581908/how-can-i-freeze-all-interaction-with-other-windows) – Freddy May 10 '14 at 13:57
  • 1
    Hey again. It seems like your code sometimes doesn't work. however it works 80 % of the time. Any ideas what could be the cause? – Freddy May 18 '14 at 15:29
2

Cocoa Event Monitors are the way to go.
You can use them to track pretty much every kind of mouse event outside of your view hierarchy.

Check out the documentation in the Cocoa Event Handling Guide for information on how to use them and similar topics here on SO like

Community
  • 1
  • 1
Jay
  • 6,572
  • 3
  • 37
  • 65
0

Although you mention mouseDown in your question and have accepted an answer that describes how to set up a monitor, as far as I can tell from the rest of the question, you don't actually need an event that fires when the mouse button is pressed, but instead just want to check if a mouse button is currently pressed.

NSEvent has a class property you can use for this:

[NSEvent pressedMouseButtons]

This returns the indices of the currently pressed mouse buttons:

A return value of 1 << 0 corresponds to the left mouse button, 1 << 1 corresponds to the right mouse button, 1<< n, n >=2 correspond to other mouse buttons.

Rich
  • 7,348
  • 4
  • 34
  • 54