Mac beep sound with menu items and keyboard shortcuts


#1

I’m having some issues with the beep / alert sound on Mac, I mean the sound you get in Finder when you press a key shortcut that doesn’t do anything. The sound plays with some menu items in my application (doesn’t play with others), which seems to depend on the position / index of the menu item. It also plays when I forward key events from my main window to my MidiKeyboardComponent like so:

bool MyMainWindow::keyStateChanged(bool isKeyDown) {
    return midiKeyboardComponent->keyStateChanged(isKeyDown);
}

Even if I return true from my keyStateChanged, the sound still plays. But only if the forwarding actually happens, not when the MidiKeyboardComponent has keyboard focus.

My question is: When does Juce (or OSX) decide to play the sound? What does it depend on and what can I do to prevent it?


#2

How can I avoid the sound from getting played when pressing keys or clicking menu items?


#3

The beep is generated in LookAndFeel::playAlertSound() - if you never want any beeps, you can override that in your own default L+F to silence it completely.

Or just make sure that your top-level component consumes all the unwanted keypresses, because the beep is telling you that a keypress has been unused.


#4

Thanks for your help!


#5

Those beeps don’t seem to be generated in LookAndFeel::playAlertSound(). Here’s what I have:

class GlobalLookAndFeel : public juce::LookAndFeel_V3 {
public:
    void playAlertSound() override {}
};

I’m using this as the default LookAndFeel by calling setDefaultLookAndFeel. The beep is playing like before, and playAlertSound never gets called in the cases I described. I have other LookAndFeel classes, but they all inherit from GlobalLookAndFeel, and none of them overrides playAlertSound().

Consuming all the unwanted keypresses fixes the problem for keypresses, but not for menu items. How can I fix those?