Detecting mouse buttons

Discussion in 'Java' started by Daniel Tahin, Jul 4, 2005.

  1. Daniel Tahin

    Daniel Tahin Guest

    Hi all!

    How can i determine, which mouse button was pressed, using
    java.awt.Event class (JDK1.0 API)?
    I write following for the left button:
    if( e.modifiers!=Event.ALT_MASK && e.modifiers!=Event.META_MASK ) {
    doSomething();
    }
    //all these things are inside of handleEvent(Event e) {}, and i extend
    java.awt.Canvas.

    This seems to be working for the left button, BUT there is only 1
    problem. If i use the scroller, doSomething() will be invoked as well
    (the mouse has 2 buttons, and a scroller).


    Do you know any solution for this problem?
    Thanx in advance,
    Daniel
     
    Daniel Tahin, Jul 4, 2005
    #1
    1. Advertising

  2. On Mon, 04 Jul 2005 15:44:55 +0200, Daniel Tahin wrote:

    > Hi all!
    >
    > How can i determine, which mouse button was pressed, using
    > java.awt.Event class (JDK1.0 API)?
    > I write following for the left button:
    > if( e.modifiers!=Event.ALT_MASK && e.modifiers!=Event.META_MASK ) {
    > doSomething();
    > }
    > //all these things are inside of handleEvent(Event e) {}, and i extend
    > java.awt.Canvas.
    >
    > This seems to be working for the left button, BUT there is only 1
    > problem. If i use the scroller, doSomething() will be invoked as well
    > (the mouse has 2 buttons, and a scroller).


    Just guessing here, but the proper idom would likely be

    ( (e.modifiers & Event.ALT_MASK) != 0
    && (e.modifiers & Event.META_MASK)!= 0)

    since there might be both ALT and META active.

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
     
    Stefan Schulz, Jul 4, 2005
    #2
    1. Advertising

  3. On Mon, 04 Jul 2005 16:00:15 +0200, Stefan Schulz wrote:


    > Just guessing here, but the proper idom would likely be
    >
    > ( (e.modifiers & Event.ALT_MASK) != 0
    > && (e.modifiers & Event.META_MASK)!= 0)
    >
    > since there might be both ALT and META active.


    Erg. make that == in both cases. We are checking for absence of the flags,
    not presence.

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
     
    Stefan Schulz, Jul 4, 2005
    #3
  4. Daniel Tahin

    Joan Guest

    "Daniel Tahin" <> wrote in message
    news:42c93d57$0$12384$...
    > Hi all!
    >
    > How can i determine, which mouse button was pressed, using
    > java.awt.Event class (JDK1.0 API)?


    I might be mistaken, but I don't think the mouse wheel was invented back
    then.

    > I write following for the left button:
    > if( e.modifiers!=Event.ALT_MASK && e.modifiers!=Event.META_MASK ) {
    > doSomething();
    > }
    > //all these things are inside of handleEvent(Event e) {}, and i extend
    > java.awt.Canvas.
    >
    > This seems to be working for the left button, BUT there is only 1
    > problem. If i use the scroller, doSomething() will be invoked as well
    > (the mouse has 2 buttons, and a scroller).
    >
    >
    > Do you know any solution for this problem?
    > Thanx in advance,
    > Daniel
     
    Joan, Jul 4, 2005
    #4
  5. Daniel Tahin

    Daniel Tahin Guest

    Stefan Schulz schrieb:
    > On Mon, 04 Jul 2005 16:00:15 +0200, Stefan Schulz wrote:
    >
    >
    >
    >>Just guessing here, but the proper idom would likely be
    >>
    >>( (e.modifiers & Event.ALT_MASK) != 0
    >> && (e.modifiers & Event.META_MASK)!= 0)
    >>
    >>since there might be both ALT and META active.



    Yes, this is OK for scanning the left button, but the previous problem
    is still there:-((


    >
    >
    > Erg. make that == in both cases. We are checking for absence of the flags,
    > not presence.
    >
     
    Daniel Tahin, Jul 4, 2005
    #5
  6. Daniel Tahin

    Tom N Guest

    Daniel Tahin wrote:

    > How can i determine, which mouse button was pressed, using
    > java.awt.Event class (JDK1.0 API)?
    > I write following for the left button:
    > if( e.modifiers!=Event.ALT_MASK && e.modifiers!=Event.META_MASK ) {
    > doSomething();
    > }
    > //all these things are inside of handleEvent(Event e) {}, and i extend
    > java.awt.Canvas.
    >
    > This seems to be working for the left button, BUT there is only 1
    > problem. If i use the scroller, doSomething() will be invoked as well
    > (the mouse has 2 buttons, and a scroller).


    Mouse events are actually java.awt.event.MouseEvent.

    Call java.awt.event.MouseEvent.getButton()

    result 1 = left button
    2 = scroll wheel (middle button)
    3 = right button

    You might need to call canvas.addMouseListener(new MySubClassOfMouseAdapter())
     
    Tom N, Jul 5, 2005
    #6
  7. Daniel Tahin

    Roedy Green Guest

    On Mon, 04 Jul 2005 15:44:55 +0200, Daniel Tahin
    <> wrote or quoted :

    >This seems to be working for the left button, BUT there is only 1
    >problem. If i use the scroller, doSomething() will be invoked as well
    >(the mouse has 2 buttons, and a scroller).
    >
    >
    >Do you know any solution for this problem


    Have a read of my general essay on the problem. see
    http://mindprod.com/jgloss/event11.html

    I can't figure out just what it is your are asking.

    In these sorts of problem. your first task it to makes you are seeing
    the event, and not to many other events -- ie. that you are tapping
    the right place with the right sort of listener.

    Once you have that established, then start thinking about how to
    filter your events.

    You normally don't meddle at the low level you are proposing.



    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
     
    Roedy Green, Jul 5, 2005
    #7
  8. Daniel Tahin

    Daniel Tahin Guest

    Roedy Green schrieb:
    > On Mon, 04 Jul 2005 15:44:55 +0200, Daniel Tahin
    > <> wrote or quoted :
    >
    >
    >>This seems to be working for the left button, BUT there is only 1
    >>problem. If i use the scroller, doSomething() will be invoked as well
    >>(the mouse has 2 buttons, and a scroller).
    >>
    >>
    >>Do you know any solution for this problem

    >
    >
    > Have a read of my general essay on the problem. see
    > http://mindprod.com/jgloss/event11.html



    A great site. Is it possible to download the complete site for offline use?


    >
    > I can't figure out just what it is your are asking.
    >



    Eevrything worked fine with handleEvent(), but it seems for me, that
    jdk1.3 under Linux interpret the scroller of the mice, as the left
    button (scrolling has the same effect, as pressing the left button.
    Curious, because the wheel mouse support was introduced first in java 1.4).
    I solved the problem by not using the left button... :-(
    Now everything is ok.


    > In these sorts of problem. your first task it to makes you are seeing
    > the event, and not to many other events -- ie. that you are tapping
    > the right place with the right sort of listener.
    >
    > Once you have that established, then start thinking about how to
    > filter your events.
    >
    > You normally don't meddle at the low level you are proposing.
    >
    >
    >



    cu,
    Daniel
     
    Daniel Tahin, Jul 5, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aaron Fude
    Replies:
    1
    Views:
    435
    Paul Lutus
    Sep 18, 2004
  2. Hal Vaughan
    Replies:
    5
    Views:
    4,076
    Michael Rauscher
    Sep 3, 2006
  3. Bou Dramé
    Replies:
    0
    Views:
    189
    Bou Dramé
    Oct 3, 2008
  4. Stuart

    detecting mouse in other frame

    Stuart, Jul 11, 2004, in forum: Javascript
    Replies:
    1
    Views:
    115
    Mike Foster
    Jul 13, 2004
  5. James Black
    Replies:
    0
    Views:
    409
    James Black
    May 28, 2006
Loading...

Share This Page