how to use MouseAdapter

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

how to use MouseAdapter

Aryeh Weiss
I am try to do something interactive, and for this reason I need to
learn ho wot use MouseAdapter.

I began with  this, and it works fine.:

from java.awt.event import MouseAdapter

class DliteMouseListener (MouseAdapter):

     def __init__ (self):
         self.xLoc = 0
         self.yLoc = 0
     def mouseClicked (self, e):
         print 'In DliteMouseListener: e.getX() =', e.getX(), ',
e.getY() =', e.getY()
         self.xLoc = e.getX()
         self.yLoc = e.getY()

and this calls it:

dML = DliteMouseListener()
ic = inputImp.getCanvas()
ic.addMouseListener (dML)

So far, it works fine.

Now I want to be able to detect if the shift or control key is pressed,
so I looked up MouseEvent, and found
getMouseModifiersText(int modifiers), so I tried to use

e.getMouseModifiersText("Shift") or e.getMouseModifiersText("Control")
to check if the Shift or Control key is pressed during the event.

I get the following error in the Fiji ConsoleAttributeError:
'java.awt.event.MouseEvent' object has no attribute 'getModifierStateText'

I tried variations of this such as using MouseEvent.shiftKey , or
MouseEvent.ctrlKey, and again these attributes do not exist.

When I pretty-print the MouseEvent e, I see that it gives my the
coordinates, the absolute coordinates, the button number, and the
clickCount. (and indeed, e.button worked).

At this point I realize that I do not understand something basic about
how this works.

Can I modify the dML class to do this, or am I completely off target and
I need something completely different to do what I am trying to do?

Thanks in advance for any assistance.

--aryeh

--
Aryeh Weiss
Faculty of Engineering
Bar Ilan University
Ramat Gan 52900 Israel

Ph:  972-3-5317638
FAX: 972-3-7384051


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: how to use MouseAdapter - update

Aryeh Weiss
I found that the getModifiersEx() method works, and that if I press
shift it returns 64, and control returns 128. So now I have a way to
detect a pressed shift or control.  The new code is

class DliteMouseListener (MouseAdapter):

     def __init__ (self):
         self.xLoc = 0
         self.yLoc = 0
         self.mods = 0
     def mouseClicked (self, e):
         print 'In DliteMouseListener: e.getX() =', e.getX(), ',
e.getY() =', e.getY(), "mods = ", e.getModifiersEx()
         self.xLoc = e.getX()
         self.yLoc = e.getY()
         self.mods = e.getModifiersEx()
     def X(self):
         return self.xLoc
     def Y(self):
         return self.yLoc

and I run this to test it:

     while True:
#        wd = WaitForUserDialog("select "+classOfImage)
#        wd.show()
         newX = dML.X()
         newY = dML.Y()
         if dML.mods == 64:
             break
         elif xLoc != newX or yLoc != newY:
             xLoc = newX
             yLoc = newY
             print "top level: ",xLoc, yLoc

However, now the strange thing is that when I click my mouse, it does
thing twice, and if I hold shift, it breaks, but only after printing a
set of coordinates. Here is the output for three clicks and then a 4th
click while holding the shift key:

In DliteMouseListener: e.getX() = 922, e.getY() = 507, mods = 0
top level: 922 0
top level: 922 507
In DliteMouseListener: e.getX() = 622, e.getY() = 517, mods = 0
top level: 622 507
top level: 622 517
In DliteMouseListener: e.getX() = 1044, e.getY() = 875, mods = 0
top level: 1044 517
top level: 1044 875
In DliteMouseListener: e.getX() = 1148, e.getY() = 768, mods = 64
top level: 1148 875

For each call to the listener, it printed two points , because it ran
         elif xLoc != newX or yLoc != newY:
             xLoc = newX
             yLoc = newY
             print "top level: ",xLoc, yLoc
twice. Somehow ,  xLoc and yLoc changed even thought the mouse was not
clicked.
If I use the WaitfForUser dialog, it all works correctly. Even if I
click repeated on the WaitForUser prompt, it does not give me a second
point.

What have I done wrong?

--aryeh

--
Aryeh Weiss
Faculty of Engineering
Bar Ilan University
Ramat Gan 52900 Israel

Ph:  972-3-5317638
FAX: 972-3-7384051



On 18/04/2021 14:35, Aryeh Weiss wrote:

