How JavaFX set shortcut keys for the button?

JavaFX There are many ways for the button to set the shortcut keys, the first that under the common one.

The first:

1 KeyCodeCombination kc1 = new KeyCodeCombination(KeyCode.W, KeyCodeCombination.CONTROL_DOWN);
2 Mnemonic mnemonic1 = new Mnemonic(b1, kc1);
3 scene.addMnemonic(mnemonic1);
Parsing: 
KeyCodeCombination
is an inherited KeyCombination object of this class KeyCodeCombination create, write, express keys keys in the constructor.

Mnemonic similar to what the listener binding, you create a listener you need to bind controls, new new a Mnemonic object on the line.

The second :( almost never used)

1 KeyCodeCombination kc1 = new KeyCodeCombination("W", KeyCodeCombination.CONTROL_DOWN);
2 Mnemonic mnemonic1 = new Mnemonic(b1, kc1);
3 scene.addMnemonic(mnemonic1);
Analysis: 
This method is a ratio of on, just change the KeyCodeCombination content constructor, also possible directly as a string.
 

The third :( recommended)

 

KeyCodeCombination kc1 = new KeyCodeCombination(KeyCode.W, KeyCodeCombination.CONTROL_DOWN);
        scene.getAccelerators().put(kc1, new Runnable() {
            
            @Override
            public void run() {
                System.out.println("事件");
            }
});
Analysis: 
This method is very flexible, but you will find that he did not and button bindings, that is, he can set global shortcut. If you need and button bindings, the method is very simple, the method call button on the line! (You can use the package method)
can () oh with b1.fire
Note: He is not to create a new thread.

Guess you like

Origin www.cnblogs.com/modulecode/p/12079607.html