Login  Register

Re: KeyListener problem

Posted by L'assegnista on Mar 18, 2020; 6:31am
URL: http://imagej.273.s1.nabble.com/KeyListener-problem-tp5023044p5023045.html

Dear Kenneth,
I've run in a similar problem with a python Macro I wrote for TrakEM2  with
the precious help of Albert Cardona.
http://imagej.1557.x6.nabble.com/Adding-keyboard-shortcut-to-toggle-visibility-of-patches-in-TrakEM2-td5016099.html

This macro sometimes behave exactly as you described, stopping working
randomly. In our case however, re-running the macro is sufficient to make
it work again. Back in 2016 we also had to open a new display, the instance
to which the KeyListener is bound, but this magically resolved at some
point. Moreover, lately this strange behaviour is very rare and we are
indeed successfully using it.

Here the final code, multiple keys are specified with subsequent elif

from java.awt.event import KeyEvent, KeyAdapter
from ini.trakem2.display import Display, Patch

shortCut1="GFP"

class MyKeyListener(KeyAdapter):
         def keyPressed(self, event):
                keyCode = event.getKeyCode()
                     if KeyEvent.VK_1 == keyCode:
                          for layer in
Display.getFront().getLayerSet().getLayers():
                               patches = layer.getDisplayables(Patch)
                               for patch in patches:
                                    if shortCut1 in patch.title:
                                        if patch.visible == True:
                                               patch.visible = False
                                        else:
                                               patch.visible = True

Display.getFront().getCanvas().addKeyListener(MyKeyListener())

Hope this helps

Federico



Il giorno mer 18 mar 2020 alle ore 02:20 Kenneth Sloan <
[hidden email]> ha scritto:

> I have a plugin which displays an image, removes IJ.instance Key and Image
> Listeners, adds itself as Key and ImageListener, and handles keypresses for
> the keys 1-6.
>
> It works perfectly on my machine (a mac, latest OS) - but fails on someone
> elses - INTERMITTENTLY.
>
> When it fails, the plugin reports removing and adding the appropriate
> listeners...and then never reports seeing ANY keypress. (keyPressed() is
> never called).
>
> I followed the template in keyListener.java (I think that's the right
> name).
>
> There is a requestFocus() in what I think is the right place, and I've
> asked the "client" to be sure the image window is on top and "selected".
> No joy.
>
> I'm stumped - does anyone have any experience with similar problems?  Any
> suggestions on how to proceed?  Recall that I can't reproduce the problem
> on my machine.  My "client" reports that the plugin sometimes works -
> perhaps several times in a row - and then fails.  Once it starts to fail,
> even restarting FIJI does not revive it.  I'm guessing (but have not asked)
> that it requires a reboot to fix.
>
> Any clues?
> --
> 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
>


--
Federico Luzzati, PhD
Assistant Professor
University of Turin,
Dept. LIfe Sciences and Systems Biology (DBIOS)
Via Accademia Albertina, 13 - 10123 Torino - IT

Researcher at
Neuroscience Institute Cavalieri Ottolenghi (NICO)
Regione Gonzole, 10 - 10043 Orbassano (To) - IT

tel. +39-0116704683/ -6631
http://www.nico.ottolenghi.unito.it/index.php/it/adult-neurogenesis

Sostieni con il tuo 5xmille il NICO e la ricerca sulle malattie
neurodegenerative
Firma nel riquadro "Finanziamento della ricerca scientifica e
dell'Università"
CF 97564560015

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html