• 0

[JAVA] Distinguishing between Right and Left Click


Question

I've been searching the internet for the last little while, trying to find a tutorial in Java on how to distinguish a click from the mouse as either the right button, or the left button. I have been able to get the location, both X and Y, but I cannot seem to be able to tell whether it was the right, or the left button that made the click.

So far, the only thing I've found has been this, but when I strip it down to:

switch (e.Button)
		{
			case (MouseButtons.Left):
				System.out.println("Left");
				break;
			case (MouseButtons.Right):
				System.out.println("Right");
				break;
		}

I receive an error about not being able to find the symbol "Button" (seems like it needs Button declared). I have it posted inside the mouseReleased method. I've also tried e.getButton, but that didn't do the trick either.

Thanks for any help. I'm still searching, but maybe someone else knows.

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

You realize the tutorial you linked to is for C#...

What you want is from your MouseEvent is to use getButton() and then compare it to MouseEvent.BUTTON1, MouseEvent.BUTTON2, and MouseEvent.BUTTON3.

Link to comment
Share on other sites

  • 0

I posted the wrong link here... C and Java are similar enough, that I decided I'd give it a shot. Everything else for Java was almost identical, with the exception of the getButton, rather then just button.

Link to comment
Share on other sites

  • 0
I posted the wrong link here... C and Java are similar enough, that I decided I'd give it a shot. Everything else for Java was almost identical, with the exception of the getButton, rather then just button.

Similar syntax, but the APIs will differ greatly. The Java API and tutorials (although some are somewhat outdated) are your friends.

API: http://java.sun.com/javase/6/docs/api/

Tutorials: http://java.sun.com/docs/books/tutorial/index.html

Link to comment
Share on other sites

  • 0

Alright, thanks. It's late at night, not sure how I wondered to the C guide. I guess I was only looking at the domain.

I could of sworn I tried e.getButton before, but apparently, it was something else. That did the trick like I needed. Thanks so much!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.