PDA

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




claired
Nov 4, 2008, 10:55 AM
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];
}



kainjow
Nov 6, 2008, 08:19 AM
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.

claired
Nov 6, 2008, 08:54 AM
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?

kainjow
Nov 6, 2008, 02:02 PM
Not in the context I was referring to.

claired
Nov 13, 2008, 10:13 AM
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.

kainjow
Nov 13, 2008, 02:34 PM
Feel free to post your project here, or you could PM me and I could take a look.

claired
Nov 26, 2008, 10:38 AM
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.