Have to click before pressing key

爱⌒轻易说出口 提交于 2019-12-11 02:36:52

问题


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)

来源:https://stackoverflow.com/questions/19813613/have-to-click-before-pressing-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!