Skip to content, Skip to search


Jython Scripting

28 bytes added, 17:27, 19 October 2011
Add a key listener to the canvas of every open image
def doSomething(imp, keyEvent):
""" A function to react to key being pressed on an image canvas. """ IJ.log("clicked keyCode " + str(keyEvent.getKeyCode()) + " on image " + str(imp)) # Prevent further propagation of the key event: keyEvent.consume()
class ListenToKey(KeyAdapter):
def keyPressed(this, event): imp = event.getSource().getImage() doSomething(imp, event)
listener = ListenToKey()
for imp in map(WindowManager.getImage, WindowManager.getIDList()):
win = imp.getWindow() if win is None: continue canvas = win.getCanvas() # Remove existing key listeners kls = canvas.getKeyListeners() map(canvas.removeKeyListener, kls) # Add our key listener canvas.addKeyListener(listener) # ReOptionally re-add existing key listeners # map(canvas.addKeyListener, kls)
Emailconfirmed, uploaders