>
> I am try to do something interactive, and for this reason I need to
> learn ho wot use MouseAdapter.
>
> I began with  this, and it works fine.:
>
> from java.awt.event import MouseAdapter
>
> class DliteMouseListener (MouseAdapter):
>
>     def __init__ (self):
>         self.xLoc = 0
>         self.yLoc = 0
>     def mouseClicked (self, e):
>         print 'In DliteMouseListener: e.getX() =', e.getX(), ',
> e.getY() =', e.getY()
>         self.xLoc = e.getX()
>         self.yLoc = e.getY()
>
> and this calls it:
>
> dML = DliteMouseListener()
> ic = inputImp.getCanvas()
> ic.addMouseListener (dML)
>
> So far, it works fine.
>
> Now I want to be able to detect if the shift or control key is
> pressed, so I looked up MouseEvent, and found
> getMouseModifiersText(int modifiers), so I tried to use
>
> e.getMouseModifiersText("Shift") or e.getMouseModifiersText("Control")
> to check if the Shift or Control key is pressed during the event.
>
> I get the following error in the Fiji ConsoleAttributeError:
> 'java.awt.event.MouseEvent' object has no attribute 'getModifierStateText'
>
> I tried variations of this such as using MouseEvent.shiftKey , or
> MouseEvent.ctrlKey, and again these attributes do not exist.
>
> When I pretty-print the MouseEvent e, I see that it gives my the
> coordinates, the absolute coordinates, the button number, and the
> clickCount. (and indeed, e.button worked).
>
> At this point I realize that I do not understand something basic about
> how this works.
>
> Can I modify the dML class to do this, or am I completely off target
> and I need something completely different to do what I am trying to do?
>
> Thanks in advance for any assistance.
>
> --aryeh
>
> --
> Aryeh Weiss
> Faculty of Engineering
> Bar Ilan University
> Ramat Gan 52900 Israel
>
> Ph:  972-3-5317638
> FAX: 972-3-7384051
>


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: how to use MouseAdapter - update

Albert Cardona-2
Use:

e.isShiftDown()
e.isAltDown()
e.isControlDown()

which return boolean, and where “e” is the MouseEvent instance passed as argument to e.g., mousePressed.

Albert

