I am trying to have one of my plug-ins for ImageJ draw a label onto an image by pressing enter a.k.a. KeyCode 10. I have used 'removeKeyListener(this)' to try and get rid of the current listeners for the ImageWindow and then 'addKeyListener(this)' with mixed results. I want the plug-in's users to be able to hit [Enter] instead of Control+B to finalize something written with the Text Tool onto the image. Does anyone know a good way to do that?
|
Hi Alex,
you probably need the add/removeKeyListener for the ImageCanvas, not for 'this'. 'this' refers to your plugin; having it as KeyListener makes sense only if it is a (displayed) object that can be in the foreground and thus receive keyboard events. Michael ________________________________________________________________ On Jun 5, 2012, at 16:21, alexwatson wrote: > I am trying to have one of my plug-ins for ImageJ draw a label onto an image > by pressing enter a.k.a. KeyCode 10. I have used 'removeKeyListener(this)' > to try and get rid of the current listeners for the ImageWindow and then > 'addKeyListener(this)' with mixed results. I want the plug-in's users to be > able to hit [Enter] instead of Control+B to finalize something written with > the Text Tool onto the image. Does anyone know a good way to do that? -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by alexwatson
If you really want to remove all the other key listeners, you need to get each of them using a = getKeyListeners(); and then remove each of them using removeKeyListener(a[i]), but that is usually not necessary, since the gui element will call all key listeners that have been attached to an object. Your own key listener could then look something like:
myObject.addKeyListener(new KeyListener() { public void keyPressed(KeyEvent key) { switch(key.getKeyCode()) { case KeyEvent.VK_ENTER: doFunction(); } } ....//other KeyListener methods }); removeKeyListener removes the object you pass in from the list of key listeners if it's currently in that list. Someone else on the list may also be able to guide you in writing a simple macro, I've also seen that work well too but I think you would have to choose a key other than enter for that approach, see: http://imagej.nih.gov/ij/developer/macro/macros.html#shortcuts -Justin -----Original Message----- From: Watson, David (NIH/NIEHS) [F] Sent: Tuesday, June 05, 2012 10:21 AM To: List IMAGEJ Subject: KeyListener I am trying to have one of my plug-ins for ImageJ draw a label onto an image by pressing enter a.k.a. KeyCode 10. I have used 'removeKeyListener(this)' to try and get rid of the current listeners for the ImageWindow and then 'addKeyListener(this)' with mixed results. I want the plug-in's users to be able to hit [Enter] instead of Control+B to finalize something written with the Text Tool onto the image. Does anyone know a good way to do that? -- View this message in context: http://imagej.1557.n6.nabble.com/KeyListener-tp4998901.html Sent from the ImageJ mailing list archive at Nabble.com. -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by alexwatson
Hi Alex,
On Tue, 5 Jun 2012, alexwatson wrote: > I am trying to have one of my plug-ins for ImageJ draw a label onto an > image by pressing enter a.k.a. KeyCode 10. I have used > 'removeKeyListener(this)' to try and get rid of the current listeners > for the ImageWindow and then 'addKeyListener(this)' with mixed results. > I want the plug-in's users to be able to hit [Enter] instead of > Control+B to finalize something written with the Text Tool onto the > image. Does anyone know a good way to do that? The AbstractTool framework in Fiji allows you to do stuff like that. Basically, you need to implement the KeyListener interface and consume only those events that you want to handle. The tool then needs to be registered by calling the run() method. A template for using the AbstractTool framework can be found here: http://fiji.sc/Bare_Tool.java (or by opening Fiji's Script Editor with File>New>Script and selecting the Bare Tool template from the Templates>Java menu.) Ciao, Johannes -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by Senseney, Justin (NIH/CIT) [E]
How can I create a TextCanvas that behaves normally, with the exception that instead of pressing [Control + D], users could hit [Enter] to draw it? I don't really want to have to re-compile TextCanvas or ImageJ. I just want to be able to do it from a plug-in.
|
Free forum by Nabble | Edit this page |