0

I have been trying to set up mnemonics on my buttons in a SWT window (Eclipse plugin), but unfortunately I always end up with the following cases:

  1. Either I use setText("&Cancel"): it doesn't show the mnemonics until you press the "alt" button
  2. Or I use setText("&&Cancel"), but the button looks like "&Cancel". This "double ampersand" trick was found on the Eclipse forum.

If you already faced the problem, I would be glad to read your solutions to fix this. Thanks for reading.

MedAl
  • 457
  • 3
  • 19
  • Well, isn't that how mnemonics are supposed to work? Don't show them until the user presses `Alt` ? On Windows you have to force-enable the underlining of mnemonics (cf http://stackoverflow.com/a/10164042) to make them show all the time. – Baz Jun 06 '16 at 13:52
  • Indeed, it seems like you are right. Then do you know how to programmatically force the underlining of the mnemonic character ? (For instance, Eclipse menus has the mnemonics showing up even if you don't press the Alt key) – MedAl Jun 06 '16 at 15:36

1 Answers1

0

Finally managed to find a solution. Here is how to simulate a ALT keystroke:

private void showMnemonics() {

    Event event = new Event();
    event.keyCode = SWT.ALT;
    event.type = SWT.KeyDown;
    Display.getDefault().post(event);

}

Edit : I guess the following code should be add to the method, in order to release the key :

event.type = SWT.KeyUp;
Display.getDefault().post(event);
MedAl
  • 457
  • 3
  • 19