Hi Kenneth,
as far as I can say, KeyListeners on ImageWindow and ImageCanvas work
well, also in the current version of ImageJ. I have a plugin with
essentially the same calls to removeKeyListener and addKeyListener in
regular use.
Of course, the KeyListener of an image only works if that image is in
the foreground. It won't work if the ImageJ panel (or something else)
has focus.
My suspicion is that the problem is not related to the KeyListener but
rather to the synchronization and/or wait-notify construct.
What is the function that the KeyListener is supposed to trigger?
If does not take significant computation time, I usually put that
directly into the keyPressed method. The wait-notify construct is needed
only if the operation can take longer than a fraction of a second. The
keyPressed callback is executed in the EventQueue; if one puts long
calculations there, the GUI becomes slow.
Just to make sure, one trivial mistake that sometimes happens is that
"this" refers to different Objects, e.g. because one has an inner class.
Michael
________________________________________________________________
On 21.10.24 17:59, Kenneth Sloan wrote:
I’m reviving an ImageJ Java plugin from 2020. It uses a KeyListener.
The new build does not get any keypresses. I’ve looked at the current
documentation, and can’t find a problem. The plugin is fairly large. I’ll try
to make a minimal version - but would be happy to share it on my ImageJ Update
Site (see below).
Remember - this plugin worked fine in 2020, but now fails.
Here is what I think is the relevant code:
// listen to keyboard - stop IJ from listening!
win = ipl.getWindow(); // delayed until we are SURE it's visible
IJ.log("win = "+win);
canvas = win.getCanvas();
IJ.log("canvas = "+canvas);
win.removeKeyListener(IJ.getInstance());
canvas.removeKeyListener(IJ.getInstance());
IJ.log("removed IJ Listeners");
win.addKeyListener(this);
canvas.addKeyListener(this);
ImagePlus.addImageListener(this);
IJ.log("added Listeners");
…
win.requestFocus();
IJ.log("sampling...”);
category = sampleAt(xList.get(currentSamplePoint),
yList.get(currentSamplePoint));
IJ.log("sampled”);
…
while(true)
{
synchronized(this)
{
IJ.log("waiting...");
wait(); // keyPressed wakes us
IJ.log("woken up!");
}
…
// key pressed!
// keyCode, keyChar, and modifiers are current
If(keyCode == KeyEvent.VK_SPACE) return -1; // DELETE does not
work
if(keyCode == KeyEvent.VK_ENTER) return -2; // no mas!
int category = keyCode - KeyEvent.VK_1; // 1-based keys;
0-based category
if(category < 0) continue; // try again!
if(category >= choices.length) continue; // try again!
return category; // good category!
}
...
@Override
public void keyPressed(KeyEvent e)
{
IJ.log("keyPressed");
keyCode = e.getKeyCode();
keyChar = e.getKeyChar();
modifiers = e.getModifiers();
IJ.log(" "+keyCode+" "+keyChar+" "+modifiers);
synchronized(this)
{
IJ.log("notifying");
notify();
IJ.log("notified");
}
}
And here is the IJ Log:
win = 16385_175994_R_IR_OCT_20120429-09_8C3F4720.bmp
canvas = ij.gui.ImageCanvas[canvas1,5,44,384x496]
removed IJ Listeners
added Listeners
sampling...
sampleAt(90.05, 63.05)
waiting...
It looks to me as if we “wait” and then never get a KeyPress event.
When I press keys, I seem to get the usual ImageJ responses (does this mean
that the IJ Listeners were NOT removed?
Update site: CreativeComputation
Plugin name: AVL_Area_Fraction
—
Kenneth Sloan
[email protected]
Vision is the art of seeing what is invisible to others.
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html