Key Events

Discussion in 'Mac Programming' started by Binju, Mar 31, 2010.

  1. Binju macrumors member

    Joined:
    Jan 31, 2010
    #1
    Code:
    - (void) keyDown:(NSEvent *)theEvent
    {
    }
    is not responding.Can anyone tell me the reason?
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    I wrote the method

    Code:
    - (void) readBinjusMind (NSEvent *) theEvent
    {
    }
    
    but it is not responding.

    Your method will (a) not react to the keyDown event in any way and (b) prevent it from being passed to the next responder. Where exactly did you add this method? How do you know that it is "not responding"? What did you expect to happen in which situation?
     
  3. Binju thread starter macrumors member

    Joined:
    Jan 31, 2010
    #3
    Code:
    - (void)keyDown:(NSEvent *)event
    
    {
    
     NSLog(@"Key down");
    
    }
    
    The above code should respond to me by giving "Key down" message when pressing any key in my keyboard know?

    But it does not display the message when pressing the key.
     
  4. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #4
    It seems likely you haven't also added the following:

    Code:
    -(BOOL)acceptsFirstResponder
    {
        [[self window] makeFirstResponder:self];
        return YES;
    }
    
     
  5. Binju thread starter macrumors member

    Joined:
    Jan 31, 2010
  6. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #6
    As gnasher729 said, where did you put this code? In what object is it a method?
     
  7. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #7
    I've been through his posting history and I can't see where he EVER answered a question.

    He only asks them!
     
  8. Binju thread starter macrumors member

    Joined:
    Jan 31, 2010
    #8
    Code:
    //  KitplayerAppDelegate.m
    #import "KitplayerAppDelegate.h"
    #import "QTKit/QTKit.h"
    #import "QuickTime/Movies.h"
    #import "Appkit/NSResponder.h"
    
    
    @implementation KitplayerAppDelegate
    
    @synthesize window;
    
    
    
    static const unichar kESCKey = 27; 
    -(BOOL)acceptsFirstResponder
    {
    	NSLog(@"First responder");
    	 [[mMovieView window] makeFirstResponder:mMovieView];
        return YES;
    }
    
    - (void)keyDown:(NSEvent *)event
    {
        NSLog(@"key down");
    	unichar keyPressed = [[event charactersIgnoringModifiers] characterAtIndex:0];
    	
        if( keyPressed == kESCKey )
    	{
    		
    		
    		if( [mMovieView isInFullScreenMode] )
    		{
    			[mMovieView exitFullScreenModeWithOptions:fullScreenOptions];
    		} 
    		
    		
    	} 
    
    }
    

    I placed a quick time movie view (mMovieView)in my window. While the movie is playing in full screen mode, I want to exit the fullscreen mode by pressing esc key .
     
  9. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #9
    Thank you for answering the question.

    You need to add them to the View subclasses not the Application Delegate.
     
  10. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #10
    Thanks for finally answering the very first question that I asked. Listen, when people ask you questions, they do it for a reason. Here, your answer means I don't have to read your mind, because the problem is obvious:

    You added code so that your application delegate could become first responder if it is asked to be one, and code so that your application delegate would stop a running movie when it processes an escape key. The problem is that your application delegate will _never_ be asked to become first responder, because views can become first responder, not applications, and it will _never_ be asked to process a key because the actual first responder will handle the key first.

    If you want your movie view to have the ability to stop a movie playing, that's the job of the movie view. Add code there.
     
  11. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #11
    I believe that is not strictly true. If you look at NSView, it has no methods for interacting with the NSEvent, the event methods derive from NSView's first superclass, NSResponder. Hence, I believe it could be possible to make the Application delegate handle keystrokes simply by making it a subclass of NSResponder.

    Try changing the line in the interface file to
    Code:
    @interface KitplayerAppDelegate : [COLOR="Blue"]NSResponder[/COLOR] { ...
    It is not really proper Cocoa form to do that, but I think it would work. If your application might have more than one window open, however, it would be better to subclass the movie view.
     
  12. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #12
    Now, as I read his posting history, he'll not acknowledge the assistance provided, but will start another thread shortly with another problem.
     
  13. Binju thread starter macrumors member

    Joined:
    Jan 31, 2010
    #13
    try
     
  14. Binju thread starter macrumors member

    Joined:
    Jan 31, 2010
    #14

    Not he man, she....;)
     
  15. Binju thread starter macrumors member

    Joined:
    Jan 31, 2010
    #15
    No luck ya
     
  16. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #16
    Whilst the above is true: any NSResponder can handle NSEvents this only works if the NSResponder is in the responder chain. By default views tend to get added to the responder chain when they are in a window that is key. I'm fairly sure that this is not true of the application delegate.

    You may need to add your NSResponder sub-class to the responder chain.

    I would read Event Architecture (specifically the The Path of Key Events).

    Note that NSApplication is a NSResponder. If you want to insert your delegate as the first object that gets to handle events then something like the below will work (say in the init of your delegate):

    Code:
    NSResponder *currentNextResponder  = [NSApp nextResponder];
    [NSApp setNextResponder:self];
    [self setNextResponder:currentNextResponder];
     
  17. Binju thread starter macrumors member

    Joined:
    Jan 31, 2010
    #17
    Code:
    -(id) init {
        self = [super init];
    	
        if ( self ) {
    		NSResponder *currentNextResponder  = [NSApp nextResponder];
    		[NSApp setNextResponder:self];
    		[self setNextResponder:currentNextResponder];  //Got error in this line 
        
    	}
    	
        return self;
    }
    
    
     
  18. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #18
    I didn't bother re-reading this thread, but ...

    I don't believe your application delegate inherits the NSResponder protocol thus the error, or warning (depending on flag to the compiler).
     
  19. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #19
    This is probably true. But it clearly should when key events are being captured.
     

Share This Page