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

I recently started using the robot library. I tried using inputEvent. and mouseEvent with the BUTTON_MASK1… (I saw those methods on forums)

  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.


After some research, I have a working code :

import java.awt.*;

 * From :
java.awt.Frame getWindowFrame() {
  return (java.awt.Frame) ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();

 * From :
void click(int x, int y) throws AWTException {
  Robot bot = new Robot();
  bot.mouseMove(x, y);    

void setup() {
  size(500, 500);

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) {

void mousePressed() {
  println("Robot clicked at : " + mouseX + ", " + mouseY);
  // Draw a black circle
  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 :

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! :wink:



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

1 Like

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:

String emergencyExit="Some path to a file on a stick";
public boolean checkEmergencyExit() {
  try {
    return new File(emergencyExit).exists();
  catch(Exception e) {
    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.