Login  Register

Re: KeyListener problem

Posted by Fred Damen on Mar 18, 2020; 9:47pm
URL: http://imagej.273.s1.nabble.com/KeyListener-problem-tp5023044p5023064.html

Greetings,

The plugin below seems to work for me. Closing the image window restores
the key handling back to normal.

Note that, in my interpretation of the javadocs for KeyListener,
uncommenting the restoration of the ij KeyListener should work the same by
including the e.consume() in the callbacks, but alas on the window machine
that I am currently on it does not seem to work the same.

Does the java virtual machine close down? If it actually has you should
not see what you are seeing.  FIJI may have closed or only seem to have
closed down, but the JVM might be keeping a handle on your objects for
some reason.  I had the problem ages ago, that a previous version of my
plugin was run every time I freshly compiled and ran an updated version of
the plugin.

Also try and send the JVM a SIGKILL (ctrl-\) and stare at the output...
(see previous emails on this subject)

Enjoy,

Fred

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import java.awt.event.*;
import ij.plugin.*;
import ij.plugin.frame.*;

public class My_Keys implements PlugIn, KeyListener, WindowListener {

   public void run(String arg) {
      ImagePlus imp = IJ.getImage();
      imp.getWindow().getCanvas().removeKeyListener(IJ.getInstance());
      imp.getWindow().getCanvas().addKeyListener(this);
      //imp.getWindow().getCanvas().addKeyListener(IJ.getInstance());
      imp.getWindow().getCanvas().requestFocus();
      imp.getWindow().addWindowListener(this);
      IJ.log("stealing keys for '"+imp.getTitle()+"'");
      }

    public synchronized void keyPressed(KeyEvent e) {
       IJ.log("Pressed: '"+e.getKeyText(e.getKeyCode()));
       e.consume();
       }

    public void keyTyped(KeyEvent e) {
       IJ.log("Typed: '"+e.getKeyText(e.getKeyCode()));
       e.consume();
       }
    public void keyReleased(KeyEvent e) {
       IJ.log("Releas11red: '"+e.getKeyText(e.getKeyCode()));
       e.consume();
       }

    public void windowActivated(WindowEvent e)   {}
    public void windowClosed(WindowEvent e)      { IJ.log("Bye"); }
    public void windowClosing(WindowEvent e)     { IJ.log("Leaving so
soon."); }
    public void windowDeactivated(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowIconified(WindowEvent e)   {}
    public void windowOpened(WindowEvent e)      {}
}



On Wed, March 18, 2020 1:16 pm, Kenneth Sloan wrote:

> I have solved the potential race condition by moving the:
>
> win = ipl.getWindow()
>
> until AFTER several other interactions take place, all of which cannot
> proceed until the window is visible.  But, this has zero effect on my
> machine.
>
> Another possibility was that the desired window had lost focus - but in
> that case (i.e., when I do it intentionally) pressing most number keys in
> 1-6 will invoke  some standard ImageJ shortcut,  and my "client" reports
> that "nothing happens" on those keypresses.
>
> He has a new version, and if that fails I will go to watch it fail, in
> person.  I still can't reproduce it on my machine.
>
> The good news is that I'm cleaning up a lot of cruft by eliminating all
> the possible problems.  None of them have helped, but the code is
> cleaner...
>
> A nagging thought is that the plugin often works 1-4 times in a row on his
> machine and then fails.  Once it fails, it fails forever.  Even restarting
>  FIJI seems to have no effect - but re-instaling the plugin does (for at
> least the first invocation following installation).
>
> Very strange!
>
> If there are any volunteers, I can put  source code on Box and send a link
> (or just e-mail the source code.  It's 1 .java file (467 lines) and (in my
> opinion) very simple.  I'll accept all "pointing and giggling" at my bad
> coding style.  All it does is implement a standard stereology grid,
> displaying cross-hairs (using overlays) and asking the user to classify
> each point by typing a number from 1-6.  It really shouldn't be this
> difficult!
>
> The hardest problem with intermittent bugs is replicating them!
>
> --
> Kenneth Sloan
> [hidden email]
> 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