0

I was going through a code that had implementation of Robot() class in it. I did not understand the following method

    public void mousePressed(MouseEvent e) {
    System.out.println("Mouse Pressed");
    writer.println(EnumCommands.PRESS_MOUSE.getAbbrev());
    int button = e.getButton();
    int xButton = 16;
    if (button == 3) {
        xButton = 4;
    }
    writer.println(xButton);
    writer.flush();
}

This basically sends the MouseEvent to another PC using sockets of java. Could someone please explain why is the value of xButton set to 16 and what is happening in the if statement?

TheNoob
  • 33
  • 9
  • 1
    Frankly, no. Not without more context. – Henry Jan 24 '18 at 18:15
  • @Henry I'm afraid there isn't any more to this method. There's other methods but no more to this one and even on the receiving end, the code does nothing to manipulate the values 16, 3 or 4 – TheNoob Jan 24 '18 at 18:46
  • This piece of code is an event handler. [```java.awt.Robot```](https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html) is about generating events, and while it may appear on the other side (where the ```MouseEvent``` gets simulated), it does not use magic values of 16 or 4, not even internally (see https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/awt/Robot.java and https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/awt/event/MouseEvent.java). These numbers are decoded in the code receiving these text messages. – tevemadar Jan 24 '18 at 18:53
  • @tevemadar, the following lines were written in the receiving: end case -1: robot.mousePress(scanner.nextInt()); break; As you can see, there is no decoding on the receiver's side. Any idea what's going on here? – TheNoob Jan 24 '18 at 19:20

2 Answers2

1

Ok, mea culpa, it has been some time since I used Robot. So, the number 16 comes from InputEvent: https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/awt/event/InputEvent.java#L95 - just it is not very tasteful to hardcode it as 16, the documentation suggests usage of these BUTTONx_MASK fields (https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html#mousePress(int) ), and the source locally suggest the usage of BUTTONx_DOWN_MASK (scroll a couple lines upwards).
The 4 is even more terrible, since for button 2 and 3 the source just refers Event.ALT/META_MASK, but Event.META_MASK really ends up being 4 at the end (https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/awt/Event.java#L84 ), so that is right click as suspected.

tevemadar
  • 12,389
  • 3
  • 21
  • 49
0

The MouseEvent method getButton() returns the number of the button that was pressed. You can read up more on that here.

As to what is happening in this code, it appears to check to see if the right mouse button/right mouse click (this post explains which button corresponds to which number) is pressed, and sends the int value 4 if it was. If the button that was pressed was not a right-click, then it sends the int value 16.

EDIT

Actually, in looking at this more, my original answer is not 100% correct. The methods talked about on this post are used to determine which button the MouseEvent is coming from, not a direct correlation between 1 = left click, 2 = middle click, 3 = right click. I still think this was making some decision based on whether or not the MouseEvent was a right-click, but as @Henry said, without more context, I can't be sure.

Francis Bartkowiak
  • 1,374
  • 2
  • 11
  • 28
  • this code is basically being used to determine which button is clicked and it sends that information. But three mouse events can occur and this code is handling only two of them. That's making me even more confused – TheNoob Jan 24 '18 at 18:44
  • Context is key. This is the best information I can provide given the context. If you have access to the person who wrote it, I'd suggest talking to/emailing them to see what they were going for. – Francis Bartkowiak Jan 24 '18 at 18:46