Test whether a button has been pressed even when the program is minimized

Hi I want to create a program that runs in the background and tests whether a key has been pressed. Like a keylogger…
I need that because when I press ALT & S, for example, I want to take a screenshot.

Here is the Code that I have for the Screenshot:

import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;

Robot robot;

PImage screenImage;

PVector a, b;
PVector c, s;

void setup() {
  screenImage = captureScreen();
  a = new PVector(0, 0);
  b = new PVector(0, 0);
  c = new PVector(0, 0);
  s = new PVector(0, 0);
void draw() {
  background(255, 0, 0);
  image(screenImage, 0, 0);
  if (mousePressed) {
    b.set(mouseX, mouseY);

    fill(255, 0, 0, 60);
    stroke(255, 0, 0);
    rect(c.x, c.y, s.x, s.y);
void mousePressed() {
  a.set(mouseX, mouseY);
  b.set(mouseX, mouseY);
void mouseReleased() {
  selectOutput("Where should the screen be saved?", "fileSelected");
void fileSelected(File selection) {
  PImage si = screenImage.get(abs(int(c.x+s.x/2)), abs(int(c.y+s.y/2)), abs((int)s.x), abs((int)s.y));
void setCenterAndSize() {
  float x = (a.x+b.x)/2;
  float y = (a.y+b.y)/2;
  c.set(x, y);
  s.set(min(a.x, b.x)-max(a.x, b.x), min(a.y, b.y)-max(a.y, b.y));
void setRobot() {
  try {
    robot = new Robot();
  catch(Exception e) {
PImage captureScreen() {
  Rectangle rect = new Rectangle(0, 0, displayWidth, displayHeight);
  BufferedImage bufferedImg = robot.createScreenCapture(rect);
  PImage i = new PImage(bufferedImg);
  return i;

In processing you can use surface.setVisible (false); to make the program invisible. But as soon as this happens, the keyPressed function is no longer executed…
I hope you can help me :smile: