Solved

How to catch right and left mouse pressed at the same time?

Posted on 2008-06-11
4
267 Views
Last Modified: 2010-03-30
Hi Experts..
 i wanna have 3 mouse pressed event
1st :  left mouse pressed
2nd : Right mouse Pressed
3rd : right and left mouse pressed

how can i handle my 3rd event(right and left pressed) in the same time...because as we know..we cant do it in the almost exact time..so that it would run the 1st event/2nd event first before running the 3rd event
so how does to avoid 1st/2nd even to occured before my 3rd?
0
Comment
Question by:iphone
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 21766136
don't think you can avoid it.
You'll need to add some logic to your code to handle it.
eg. checking if the other is already pressed when the other other is
And adding a slight delay to wait for the other to be pressed before processing the single press
0
 
LVL 8

Accepted Solution

by:
malfunction84 earned 250 total points
ID: 21766154
If you operate off of when the buttons are released rather than when they're pressed, you can detect if the other button was pressed during the interval where a button is held down.  That's your best chance of detecting when both buttons are down.

Try the following.  (I'm going to use pseudocode to speed this up.)
define flag_1 //indicates that button_1 was released while button_2 is still pressed

define flag_2 //indicates that button_2 was released while button_1 is still pressed
 

when button_1 is released {

  if (flag_2 is set)

    unset flag_2

    execute event_3

  else

    if (button_2 is still being held down)

      set flag_1

    else

      execute event_1

}
 

when button_2 is released {

  if (flag_1 is set)

    unset flag_1

    execute event_3

  else

    if (button_1 is still being held down)

      set flag_2

    else

      execute event_2

}

Open in new window

0
 
LVL 10

Expert Comment

by:margajet24
ID: 21766165

it has been a long time since i used java..

but please try this..

public void mouseClicked(MouseEvent e)
{
    if (e.getButton() == (MouseEvent.BUTTON1 | MouseEvent.BUTTON2) )
        // do something here
    else if (e.getButton() == MouseEvent.BUTTON1 )
        // do something here    
    else if (e.getButton() == MouseEvent.BUTTON2 )
        // do something here
}

           
0
 

Author Comment

by:iphone
ID: 21766177
Thx for your guys quick replies
but think malfunction84 thrown something for me to think on it
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 86
Unexpected HttpURLConnection connection behavior 2 86
Problem to start Neon 20 88
arguments to jar 5 15
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now