Here is a notepad-like demo that allows you to activate menus with Alt-letter combinations. It’s Windows XP, haven’t tested recently on Linux.
With juce 1.4.6, there was, IIRC, a bug that caused the first Alt-letter press to fail but when pressing two times, it worked.
This was fixed in august or so, but now it’s gone bad again. With newer versions, it doesn’t work - a short flash is shown but no menu. It works with a version I think is from 2008-11-17.
The sample I provide here isn’t tidy but it’s reasonable short.
Compile and start on a MS Windows box, then press Alt+C or Alt+S with the focus in the text window. This should activate the menus - and does with slightly older juce trunk, but unfortunately not on the newest.
I tried to debug but I don’t understand what’s going on, it seems the modal show is cancelled by a WM_SYSCOMMAND… then, there are timers involved which makes debugging difficult.
#include <juce.h>
class MyJUCEApp
: public juce::JUCEApplication, public juce::KeyListener
{
class MenuWindow : public juce::MenuBarModel, public juce::DocumentWindow
{
public:
MenuWindow() : DocumentWindow("JuceMenOops", juce::Colour(200, 200, 100), 0)
{
setMenuBar(this);
}
const juce::StringArray getMenuBarNames()
{
juce::StringArray names;
names.add(T("Smoking"));
names.add(T("Cigarettes"));
return names;
}
const juce::PopupMenu getMenuForIndex(int index, const juce::String &name)
{
juce::PopupMenu menu;
menu.addItem(1, "Quit");
menu.addItem(2, "Cough");
return menu;
}
void menuItemSelected(int item, int)
{
if (item == 1) JUCEApplication::getInstance()->quit();
}
void showPopupMenu(int index)
{
for (int i = 0; i < getNumChildComponents(); i++)
{
if (MenuBarComponent *menuBar = dynamic_cast<MenuBarComponent *>(getChildComponent(i)))
{
menuBar->showMenu(index);
return;
}
}
}
juce_UseDebuggingNewOperator
};
MenuWindow *window;
juce::TextEditor *pane;
public:
MyJUCEApp() : window (0), pane(0) {}
void initialise (const String& commandLine)
{
window = new MenuWindow;
window->setBounds (100, 100, 400, 500);
pane = new juce::TextEditor();
pane->setMultiLine(true);
pane->setText("Writing something nasty on the wall...");
window->setContentComponent(pane);
window->setVisible(true);
pane->grabKeyboardFocus();
pane->addKeyListener(this);
}
void shutdown()
{
delete window;
}
const String getApplicationName()
{
return T("JuceMenOops");
}
const String getApplicationVersion()
{
return T("1.0");
}
bool keyPressed(const juce::KeyPress &slam, juce::Component *)
{
if (slam.getModifiers().isAltDown())
{
int index = 0;
switch (slam.getKeyCode())
{
case 'S':
index = 0;
break;
case 'C':
index = 1;
break;
default:
return false;
}
window->showPopupMenu(index);
}
return false;
}
juce_UseDebuggingNewOperator
};
START_JUCE_APPLICATION (MyJUCEApp)