重新获得窗口焦点后,Java keyPressed 事件停止工作

发布于 2025-01-16 21:51:52 字数 690 浏览 3 评论 0原文

我的 KeyPressed 事件有一点问题。 如果我启动我的应用程序,一切都会正常。如果我然后对窗口进行散焦,然后重新聚焦,则 KeyEvent 不会触发。我按“Shift + Tab”后,它再次起作用。

@Override
public void keyPressed(KeyEvent e) {
// Prints the keycode
System.out.println("Key-Pressed: " + e.getKeyCode());
}

这些只是为了显示窗口是否处于焦点。

@Override
public void windowGainedFocus(WindowEvent e) {
System.out.println("Window Focus Gained");            
}

@Override
public void windowLostFocus(WindowEvent e) {
System.out.println("Window Focus Lost");            
}

这里有一个小视频,显示了我所描述的内容: https://youtu.be/j0VKQKUjJGc

我希望有人可以帮我解决这个问题,提前谢谢你。

I have a little Problem with my KeyPressed Event.
If I launch my App everything works fine. If I then Defocus the Window, and later Refocus, the KeyEvent does not fire. After I press "Shift + Tab", It works again.

@Override
public void keyPressed(KeyEvent e) {
// Prints the keycode
System.out.println("Key-Pressed: " + e.getKeyCode());
}

These are just, for showing if the Window is in focus.

@Override
public void windowGainedFocus(WindowEvent e) {
System.out.println("Window Focus Gained");            
}

@Override
public void windowLostFocus(WindowEvent e) {
System.out.println("Window Focus Lost");            
}

Here a small Video that shows what I described: https://youtu.be/j0VKQKUjJGc

I hope some one can help me with this, thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坦然微笑 2025-01-23 21:51:52

你有没有尝试过:

public void windowGainedFocus(WindowEvent e) {
    System.out.println("Window Focus Gained");
    // requestFocus on the object with the keyPress listener
    panel.requestFocus();
}

Have you tried:

public void windowGainedFocus(WindowEvent e) {
    System.out.println("Window Focus Gained");
    // requestFocus on the object with the keyPress listener
    panel.requestFocus();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文