1

I'm creating a program which lets the user define buttons, which then will fire notes to the synthesizer. Make your own, custom keyboard of sorts.

Now this keyboard should also react to global, custom hotkeys.

And here lies the problem: To make them global, I attach them to the scene, but then every newly set hotkey overwrites the previous ones. Have some code:

        btn.getScene().setOnKeyPressed((KeyEvent e) -> {
            if (e.getText().equals(hotkey)) {
                btn.arm();
                btn.fire();
            }
        });
        btn.getScene().setOnKeyReleased((KeyEvent e) -> {
            if (e.getText().equals(hotkey)) {
                btn.disarm();
            }
        });

The immediate solution that comes to mind is creating an arraylist that keeps track of all the hotkeys, and have the keylistener iterate over it every time, and another arraylist of arraylists with the associating buttons.

Is there maybe a more elegant, direct way?

Anarc
  • 53
  • 1
  • 6
  • This is pretty close to: [Using JavaFX 2.2 Mnemonic](http://stackoverflow.com/questions/12710468/using-javafx-2-2-mnemonic) which talks about accelerators. Slight difference is that you are tracking press/release events rather than typed keys as used in accelerators. – jewelsea Dec 08 '16 at 08:52
  • @jewelsea I came across that one, but I want the user to be able to hold down the key for a longer note – Anarc Dec 08 '16 at 15:15

1 Answers1

2

Using two array lists to track hotkeys and associated buttons will eventually lead you to a convoluted solution where you have to implement a way to associate the hotkeys and the buttons.

Based on this, it sounds like using a Map would fix your problems. A key value pair with the specific hotkey mapped to the associated button.

Check out the java documentation on Map for all the methods:

https://docs.oracle.com/javase/8/docs/api/java/util/Map.html

Just choose which implementation would best fit your needs.

Migsarmiento
  • 153
  • 6
  • Probably this should be the `Map` implementation of choice: https://docs.oracle.com/javase/8/docs/api/java/util/EnumMap.html, using `KeyCode` as key. – fabian Dec 08 '16 at 11:12