Skip to content

Instantly share code, notes, and snippets.

@petebankhead
Last active October 14, 2022 07:39
Show Gist options
  • Save petebankhead/63a6d2e93b7ce704e57eefb6885010fa to your computer and use it in GitHub Desktop.
Save petebankhead/63a6d2e93b7ce704e57eefb6885010fa to your computer and use it in GitHub Desktop.
Set KeyCombination accelerators for menu items in QuPath v0.3.2
/**
* QuPath script to set accelerators for specific menu items.
*
* Written for QuPath v0.3.2
* In answer to https://forum.image.sc/t/feature-request-windows-with-favorite-menu-items/72721/3
*
* @author Pete Bankhead
*/
def combos = [
'Analyze>Density maps>Create density map': 'shortcut+p',
'Classify>Object classification>Create single measurement classifier': 'shortcut+shift+1'
]
// Set to the menu items
Platform.runLater {
for (def entry : combos.entrySet()) {
def mi = getQuPath().lookupMenuItem(entry.getKey())
def combo = javafx.scene.input.KeyCombination.keyCombination(entry.getValue())
// Bind accelerator to action if possible
def action = mi.getProperties().values().find(a -> a instanceof org.controlsfx.control.action.Action)
if (action != null) {
action.setAccelerator(combo)
// Register in QuPath (might not be needed)
getQuPath().registerAccelerator(action)
} else {
mi.acceleratorProperty().unbind() // Just in case
mi.setAccelerator(combo)
}
// Seems necessary in Windows to get it working
def menu = mi.getParentMenu()
def items = menu.getItems()
int ind = items.indexOf(mi)
items.remove(mi)
items.add(ind, mi)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment