# How to use robot.mousePress(int) and robot.mouseRelease(int)?

**URL:** https://discourse.processing.org/t/how-to-use-robot-mousepress-int-and-robot-mouserelease-int/27768
**Category:** Coding Questions
**Created:** [February 12, 2021, 5:36pm UTC](https://discourse.processing.org/t/how-to-use-robot-mousepress-int-and-robot-mouserelease-int/27768 "2021-02-12T17:36:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [February 12, 2021, 5:36pm UTC](https://discourse.processing.org/t/how-to-use-robot-mousepress-int-and-robot-mouserelease-int/27768/1 "2021-02-12T17:36:51Z")

</div>

Hi!  
I recently started using the robot library. I tried using `inputEvent.` and `mouseEvent` with the BUTTON\_MASK1… (I saw those methods on forums)  
However;

1. I can’t use robot.mousePress(int) and robot.mouseRelease(int)
2. Is there a way to use robot library without try { } catch(Exemption e) { }?

Can you think of a way to use it?

Thank you.

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [February 12, 2021, 6:33pm UTC](https://discourse.processing.org/t/how-to-use-robot-mousepress-int-and-robot-mouserelease-int/27768/2 "2021-02-12T18:33:14Z")

</div>

Hi,

After some research, I have a working code :

```auto
import java.awt.*;

/**
 * From : https://forum.processing.org/two/discussion/17675/how-to-get-surface-window-x-and-y-coordinates
 */
java.awt.Frame getWindowFrame() {
  return (java.awt.Frame) ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
}

/**
 * From : https://stackoverflow.com/questions/19185162/how-to-simulate-a-real-mouse-click-using-java/19296065#19296065
 */
void click(int x, int y) throws AWTException {
  Robot bot = new Robot();
  bot.mouseMove(x, y);    
  bot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
  bot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
}

void setup() {
  size(500, 500);
  background(255);
}

void draw() {
  // Every 50 frames
  if (frameCount % 50 == 0) {
    try {
      java.awt.Frame f = getWindowFrame();
      
      // Get random coordinates on the window
      int randomX = int(random(width));
      int randomY = int(random(height));
      
      // Click at that location
      click(f.getX() + randomX, f.getY() + randomY);
    } catch(Exception e) {
      println(e);
    }
  }
}

void mousePressed() {
  println("Robot clicked at : " + mouseX + ", " + mouseY);
  
  // Draw a black circle
  fill(0);
  circle(mouseX, mouseY, 50);
}

```

Make sure you don’t put a value that is too low for the click interval because the robot is powerful!  
You can still press escape if so.

The only problem is that the frame location is the top left corner and don’t take into account the window header.

Useful links :

- Get window coordinates : [How to get surface / window x and y coordinates? - Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/17675/how-to-get-surface-window-x-and-y-coordinates)

- Java doc for the Robot class : [Robot (Java Platform SE 7 )](https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html)

  

For your second question, this is how Java handles exceptions and in a function, you must catch the exception using a `try {...} catch {...}` or use `throws` so the function throw the exception higher in the call stack.

Have fun! 😉

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [September 16, 2022, 8:48am UTC](https://discourse.processing.org/t/how-to-use-robot-mousepress-int-and-robot-mouserelease-int/27768/3 "2022-09-16T08:48:34Z")

</div>

@josephh

Thank you.  
Your content has been of great help. thank you.

---

<div class="post-metadata">

### Author: ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)
#### Post date: [September 16, 2022, 9:06am UTC](https://discourse.processing.org/t/how-to-use-robot-mousepress-int-and-robot-mouserelease-int/27768/4 "2022-09-16T09:06:11Z")

</div>

> [@josephh](#):
>
> Make sure you don’t put a value that is too low for the click interval because the robot is powerful!  
> You can still press escape if so.

I use Robot for grinding in enchanting in Skyrim so in my case you can’t press escape to exit so I use another way to force quit the program:

I put a file on a stick and copy the path into this code:

```auto
String emergencyExit="Some path to a file on a stick";
public boolean checkEmergencyExit() {
  try {
    return new File(emergencyExit).exists();
  }
  catch(Exception e) {
    e.printStackTrace();
    return false;
  }
}
void checkExit() {
  if (checkEmergencyExit()) exit();
}

```

Next I have my sketch use checkExit after every Robot. Should the script do something it isn’t supposed to I insert the stick stopping the program.

This is also useful if the Robot e.g. accidentally minimizes the window or clicks on another window.
