Using keyboard with plugin

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Using keyboard with plugin

Richard Ansorge
Hi,

I am trying to develop a PluginFilter for simple Image Editor which uses
keyboard commands. I am reading the keyboard using the keyListener.java
example.

Everything works, EXCEPT most keystrokes are also captured as hotkeys by
other parts of ImageJ; e.g. "1" by the gel package. Is there any way of
preventing this?  

I have tried adding a "consume" to the keyPressed function but that does not
work:

        public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                char keyChar = e.getKeyChar();
                int flags = e.getModifiers();
                IJ.write("keyCode=" + keyCode + " (" +
KeyEvent.getKeyText(keyCode)
                        + ") keyChar=\"" + keyChar + "\" (" + (int)keyChar +
") "
                        + KeyEvent.getKeyModifiersText(flags));

                e.consume(); //ADDED to original - does not work
        }

I am aware that the keypad is free to use but I am left handed and therefore
not keen on this..

Thanks for any help!

Richard Ansorge.
Reply | Threaded
Open this post in threaded view
|

Re: Using keyboard with plugin

Albert Cardona
Richard,

The problem is that the instance of ij.ImageJ reads the KeyEvent before
it reaches your plugin (even if you call consume() on the event, it has
already been read). So, easy: remove the KeyListener from the ImageJ
class, and when your plugin finishes (inside the finally clause of a try
{ } catch () {} finally {} , just to be sure) re-add the listener.

As easy as:

IJ.getInstance().removeKeyListener(IJ.getInstance());


If you want to test this on the fly, just use the Jython Intepreter,
type the same without the ending semicolon
http://www.mcdb.ucla.edu/research/hartenstein/software/imagej/other_plugins.html#interpreter 



To add back the listener:

IJ.getInstance().addKeyListener(IJ.getInstance());


Albert
Reply | Threaded
Open this post in threaded view
|

Re: Using keyboard with plugin

Wayne Rasband
In reply to this post by Richard Ansorge
Check "Require Command Key for Shortcuts" in Edit>Options>Misc and the
user will have to hold down the control key (command key on the Mac) to
use the built in shortcuts. A plugin can enable/disable this option by
assigning true or false to the public Prefs.requireControlKey variable.

-wayne


On Jan 12, 2007, at 11:30 AM, Richard Ansorge wrote:

> Hi,
>
> I am trying to develop a PluginFilter for simple Image Editor which
> uses keyboard commands. I am reading the keyboard using the
> keyListener.java example.
>
> Everything works, EXCEPT most keystrokes are also captured as
> hotkeys by other parts of ImageJ; e.g. "1" by the gel package. Is
> there any way of preventing this?
>
> I have tried adding a "consume" to the keyPressed function but
> that does not work:
>
> public void keyPressed(KeyEvent e) {
> int keyCode = e.getKeyCode();
> char keyChar = e.getKeyChar();
> int flags = e.getModifiers();
> IJ.write("keyCode=" + keyCode + " (" +
> KeyEvent.getKeyText(keyCode)
> + ") keyChar=\"" + keyChar + "\" (" + (int)keyChar +
> ") "
> + KeyEvent.getKeyModifiersText(flags));
>
> e.consume(); //ADDED to original - does not work
> }
>
> I am aware that the keypad is free to use but I am left handed and
> therefore
> not keen on this..
>
> Thanks for any help!
>
> Richard Ansorge.
>