Have to click before pressing key

2019-07-15 09:19发布

问题:

I've finally managed to get my GraphicsProgram in java to remove a certain GObject when the spacebar is pressed.

This is the code:

public void keyPressed(KeyEvent key){
    int space = key.getKeyCode();
    if (space == KeyEvent.VK_SPACE){
        remove(welcome);
    }
}

The problem is that if I start my program, it will not recognize that I am inside the GraphicsWindow program that starts up. (This is somewhat difficult to explain) Basically, I first have to click inside the window, before it will recognize the pressing of the spacebar.

If I do not first click on my window, it does not know I am inside it, and will not recognize a spacebar input.

So is there a way for java to open the window (I am doing this from CMD in windows) in the "front" if you will, immediatelly prepared for a spacebar input?

(If this is a Windows OS specific problem and not a java problem, then I will simply have to find a workaround)