> On Apr 18, 2021, at 2:04 PM, Aryeh Weiss <[hidden email]> wrote:
>
> I found that the getModifiersEx() method works, and that if I press shift it returns 64, and control returns 128. So now I have a way to detect a pressed shift or control.  The new code is
>
> class DliteMouseListener (MouseAdapter):
>
>     def __init__ (self):
>         self.xLoc = 0
>         self.yLoc = 0
>         self.mods = 0
>     def mouseClicked (self, e):
>         print 'In DliteMouseListener: e.getX() =', e.getX(), ', e.getY() =', e.getY(), "mods = ", e.getModifiersEx()
>         self.xLoc = e.getX()
>         self.yLoc = e.getY()
>         self.mods = e.getModifiersEx()
>     def X(self):
>         return self.xLoc
>     def Y(self):
>         return self.yLoc
>
> and I run this to test it:
>
>     while True:
> #        wd = WaitForUserDialog("select "+classOfImage)
> #        wd.show()
>         newX = dML.X()
>         newY = dML.Y()
>         if dML.mods == 64:
>             break
>         elif xLoc != newX or yLoc != newY:
>             xLoc = newX
>             yLoc = newY
>             print "top level: ",xLoc, yLoc
>
> However, now the strange thing is that when I click my mouse, it does thing twice, and if I hold shift, it breaks, but only after printing a set of coordinates. Here is the output for three clicks and then a 4th click while holding the shift key:
>
> In DliteMouseListener: e.getX() = 922, e.getY() = 507, mods = 0
> top level: 922 0
> top level: 922 507
> In DliteMouseListener: e.getX() = 622, e.getY() = 517, mods = 0
> top level: 622 507
> top level: 622 517
> In DliteMouseListener: e.getX() = 1044, e.getY() = 875, mods = 0
> top level: 1044 517
> top level: 1044 875
> In DliteMouseListener: e.getX() = 1148, e.getY() = 768, mods = 64
> top level: 1148 875
>
> For each call to the listener, it printed two points , because it ran
>         elif xLoc != newX or yLoc != newY:
>             xLoc = newX
>             yLoc = newY
>             print "top level: ",xLoc, yLoc
> twice. Somehow ,  xLoc and yLoc changed even thought the mouse was not clicked.
> If I use the WaitfForUser dialog, it all works correctly. Even if I click repeated on the WaitForUser prompt, it does not give me a second point.
>
> What have I done wrong?
>
> --aryeh
>
> --
> Aryeh Weiss
> Faculty of Engineering
> Bar Ilan University
> Ramat Gan 52900 Israel
>
> Ph:  972-3-5317638
> FAX: 972-3-7384051
>
>
>
>> On 18/04/2021 14:35, Aryeh Weiss wrote:
>>
>> I am try to do something interactive, and for this reason I need to learn ho wot use MouseAdapter.
>>
>> I began with  this, and it works fine.:
>>
>> from java.awt.event import MouseAdapter
>>
>> class DliteMouseListener (MouseAdapter):
>>
>>     def __init__ (self):
>>         self.xLoc = 0
>>         self.yLoc = 0
>>     def mouseClicked (self, e):
>>         print 'In DliteMouseListener: e.getX() =', e.getX(), ', e.getY() =', e.getY()
>>         self.xLoc = e.getX()
>>         self.yLoc = e.getY()
>>
>> and this calls it:
>>
>> dML = DliteMouseListener()
>> ic = inputImp.getCanvas()
>> ic.addMouseListener (dML)
>>
>> So far, it works fine.
>>
>> Now I want to be able to detect if the shift or control key is pressed, so I looked up MouseEvent, and found
>> getMouseModifiersText(int modifiers), so I tried to use
>>
>> e.getMouseModifiersText("Shift") or e.getMouseModifiersText("Control") to check if the Shift or Control key is pressed during the event.
>>
>> I get the following error in the Fiji ConsoleAttributeError: 'java.awt.event.MouseEvent' object has no attribute 'getModifierStateText'
>>
>> I tried variations of this such as using MouseEvent.shiftKey , or MouseEvent.ctrlKey, and again these attributes do not exist.
>>
>> When I pretty-print the MouseEvent e, I see that it gives my the coordinates, the absolute coordinates, the button number, and the clickCount. (and indeed, e.button worked).
>>
>> At this point I realize that I do not understand something basic about how this works.
>>
>> Can I modify the dML class to do this, or am I completely off target and I need something completely different to do what I am trying to do?
>>
>> Thanks in advance for any assistance.
>>
>> --aryeh
>>
>> --
>> Aryeh Weiss
>> Faculty of Engineering
>> Bar Ilan University
>> Ramat Gan 52900 Israel
>>
>> Ph:  972-3-5317638
>> FAX: 972-3-7384051
>>
>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: how to use MouseAdapter - update

Aryeh Weiss
Thank you. These work.
I did not find them earlier- I see now that they are inherited from
InputEvent.

To avoid getting spurious responses, I needed to put a delay in the loop
(the time.sleep(0.5) command)


     while True:
#        wd = WaitForUserDialog("select "+classOfImage)
#        wd.show()
         time.sleep(0.5)    # needed to avoid getting spurious output.
         newX = dML.X()
         newY = dML.Y()
         if dML.shiftPressed():
             break
         elif xLoc != newX or yLoc != newY:
             xLoc = newX
             yLoc = newY
             print "top level: ",xLoc, yLoc
             IJ.log("top level: "+str(xLoc)+" "+str(yLoc))

Best regards
--aryeh

On 18/04/2021 21:46, Albert Cardona wrote:

