java - Pressing both mouse buttons not working correctly -


I have to see if I press the left, right or both buttons on my mouse, here is a sample code: < / P>

  package sandbox; Import java.awt.event.MouseEvent; Import java.awt.event.MouseListener; Import javax.swing.JButton; Import javax.swing.JFrame; Import javax.swing.SwingUtilities; Public class sandbox JFrame {public static expands zero main (string [] args) {Sandbox S = new sandbox (); s.setVisible (true); } Public Sandbox () {setDefaultCloseOperation (EXIT_ON_CLOSE); setSize (300, 200); setResizable (wrong); SetTitle ("frame"); SetLayout (zero); Button = New Pocket ("Click Me"); but. Setbound (0, 0, 120, 50); But KaddMouseListener (new Mauslistr () {@Override public void Mausclait (MouseEvent e) {} @ Override pressed public void Mouse (Mausivent e) {if (swing Utiylsksaliftmosbtn (e) | swing Utilijh. Aiaraiarmusbtn (e) ) system. println ( "both"); if (swing Utiylsksaliftmosbtn (e)) Sistmkautkprintlann ( "left"); if (swing Utiylskaisiaritmosbtn (e)) system. Autkprintlain ( " Right ");} @ Override Pub Black Wide Moorrelezed (MouseEver E) {} Override Public Wide Mice Enter (MouseEver E) {} @ Override Public Whole Mousexide (MouseEvent E) {}}); (But add); }}   

I have to use swing uTs, but if I press a button on the left or right then it will not print "both", "left" or "right", a there is a problem. If I use MouseVent Class, it was working properly but I needed it because it is possible through Swing UT's class, thanks.

Edit: Use MouseEvent class and it is doing the right thing:

  if (e.getModifiersEx () == (MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON3_DOWN_MASK) ) ...    

OR ( | ) Do not check, and use the AND ( & amp; or short cut version & amp; amp; amp; amp; amp; amp; amp; amp; amp;; ) instead:

  if (swing utiils.elelfoft muuteton (e) & swingout. Irirmouth button (e)) ...   

update :

Check it out That are pressing both buttons, Swing Utiijiti does not seem right equipment. isXxxxMouseButton (...) Methods do the same way that you do, they check the button mask, but use different masks that are not set when both buttons are pressed .

For example, isRightMouseButton () has been implemented as follows:

  Public stable boolean is RightMouseButton (MouseEvent anEvent) {return ((AnEvent.getModifiers () and InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK); }   

As you can see, the implementation is not complete, so you can better check yourself to mask.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -