JavaFX KeyCodeCombinations in Clojure

I’ve been experimenting with adding keyboard accelerators to some of the Clojure programs I’ve written with JavaFX-based user interfaces. As part of that investigation, I tried to translate the Java program here to Clojure. The program just puts up a window with a menu bar containing only a “File“ menu which itself contains one item, “Exit“. Most programs provide a keyboard shortcut or accelerator to close the program with a Ctrl-X (on Windows). Figuring out how to add that functionality was a bit of an issue for me.

It turns out that the constructor for a KeyCodeCombination accepts a variable length argument array. In a few cases, you can get lucky and simply provide a Clojure vector of the arguments. This wasn’t one of those happy coincidences. Instead, you need to use the canonical method of packing all of those arguments into a Java array using Clojure’s into-array function. You need to do it even if you have only one argument, as was the case for me.. That isn’t usually so bad, but this time I had a little trouble figuring out the type of array to create. Turns out you have to specify that the list of modifiers expected by the constructor are KeyCombination.Modifier arguments in Java are KeyCombination$Modifier in Clojure.