Search code examples
cocoansviewnsresponder

Unwanted beep when a key is hit


I have a custom view that accepts key input, its inside a NSScrollView. I have set the acceptsFirstResponder to yes and it is accepting the keyDown successfully. But every time I hit a key, I hear a beep. I'm sure that there something else needed but can't remember what.. Please help.

-(void)keyUp:(NSEvent *)theEvent{

NSLog(@"is first responder %i", self.window.firstResponder == self);

switch (theEvent.keyCode) {
    case KeyCodeEnumBackspace:
    case KeyCodeEnumDelete:
    {
        if (self.scheduleControl.selectedEvent) {
            [self.scheduleControl deleteEvent:self.scheduleControl.selectedEvent];
        }
    }
        break;

    default:
        break;
}
   }

Solution

  • Got it. The beep occurs at keyDown, not at KeyUp. To remove the beep, I need to handle it, and an empty implementation will suffice. The key is not to pass it to super

    - (void)keyDown:(NSEvent *)theEvent {
    
    }
    
    - (void)keyUp:(NSEvent *)theEvent {
        switch (theEvent.keyCode) {
            case KeyCodeEnumBackspace:
            case KeyCodeEnumDelete:
                if (self.scheduleControl.selectedEvent) {
                    [self.scheduleControl deleteEvent:self.scheduleControl.selectedEvent];
                }
                break;
            default:
                break;
        }
    }