PopupMenu + alt key = hidden choices

I’m trying to find a way to enable/refresh hidden items in my popup menu that are triggered via the ‘alt’ key.
I’ve got a timer running that checks the desktop main mouse source’s modifiers.
if ‘alt’ is pressed, it refreshes the MenuBarComponent.

It works pretty well if I hold down ‘alt’ BEFORE opening the popup menu in the menu bar.
But if a menu is already open, it has no effect. Any ideas on how to get these hidden menu choices to appear after the menu has already appeared? A common example is when you open the Window menu for any app on OS X, and when you press ‘Alt’, the Minimize/Zoom choices changes:


ah, it doesn’t work if the menu is open, because this doesn’t refresh the menu if the menu names don’t change:

void MenuBarComponent::menuBarItemsChanged (MenuBarModel* /*menuBarModel*/)
    StringArray newNames;

    if (model != nullptr)
        newNames = model->getMenuBarNames();

    if (newNames != menuNames)
        menuNames = newNames;

Is there a way to refresh the active popup menu when a keypress is detected?

1 Like

I just hacked this together really quick, it has bugs (don’t discard the menu :smiley: ) and is just a quick and dirty example but this does what you want.

Click anywhere, a popup will open, use the Alt key to toggle options


This can’t be used with a MenuBarComponent.

What I need is

int MenuBarComponent::getCurrentPopupIndex() const { return currentPopupIndex; }

which doesn’t exist.

I got it working when I added that function to MenuBarComponent though:

That’s via this:

void MenuBarUpdater::timerCallback()
    if( mbc != nullptr )
        auto& desktop = Desktop::getInstance();
        bool isAltDown = desktop.getMainMouseSource().getCurrentModifiers().isAltDown();

        if( isAltDown != lastAltStatus )
            lastAltStatus = isAltDown;
            auto currentMenuIndex = mbc->getCurrentPopupIndex();
            if( isAltDown && currentMenuIndex >= 0 )
                activeMenuIndex = currentMenuIndex;

void MenuBarUpdater::refresh()
    mbc->showMenu(-1); //closes current menu
    DBG( "activeMenuIndex: " << activeMenuIndex );
    mbc->showMenu(activeMenuIndex); //forces current menu to be recreated.

and doing this in getMenuForIndex():

        if( Desktop::getInstance().getMainMouseSource().getCurrentModifiers().isAltDown() )

CC: @t0m @jules I’ll be submitting a pull request for that MenuBarComponent index getter.

Please don’t tag the JUCE team in your posts (we’ve asked you this before) - we read everything on the forum anyway.