How to fetch the middle mouse button in java?

I use public boolean mouseDown(Event ev, int x, int y) to detect a click of the mouse.
I can distinguish between the right mouse button (ev.metaDown() is true) and the left and middle.

How can i differentiate the left from the middle button? Or if it is impossible with mouseDown, what should i use?

Asked by: Chelsea111 | Posted: 28-01-2022

Answer 1

Try using ALT_MASK:

This flag indicates that the Alt key was down when the event occurred. For mouse events, this flag indicates that the middle mouse button was pressed or released.

So your code might be:

if (ev.modifiers & Event.ALT_MASK != 0) {
    // middle button was pressed

Of course, all this is assuming you have a very good reason to use mouseDown in the first place, since it is deprecated. You should (probably) be using processMouseEvent instead, which gives you a MouseEvent to play with.

Answered by: Abigail638 | Posted: 01-03-2022

Answer 2

mouseDown is deprecated. All you need is accessible by the MouseEvent.getButton. Track BUTTON3.

Answered by: Freddie938 | Posted: 01-03-2022

Answer 3

This might do it:

Haven't tried it myself.

Answered by: Carina534 | Posted: 01-03-2022

Similar questions

java - Pass events in custom JTable

Hi I have a class called ColorChooser (in the package) This is a custom component used to select colors. What I want is to display a JTable with ColorChoosers in the second column. So I created my own TableCellRenderer and it works: @SuppressWarnings("serial") class ColorChooserTableRenderer extends DefaultTableCellRenderer { public static List<ColorCho...

java - key events with Robot class

there is a problem during handling Robot.KeyPress(KeyEvent...) is it neccesarry to specify all the keys every time.... e.g Robot.KeyPress(KeyEvent.VK_A); Robot.KeyPress(KeyEvent.VK_B); Robot.KeyPress(KeyEvent.VK_C); if there is any shortkut for not repeating this everytime...plz tell me..... and the interpretation for keys that are recieved at client side is diff...than that is sent from server side... ...

key events in java

i have tried a lot you all gave me the code segments as a solution...but still the same error is can anyone give me the solution... It seems a big oversight that the Robot cannot be used to enter basic non-alphanumeric characters. Is there a cross-platform API that converts characters to key strokes?

java - Mouse events on JPanel

i have mouselisteners on multiple jpanels. I need to detect if the mouse is pressed when it enters on a different jpanel than the one the event has started from. how can i do that?

How to send events simply in Java?

I'm writing a basic tic tac toe game in Java. In my Board class I have a swing window which determines which button was pressed. TttGame is the class which has a Board object. The goal of this board is not to act as a fully functional tic-tac-toe board but to serve as a framework where the TttGame class controls all the logic. As such I need the Board to send some kind of event when the button is pressed to the T...

Java events vs C# - new to Java

I am new to the world of Java. I'm coming from C#. I'm trying to set up a custom event. Here is how I would have done this in C# class A { public EventHandler Changed; public void FunctionA() { if(Change != null) Changed(this, null); //fire the event! } } class B { private A instanceOfA = new A(); public void FunctionB() { A.Changed+= new EventHandler...

c# - Add MIDI Events to Clock - Java

I am creating a simple MIDI application in C# with an open source library that allows the user to specify a channel, pitch and velocity and add them to a Clock. This clock can then play all the notes in the sequence that they were added. Everything works great and now I am in the process of creating a Java version of the application, the only troub...

Java Events - How to send extra information to it?

I am a C# dev but currently working on Java. In C# the events have two arguments: the eventArgs - information about the event iself - and the sender, which is information about the object who fired the event. Java has a slightly different approach, each event handler is a class (nested or not). I decided for creating no-nested classes because of code cleanness. What happens is that I am having problems because I

java - Ton of events, ways to shrink code base?

In my program I have something close to 50 events that are thrown out. The issue I'm having is settling on a standard way to recieve the events. My first thought was to use a generic interface and just have listeners implement this and add themselves to the queue of listeners. Unfortunately this meant tha...

How can I react on Mouse events in a Java applet and then paint accordingly?

My homework is Write an applet that draw the house shown on the left in Figure 14-32. When the user clicks on the door or windows, they should close. The figure on the right shows the house with its door and windows closed. I basically want a Java Applet where, if a user clicks on a rectangle, another one is sudden created and drawn. Here is my code so far.

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)