0

I would like to be able to have background input in Java. I set up the keyboard listener the usual way (I think). This code works fine, but whenever the program loses focus, the keys stop updating. The update method is ran 60 times a second and continues to be ran while the frame is out of focus, but the keys don't update (If a key is pressed when the frame loses focus, the program will act as if you are holding it down).

My question: is there a way to allow Java to receive background inputs when the frame is out of focus? I would prefer a solution without external libraries but any will help.

The reason I would like to do this is because my program will have multiple frames and I need to be able to receive input in the main frame even if it is not in focus, including when none of the frames are in focus.

Not sure how helpful it will be but my current code for listening for key events is below.

Here I add the key listener to my main class.

public static Keyboard key;
key = new Keyboard();
addKeyListener(key);

This code is from my Keyboard class

private boolean[] keys = new boolean[120];

public boolean up, down, left, right;

public void update() {
    up = keys[KeyEvent.VK_UP] || keys[KeyEvent.VK_W];
    down = keys[KeyEvent.VK_DOWN] || keys[KeyEvent.VK_S];
    left = keys[KeyEvent.VK_LEFT] || keys[KeyEvent.VK_A];
    right = keys[KeyEvent.VK_RIGHT] || keys[KeyEvent.VK_D];
}

public void keyPressed(KeyEvent e) {
    keys[e.getKeyCode()] = true;
}

public void keyReleased(KeyEvent e) {
    keys[e.getKeyCode()] = false;
}
user1803551
  • 12,965
  • 5
  • 47
  • 74
Caleb Limb
  • 304
  • 1
  • 11
  • "*my program will have multiple frames*" See [this](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) and consider a `JDesktopPane` instead. As for the focus problems for key inputs, see [this](http://stackoverflow.com/questions/22741215/how-to-use-key-bindings-instead-of-key-listeners/). – user1803551 May 05 '15 at 23:56
  • I am making a game in which the objects are individual frames so that it can be played over top of another window. I've fixed all of the problems regarding keeping them on top and not interfering with the window but whenever you click anywhere off of one of the objects it looses focus quits taking input. – Caleb Limb May 06 '15 at 00:05
  • As I referred to, use `JInternalFrame` for you objects instead and the `JDesktopPane` as your "another window". The second link I posted shows you how to solve the focus problem. – user1803551 May 06 '15 at 00:10
  • `KeyListener` is for use with a single component which MUST have keyboard focus AND be focusable. You could consider using the key bindings API ([How to Use Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html)) which can avoid these limitations, but can still be blocked by other components (ie text components, `JList`, `JTable`) which need to respond to a given key based on their own needs. – MadProgrammer May 06 '15 at 00:57
  • *"My question: is there a way to allow Java to receive background inputs when the frame is out of focus?"* - No; *"I would prefer a solution without external libraries"* - This would be your only choice, depending on what you are trying to do – MadProgrammer May 06 '15 at 00:58

0 Answers0