1

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.

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.

Ivar
  • 6,138
  • 12
  • 49
  • 61
Tamoor Rahil
  • 69
  • 1
  • 6
  • 1
    Thanks. i have edited the post :) – Tamoor Rahil Aug 14 '19 at 09:20
  • 1
    I don't think there is a difference, but maybe you can detect one by listening closely to all other mouse events. For example, when you click with the button you most likely don't move the curser at the same time, while clicking on the pad might also trigger a small movement. But this might depend on the mousepad implementation as well. Check out all MouseEvents and log them to see what happens. – GameDroids Aug 14 '19 at 09:31
  • Thanks. That seems like a valid option. I didnt think of it in that way. I will go ahead and get myself more educated on events and try it out :) – Tamoor Rahil Aug 14 '19 at 09:46

2 Answers2

2

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?

cameron1024
  • 9,083
  • 2
  • 16
  • 36
  • I want to be able to run some test. Lets say you wanna buy a used laptop, i want to boot it on usb(i am using MX linux), my latest snapshot has a java program on desktop, it gives me specs. then i created a keyboard test. yes not laptops have same keyboards but i can visually see on the screen if a key is off. now next step is mouse pad. i am just adding fuctionality as one seems to be completed. – Tamoor Rahil Aug 14 '19 at 09:26
  • 1
    Your best option would be to instruct the user to click each "left click" button they have in sequence. Either that or some driver/manufacturer specific native code, but that is not my area of expertise – cameron1024 Aug 14 '19 at 09:30
  • Thanks cameron i will keep that as an option 2 :) i will go ahead and try what GameDroids suggested first. – Tamoor Rahil Aug 14 '19 at 09:44
0

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.

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89