View accepts first responder but KeyDown event beeps & is not triggered

Discussion in 'Mac Programming' started by claired, Nov 4, 2008.

  1. claired macrumors newbie

    Joined:
    Nov 4, 2008
    #1
    I cannot seem to get my keyDown event to work although my view is set to accept first responder.
    When any a key is pressed a beep is sounded.
    If I put a breakpoint on the keyDown method, it is never reached and nothing is logged for the event in the debugger window.

    I have searched this site and there was a similar problem but it looked like the developer had not set his view to accept first responder.

    Please see the code in question below. Any help would be greatly appreciated.


    #pragma mark EVENTS

    - (BOOL)acceptsFirstResponder
    {
    return YES;
    }

    - (BOOL)becomeFirstResponder
    {
    return YES;
    }

    - (void)keyDown:(NSEvent *)theEvent
    {
    if ([theEvent modifierFlags] & NSNumericPadKeyMask)
    {
    // arrow keys have this mask
    NSString *theArrow = [theEvent charactersIgnoringModifiers];
    unichar keyChar = 0;

    if ( [theArrow length] == 0 )return;
    // reject dead keys

    if ( [theArrow length] == 1 )
    {
    keyChar = [theArrow characterAtIndex:0];

    if ( keyChar == NSLeftArrowFunctionKey )
    {
    [self showThePreviousImage];
    return;
    }
    if ( keyChar == NSRightArrowFunctionKey )
    {
    [self showTheNextImage];
    return;
    }


    [super keyDown:theEvent];
    }
    }
    [super keyDown:theEvent];
    }
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Your code works fine for me. I'm not sure the issue, but make sure you don't have a subview within that view that can become first responder.
     
  3. claired thread starter macrumors newbie

    Joined:
    Nov 4, 2008
    #3
    Thanks for getting back to me and testing out the code.

    I have a window that contains an IKImageView. This is a real newbie question, but would you class that as a sub-view?
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
  5. claired thread starter macrumors newbie

    Joined:
    Nov 4, 2008
    #5
    Sorry for the delay in reply.

    Thanks again for your help.
    I had a hunch that would be the answer.
    Not sure what else to check/change to get the cursor keys working.
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    Feel free to post your project here, or you could PM me and I could take a look.
     
  7. claired thread starter macrumors newbie

    Joined:
    Nov 4, 2008
    #7
    Again, sorry for taking so long to get back to you.

    Thanks for your reply.
    I have managed to get it to accept a key press. It was a newbie mistake!
    The Window needed the class changing to use the App Controller.

    Once again, thank you for all of your help.
     

Share This Page