Reputation: 69
I am creating a click test for mousepad in Java. I can differentiate between left, right and middle click through:
if(evt.getButton()==3) // or 1 or 2
What I can't seem to do is to differentiate between 2 lefts.
The 2 lefts being click on 1 and click on 2 in the image above. I have tried to see if I get different values from them when I click by debugging and checking the event object but its the same. For keyboard one can differentiate between two Ctrl or 2 shifts by getting keyLocation
, can one do something similar with click? Like not get where on the screen is clicked but which button was pressed.
private void formMouseClicked(java.awt.event.MouseEvent evt) {
System.out.println(evt.getButton());
if (evt.getButton() == 3) {
//3=right
button1.setBackground(Color.GREEN);
textField1.setText("Code : Right");
} else if (evt.getButton() == 2) {
//middle
button6.setBackground(Color.GREEN);
textField1.setText("Code : Middle");
} else if (evt.getButton() == 1) {
//1=left
button5.setBackground(Color.GREEN);
textField1.setText("Code : Left");
} else {
textField1.setText("Code : " + evt.getButton());
}
}
This above is my code so far in regards with click.
I have searched a lot but couldn't find information that could help yet.
Upvotes: 1
Views: 754
Reputation: 15896
You can you below code SwingUtilities methods
SwingUtilities.isLeftMouseButton(MouseEvent anEvent)
SwingUtilities.isRightMouseButton(MouseEvent anEvent)
SwingUtilities.isMiddleMouseButton(MouseEvent anEvent)
Like:
if(SwingUtilities.isLeftMouseButton(evt)){
// do your work
}
// same for right and middle mouse key
But if you want to check where click happed on the mouse from enter link description here
Java Swing is an old technology, it supports the traditional mouse wheel rotation events.
So i don't think its possible to get difference b/w click happening on diff location of touchpad.
From MouseEvent
There is no way to get the location of mouse click.
Upvotes: 0
Reputation: 10156
As far as I know, there is no way to do this. The trackpad will simply present itself as if mouse1 has been pressed when you click the trackpad. Keyboards have different keycodes for left/right shift, etc... I don't believe there are different keycodes for "trackpad click" and "mouse 1".
Anyway, why would you want to add functionality that only laptop users (and only certain laptop users) would be able to use. What about desktops? What about mobile?
Upvotes: 2