[Cocoa] keyDown Event Not Working.

Discussion in 'Mac Programming' started by zmagi, Dec 1, 2018.

  1. zmagi macrumors newbie

    Joined:
    Nov 29, 2018
    #1
    Using Xcode 4.6.3 (4H1503)

    - (BOOL)acceptsFirstResponder {
    return YES;
    }
    -(void)keyDown: (NSEvent *)theEvent
    {
    if ([[theEvent characters] isEqualToString: @"f"]) {
    NSLog(@"###");
    }
    }
     
  2. robvas macrumors 68030

    Joined:
    Mar 29, 2009
    Location:
    USA
    #2
    There is nothing wrong with that code. Can you post your whole project? Something else is causing the problem.
     
  3. zmagi thread starter macrumors newbie

    Joined:
    Nov 29, 2018
    #3
    The NSEvent is not firing and the functions for drag does not even respond on file drag and drop.
     

    Attached Files:

  4. Senor Cuete macrumors 6502

    Joined:
    Nov 9, 2011
    #4
    Be sure that the object in the .xib is properly connected to the code as the file's owner.
     
  5. zmagi thread starter macrumors newbie

    Joined:
    Nov 29, 2018
    #5
    I posted the xcodeproj, go ahead take look at it.
     
  6. robvas macrumors 68030

    Joined:
    Mar 29, 2009
    Location:
    USA
    #6
    ApplicationDelegate is not a subclass of NSResponder, you may implement keyDown: method in it, but it will not get invoked when you press keys.

    Even in a small app you want to use a controller subclass instead of dumping functionality in the app delegate.

    So, you need to create a controller/subview and place it in your window and do the key handling there.

    See if you can find the book 'Cocoa Programming for OS X' by Aaron Hillegass
    There is a chapter on the keyboard events. I've attached the example project for your review.
     

    Attached Files:

Share This Page

5 December 1, 2018