Is it possible to have a KeyCodeCombination with no modifier? or do I need to use something else?

StackOverflow https://stackoverflow.com/questions/22697051

  •  22-06-2023
  •  | 
  •  

Question

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");
        }
    });
Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top