How to stop listening for NSEvents?
Asked Answered
T

2

7

I have a problem with listening for events, I can listen for events which works perfectly however I can't make it stop listening to events. I researched it for a while and came up with the a method, + (void)removeMonitor:(id)eventMonitor, that it says I should use when I'm done with the listener

But when I try to use the method, like so

[NSEvent addGlobalMonitorForEventsMatchingMask:(NSLeftMouseDownMask|NSKeyDownMask) handler:^(NSEvent *event) {
    [NSEvent removeMonitor:event];
}];

I keep getting an error of "-[NSEvent invalidate]: unrecognized selector sent to instance" Which I researched as well, and I believe it means that I'm overwriting a memory that is being used. However I don't know how to solve this problem. Any suggestions, or help is greatly appreciated!

UPDATE Thanks to JWWalker, Samir and Abizern, it now works

//I made a global variable called eventHAndler

.h file

id eventHAndler

.m file

eventHAndler = [NSEvent addGlobalMonitorForEventsMatchingMask:(NSLeftMouseDownMask|NSKeyDownMask) handler:^(NSEvent *event){
///code 
}];

/// created another method called stop. When called it stops the eventHAndler
- (IBAction)Stop:(id)sender 
{
    stop = 1;
    NSLog(@"inside stop method");
    [NSEvent removeMonitor:eventHAndler];
}
Trimer answered 19/8, 2012 at 23:28 Comment(0)
A
21

You're passing the wrong thing to removeMonitor:. The call to +[NSEvent addGlobalMonitorForEventsMatchingMask: handler:] returns a value called an event handler object. That's what can be passed to removeMonitor:.

Annadiana answered 28/8, 2012 at 2:2 Comment(6)
I'm sorry I'm still kinda confused. How do I get the returned value for +[NSEvent addGlobalMonitorForEventsMatchingMask: handler:^(NSEvent *event)]?Trimer
@A.sharif, if you mean how do you get it in the block that handles the event, then frankly I don't know, I've never used blocks.Annadiana
JWWalker is right. Something like : id eventHAndler = [NSEvent addGlobalMonitorForEventsMatchingMask: handler:]; .... [NSEvent removeHandler:eventHAndler] Make sure you remove the handler only once, otherwise you may encounter memory issues.Buckwheat
That doesn't work, and it doesn't even initialize the keyDownMask and the MouseDownMask. I run this method [NSEvent addGlobalMonitorForEventsMatchingMask:(NSLeftMouseDownMask|NSKeyDownMask) handler:^(NSEvent *event) { ///bunch of code }]; It runs and executes code depending on what the user presses. My problem is, that the method keeps running no matter what. Lets say the user wants to stop the program, and he hits the stop button, that should send a stop instruction to the addGlobalMonitorForEvents method. Isn't there a simple stop instruction for this method?Trimer
Dude, whats the correct syntax for the answer then? I can only guess!Highboy
@Boggartfly, @A.sharif, I think if you said __block id eventHandler = [NSEvent ... then the event handler would be accessible inside the handler block.Annadiana
G
-1

According to: http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/EventOverview/MonitoringEvents/MonitoringEvents.html

They say:

A global event monitor looks for user-input events dispatched to applications other than the one in which it is installed. The monitor cannot modify an event or prevent its normal delivery. And it may only monitor key events if accessibility is enabled or if the application is trusted for accessibility.

So it is not possible says the man himself :P

Gasolier answered 28/8, 2012 at 0:25 Comment(4)
I'm not trying to stop the action from happening. I'm trying to stop listening to it. Essentially My problem is, my program goes into a loop that just listens. I want to leave that loop. The comment you quoted is talking about altering what the user inputs, which is not what I'm trying to do.Trimer
If your just trying to stop listening to it why not just use a simple BOOL or something similar and check if it is yes or no, and do whats appropriate.Gasolier
I can't get out of the loop with a return or a break statement, so a Boolean statement wouldn't help me unless I knew how to break out of the loop.Trimer
I am not too familiar in this area so if you want me to delete my post just let me know. The other answerer seems to know what he's talking about. :)Gasolier

© 2022 - 2024 — McMap. All rights reserved.