Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Key down event in an NSView controller

I'm trying to find a solution that allows me to get keydown events in a view controller. I do not believe a view controller is part of the responder chain by default.

I would appreciate a sample of how to go about this. I have had trouble finding documentation I can understand on how to add the VC to the responder chain and get the events.

Thanks.

Miek

like image 699
Miek Avatar asked Mar 22 '23 00:03

Miek


1 Answers

You can implement something like this:

-(void) globalKeyDown: (NSNotification *) notification 

method in your controller class, and then just add the observer in awakeFromNib...or loadView method of your controller

- (void)awakeFromNib
{
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(globalKeyDown:)
                                                 name:@"my_keyEvent" 
                                               object:nil];
}

in your view class

-(void)keyDown:(NSEvent *)theEvent
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"my_keyEvent"
                                                    object:theEvent
                                                  userInfo:@{@"sender":self}];
}
like image 70
Eugene Gordin Avatar answered Apr 06 '23 10:04

Eugene Gordin