Hide
IJ2 is out
 Sec. 34: Key Modifiers Up Part VI: Keyboard Shortcuts Credits & References 

35 Toolbar Shortcuts

Keyboard shortcuts cannot be used directly to activate tools in the ImageJ Toolbar↑ (with the exception of the Magnifying Glass↑ and the Scrolling Tool↑). However, shortcuts can be assigned to macros that use the setTool() macro function.
The set of macros listed below (taken from ToolShortcuts) exemplify how to assign the function keys F1 through F12 to some of the most commonly used Tools↑. Once copied to the ImageJ/macros/StartupMacros.txt file, they will be automatically installed at startup.
Assigning Keyboard Shortcuts to ImageJ Tools
/* These macros allow tools to be selected by pressing function keys.
   Add them to ImageJ/macros/StartupMacros.txt and they will be automatically installed when ImageJ starts.
*/
​
 macro "Rectangle [f1]" {setTool("rectangle")}
 macro "Elliptical [f2]" {setTool("elliptical")}
 macro "Brush [f3]" {setTool("brush")}
 macro "Polygon [f4]" {setTool("polygon")}
 macro "Freehand [f5]" {setTool("freehand")}
 macro "Straight Line [f6]" {setTool("line")}
 macro "Segmented Line [f7]" {setTool("polyline")}
 macro "Arrow [f8]" {setTool("arrow")}
 macro "Angle [f9]" {setTool("angle")}
 macro "Multi-point [f10]" {setTool("multipoint")}
 macro "Wand [f11]" {setTool("wand")}
 macro "Magnifying Glass [f12]" {setTool("zoom")}
This approach, however, requires the user to memorize a large number of shortcuts. In addition, it may be difficult to assign so many hot-keys without conflicting with previously defined ones (see PluginsShortcuts). An alternative way to control the toolbar using the keyboard is to create macros that progressively activate tools from a predefined sequence. The next example demonstrates such strategy. It is composed of two macros activated by F1 and F2 that iterate through the toolbar from left to right (forward cycle) and right to left (reverse cycle).
Cycling Through the Toolbar Using Keyboard Shortcuts
/* These two macros loop through the tools listed in an array using "F1" and "F2"
   as keyboard shortcuts (forward and reverse cycling).
*/
​
 var index;
 var tools = newArray("rectangle", "roundrect", "oval", "ellipse", "brush", "polygon", "freehand", "line",
     "freeline", "polyline", "arrow", "wand", "dropper", "angle", "point", "multipoint", "text");
​
 macro "Cycle Tools Fwd [F1]" {
  setTool(tools[index++]);
  if (index==tools.length) index = 0; 
 } 
   
 macro "Cycle Tools Rwd [F2]" {
  if (index<0) index = tools.length-1;
  setTool(tools[index--]);
 }
A tool can be defined either by its name or by its position in the toolbar using setTool(id), which allows assigning keyboard shortcuts to Custom Tools↑ and items loaded by the More Tools Menu↑ (e.g., setTool(21); activates whatever tool has been installed on the last slot of the toolbar).
It is also possible to temporarily activate a tool. The macro below (taken from the Rename and Save ROI Sets toolset), activates the Color Picker Tool↑ when F3 is pressed, but restores the previously active tool as soon as the mouse is released.
Temporary Activation of a Tool
 macro "Pick Color Once [F3]" {
   tool = IJ.getToolName; 
   setTool("dropper");
   while (true) {
       getCursorLoc(x, y, z, flags);
       if (flags&16!=0)
           { setTool(tool); exit; }
   }
 }
 Sec. 34: Key Modifiers Up Part VI: Keyboard Shortcuts Credits & References