Custom shortcuts in Script Editor

Hi,

Is there a way to change the shortcuts in the script editor ?

I would like to change the Ctrl+Shift+R to something else because I find it quite difficult to do with one hand only (it makes my wrist hurt :woozy_face: :face_with_head_bandage:)
image

cc @ctrueden

Thank you very much

A post was split to a new topic: Ctrl+I in Script Editor

@LPUoO Unfortunately, all the Script Editor keyboard shortcuts are hard-coded. But you can hack it. Create the following file in your Fiji.app/plugins/Scripts/Plugins/AutoRun folder:

Hack_Script_Editor_Shortcuts.js
// Define menu items whose shortcuts we want to override.
var keyMap = new java.util.HashMap();
keyMap.put("Run selected code", "ctrl alt P");
// ... define more shortcut overrides here ...
// For shortcut syntax, see here:
// https://javadoc.scijava.org/Java/javax/swing/KeyStroke.html#getKeyStroke-java.lang.String-

function hackTextEditor(evt) {
	if (evt.getID() != java.awt.event.WindowEvent.WINDOW_OPENED) return;
	var w = evt.getWindow();
	if (!(w instanceof org.scijava.ui.swing.script.TextEditor)) return;

	// A Script Editor window just opened! Walk its menus.
	for (var m = 0; m < w.getJMenuBar().getMenuCount(); m++) {
		var menu = w.getJMenuBar().getMenu(m);
		for (var i = 0; i < menu.getItemCount(); i++) {
			var item = menu.getItem(i);
			if (item == null) continue;
			var label = item.getLabel();
			if (keyMap.containsKey(label)) {
				item.setAccelerator(javax.swing.KeyStroke.getKeyStroke(keyMap.get(label)));
			}
		}
	}
}
// Register our shortcut hacker with the AWT subsystem.
java.awt.Toolkit.getDefaultToolkit().addAWTEventListener(hackTextEditor, java.awt.AWTEvent.WINDOW_EVENT_MASK);

This code installs a listener when Fiji starts up that responds to new Script Editor windows opening, scanning for matching commands and overriding their shortcuts as desired.

2 Likes