How do I register pressing keys tabbed out of the application window

Currently trying to make a over complicated software to run alongside other software and I'm currrently trying to get something to register colour changes in pixels while holding down a key. However, after trying to figure out why my idea wasn't working after 2 hours of messing around, pressing keys aren't registered while tabbed out of the application window.

Is there a way around it?

import java.awt.Rectangle;
import java.awt.*;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.AWTException;
import java.awt.event.InputEvent;
import java.awt.event.KeyListener;

PImage screenshot;
int PixelY;
int PixelX;
int Trigger;
int buttons;
Robot robot;
int triggerkey;

static final String RENDERER = FX2D;
static final int FPS = 60, DELAY = 100/FPS/4, SMOOTH = 3;

void setup() {
  size(1920, 1080, RENDERER);

  smooth(SMOOTH);
  frameRate(FPS);
  imageMode(CORNER);

  screenshot = createImage(displayWidth, displayHeight, ARGB);
  thread("screenshotThread");
  //
  try { 
    robot = new Robot();
  } catch (AWTException e) {
    e.printStackTrace();
    exit();
  }
}

void draw() {
  image(screenshot, 0, 0, width, height);
  frame.setTitle("FPS : " + round(frameRate));
  fill(1,1,1);
  rect((1920/2),(1080/2+1),1,1);
  PixelX = get((1920/2),(1080/2));

  if(PixelY != PixelX){
    print("Trigger Enabled");
      Trigger = 1;
  } 

  if (PixelY == PixelX){
    Trigger = 0;
  } else {
    Trigger = 1;
    println("Toggled");
  }
  delay(1);
  PixelY = get((1920/2),(1080/2));

  print("(");
  print(PixelY);
  print(":");
  print(PixelX);
  print("(");
  println();
  if (keyPressed) {
    println(true);
    if(key == 'f'){
      println("Holding");
      triggerkey = 1;
    }
  } else { 
      triggerkey = 0;
      println(false);
  }
}

void screenshotThread(){
  final PImage shot = screenshot;
  final Rectangle dimension = new Rectangle(displayWidth, displayHeight);
  final Robot robot;

  try {
    robot = new Robot();
  }

  catch (AWTException cause) {
    exit();
    throw new RuntimeException(cause);
  }
  for (;; delay(DELAY))  grabScreenshot(shot, dimension, robot);
}

static final PImage grabScreenshot(PImage img, Rectangle dim, Robot bot) {
  bot.createScreenCapture(dim).getRGB(
    0, 0, 
    dim.width, dim.height, 
    img.pixels, 0, dim.width);

  img.updatePixels();
  return img;
}

void alt(KeyEvent e){
  if(e.getKeyCode() == 18){
  }
}

void keypressed(){
  if(triggerkey == 1){
    if(Trigger == 1){
       robot.mousePress(InputEvent.BUTTON1_MASK); 
       robot.mouseRelease(InputEvent.BUTTON1_MASK);
       print("Active");
       println("");
       delay(450);
    }
  }
}

Answers

  • If your sketch does not have the current focus, then I am not sure if you can register keys at all.

    If you find a solution under the java category in the world wide web, then you could use it in Processing. Also, please post in the new forum: https://discourse.processing.org

    Kf

Sign In or Register to comment.