3

I'm trying to add Accelerators to some scenes, but I would like a simple one-key accelerator in some instances. I am only familiar with KeyCodeCombination, and it doesn't seem to allow me to use no modifier.

Curiously, I have used the modifier + ANY (e.g. SHIFT_ANY), but it doesn't seem to work all the time (for my F10 accelerator it works with the shift key up OR down, but for my F2 accelerator it only works with the shift key DOWN).

Is there a way to make just a single key accelerator with KeyCodeCombination? Or do I have to use something else? or can someone tell me why my F2 key combination with SHIFT_ANY does not work with the shift key UP?

Here is my accelerator code:

    KeyCodeCombination f2 = new KeyCodeCombination(KeyCode.F2, KeyCombination.SHIFT_ANY);
    KeyCodeCombination f10 = new KeyCodeCombination(KeyCode.F10, KeyCombination.SHIFT_ANY);

    thisScene.getAccelerators().put(f2, new Runnable() {

        @Override
        public void run() {
            buttonCancel.fire();
            System.out.println("F2 pressed");
        }
    });
    thisScene.getAccelerators().put(f10, new Runnable() {

        @Override
        public void run() {
            buttonSave.fire();
            System.out.println("F10 pressed");
        }
    });
John
  • 165
  • 2
  • 3
  • 15

1 Answers1

1

Is there a way to make just a single key accelerator with KeyCodeCombination?

Yes. Just use the KeyCodeCombination constructor and don't pass any modifiers. For example, as in James_D's comment:

new KeyCodeCombination(KeyCode.F2)

can someone tell me why my F2 key combination with SHIFT_ANY does not work with the shift key UP?

Nope, on my keyboard, the accelerator for F2+SHIFT_ANY fires when F2 is pressed with the shift key UP (desktop, US, Win 7, Java 8b129).

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • OMG, I SWEAR I tried that (KeyCode.F2) and got a compile error before, but I did it now and it works fine. As for the SHIFT_ANY problem, the company is still using Java 7. But I really think you (jewelsea) must be some sort of wizard, because your answers always fix my problems on the first try. – John Mar 27 '14 at 23:31