I'd like to use a simple java Robot that types a text when I click Ctrl+Q. But this has to be done even if I am focused an another app (eg. a game). My code works fine, but it runs only if my JFrame is in focus.
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
robot.mouseMove(350, 150);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_T);
// Solution for different keyboard layouts (ALT values)
try {
alt(KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD4, KeyEvent.VK_NUMPAD7);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
robot.keyPress(KeyEvent.VK_Q);
robot.keyRelease(KeyEvent.VK_Q);
}