4

I have a Cocoa application that captures keypresses through a custom view in the view hierarchy. This view implements the keyUp and keyDown methods, and the keypresses are received. Even so, Cocoa still insists on playing the system error sound/ding every time I press a key. Any solutions?

Note: Although I tried to make this view first responder, it didn't work. That may have something to do with it.

Linuxios
  • 34,849
  • 13
  • 91
  • 116

1 Answers1

8

If you have unsuccessfully tried to make the view the first responder, it's most likely because NSView returns NO for acceptsFirstResponder. You can have your NSView subclass override acceptsFirstResponder to return YES:

- (BOOL)acceptsFirstResponder {
    return YES;
}

That should eliminate the beeps. Alternatively, you could have the NSView subclass override NSResponder's performKeyEquivalent: method to return YES, which should also eliminate the NSBeeps:

- (BOOL)performKeyEquivalent:(NSEvent *)event {
    return YES;
}

UPDATE:

Not sure what to suggest. I actually wrote a "Keyboard Cleaner Helper" app that's designed to basically do something similar to what you want. (I used it on my laptop when I wanted to clean the keyboard and didn't the hundreds of key presses to randomly rename files or result in repeated error beeps).

Sample project: http://www.markdouma.com/developer/KeyboardCleanerHelper.zip

Running that app, I can't get it to beep at all (notice calls are logged to Console).

NSGod
  • 22,699
  • 3
  • 58
  • 66
  • It's `acceptsFirstResponder` now? Wow. I'm not at my Mac right now, but I'll try it and accept. Thanks for the answer to this annoying problem. – Linuxios Nov 23 '12 at 02:02
  • @Linuxios: what do you mean by "It's `acceptsFirstResponder` now?" What did it used to be? Or are you maybe referring to the method you were using to try to make the view the first responder? – NSGod Nov 23 '12 at 03:23
  • I thought it was `canBecomeFirstResponder`. I'm at my Mac now, I'll give it a try. – Linuxios Nov 23 '12 at 14:16
  • No cigar I've done both, and neither works. CNN you think of anything else? – Linuxios Nov 23 '12 at 14:19
  • By the way, I got it to work by adding a NSTextView to my interface. Bizarre. – Linuxios Nov 24 '12 at 15:39
  • - (BOOL)performKeyEquivalent:(NSEvent *)event <- This works for me. Thanks you. – Tommy Mar 20 '16 at 04:53