ImageJ paint brush tool keyboard shortcut

ImageJ is easy to modify if you know a bit of Java. I don’t know any Java. But here’s what I did to add keyboard shortcuts for the paint brush tool and the line selection tool. The code is based off this help file from the ImageJ website: http://rsbweb.nih.gov/ij/docs/guide/userguide-32.html

The first step was to figure out to refer to the line tool and paint brush tools. I did this by going to Plugins>Macros>Record and clicking on the line tool and the paint brush tool in succession. The numeric codes for those tools were revealed in the macro recording window as setTool(4) and setTool(18).

Use the record macro function to see what ImageJ calls internally when you click on a tool button.
Clicking on the Straight Line tool and the Paintbrush tool showed the two commands ImageJ issued to select those tools.

 

The next step is to load up the StartupMacros.txt file that contains all of the macros that ImageJ loads on startup. Go to Plugins>Macros>Edit… to open the editor window. The StartupMacros.txt file should be in the macros folder in the ImageJ directory.

Edit the StartupMacros.txt file

In the editor window, add the following code to the end of the file:


//Activate line tool by pressing lower case L button
macro "Line Tool [l]" {setTool(4);}


//Activate brush tool by pressing b button
macro "Paintbrush Toggle [b]" {
setTool(18);
while (true) {
getCursorLoc(x, y, z, flags);
if (flags&alt!=0)
setColorToBackgound();
draw(brushWidth);
}
}

The first macro command activates the Straight Line tool when you press the l (lower case L) button. The second longer macro activates the Paintbrush tool when you press the b button (emulating Photoshop’s brush keyboard shortcut, which was my whole motivation for making this change). Save the edited StartupMacros.txt in the ImageJ macros folder (the same place you found it). You can then load up the modified StartupMacros file by going to Plugins>Macros>Install… and choosing StartupMacros.txt. The StartupMacros.txt file is also loaded on startup by ImageJ, so you should have access to these keyboard shortcuts every time you start ImageJ in the future.