> Use:
>
> e.isShiftDown()
> e.isAltDown()
> e.isControlDown()
>
> which return boolean, and where “e” is the MouseEvent instance passed as argument to e.g., mousePressed.
>
> Albert
>
>> On Apr 18, 2021, at 2:04 PM, Aryeh Weiss <[hidden email]> wrote:
>>
>> I found that the getModifiersEx() method works, and that if I press shift it returns 64, and control returns 128. So now I have a way to detect a pressed shift or control.  The new code is
>>
>> class DliteMouseListener (MouseAdapter):
>>
>>      def __init__ (self):
>>          self.xLoc = 0
>>          self.yLoc = 0
>>          self.mods = 0
>>      def mouseClicked (self, e):
>>          print 'In DliteMouseListener: e.getX() =', e.getX(), ', e.getY() =', e.getY(), "mods = ", e.getModifiersEx()
>>          self.xLoc = e.getX()
>>          self.yLoc = e.getY()
>>          self.mods = e.getModifiersEx()
>>      def X(self):
>>          return self.xLoc
>>      def Y(self):
>>          return self.yLoc
>>
>> and I run this to test it:
>>
>>      while True:
>> #        wd = WaitForUserDialog("select "+classOfImage)
>> #        wd.show()
>>          newX = dML.X()
>>          newY = dML.Y()
>>          if dML.mods == 64:
>>              break
>>          elif xLoc != newX or yLoc != newY:
>>              xLoc = newX
>>              yLoc = newY
>>              print "top level: ",xLoc, yLoc
>>
>> However, now the strange thing is that when I click my mouse, it does thing twice, and if I hold shift, it breaks, but only after printing a set of coordinates. Here is the output for three clicks and then a 4th click while holding the shift key:
>>
>> In DliteMouseListener: e.getX() = 922, e.getY() = 507, mods = 0
>> top level: 922 0
>> top level: 922 507
>> In DliteMouseListener: e.getX() = 622, e.getY() = 517, mods = 0
>> top level: 622 507
>> top level: 622 517
>> In DliteMouseListener: e.getX() = 1044, e.getY() = 875, mods = 0
>> top level: 1044 517
>> top level: 1044 875
>> In DliteMouseListener: e.getX() = 1148, e.getY() = 768, mods = 64
>> top level: 1148 875
>>
>> For each call to the listener, it printed two points , because it ran
>>          elif xLoc != newX or yLoc != newY:
>>              xLoc = newX
>>              yLoc = newY
>>              print "top level: ",xLoc, yLoc
>> twice. Somehow ,  xLoc and yLoc changed even thought the mouse was not clicked.
>> If I use the WaitfForUser dialog, it all works correctly. Even if I click repeated on the WaitForUser prompt, it does not give me a second point.
>>
>> What have I done wrong?
>>
>> --aryeh
>>
>> --
>> Aryeh Weiss
>> Faculty of Engineering
>> Bar Ilan University
>> Ramat Gan 52900 Israel
>>
>> Ph:  972-3-5317638
>> FAX: 972-3-7384051
>>
>>
>>
>>> On 18/04/2021 14:35, Aryeh Weiss wrote:
>>>
>>> I am try to do something interactive, and for this reason I need to learn ho wot use MouseAdapter.
>>>
>>> I began with  this, and it works fine.:
>>>
>>> from java.awt.event import MouseAdapter
>>>
>>> class DliteMouseListener (MouseAdapter):
>>>
>>>      def __init__ (self):
>>>          self.xLoc = 0
>>>          self.yLoc = 0
>>>      def mouseClicked (self, e):
>>>          print 'In DliteMouseListener: e.getX() =', e.getX(), ', e.getY() =', e.getY()
>>>          self.xLoc = e.getX()
>>>          self.yLoc = e.getY()
>>>
>>> and this calls it:
>>>
>>> dML = DliteMouseListener()
>>> ic = inputImp.getCanvas()
>>> ic.addMouseListener (dML)
>>>
>>> So far, it works fine.
>>>
>>> Now I want to be able to detect if the shift or control key is pressed, so I looked up MouseEvent, and found
>>> getMouseModifiersText(int modifiers), so I tried to use
>>>
>>> e.getMouseModifiersText("Shift") or e.getMouseModifiersText("Control") to check if the Shift or Control key is pressed during the event.
>>>
>>> I get the following error in the Fiji ConsoleAttributeError: 'java.awt.event.MouseEvent' object has no attribute 'getModifierStateText'
>>>
>>> I tried variations of this such as using MouseEvent.shiftKey , or MouseEvent.ctrlKey, and again these attributes do not exist.
>>>
>>> When I pretty-print the MouseEvent e, I see that it gives my the coordinates, the absolute coordinates, the button number, and the clickCount. (and indeed, e.button worked).
>>>
>>> At this point I realize that I do not understand something basic about how this works.
>>>
>>> Can I modify the dML class to do this, or am I completely off target and I need something completely different to do what I am trying to do?
>>>
>>> Thanks in advance for any assistance.
>>>
>>> --aryeh
>>>
>>> --
>>> Aryeh Weiss
>>> Faculty of Engineering
>>> Bar Ilan University
>>> Ramat Gan 52900 Israel
>>>
>>> Ph:  972-3-5317638
>>> FAX: 972-3-7384051
>>>
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html


--
Aryeh Weiss
Faculty of Engineering
Bar Ilan University
Ramat Gan 52900 Israel

Ph:  972-3-5317638
FAX: 972-3-7384051

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