如何使用 Java 在 JWindow 中捕获键盘输入?
当我按下键盘上的 F1 到 12 或 0 到 9 或 A 到 Z(所有按钮)。我没有看到它捕获我的键盘输入。我该如何解决这个问题?
public class Boot extends JWindow implements KeyListener
{
public Boot()
{
.....
this.addKeyListener(this);
....
}
public void keyTyped(KeyEvent ke)
{
System.out.println( ke.getKeyChar());
}
public void keyPressed(KeyEvent ke)
{
System.out.println( ke.getKeyChar());
/* KEY EVENTS */
// KeyEvent.KEY_TYPED
// KeyEvent.KEY_PRESSED
// int id = id.getId();
}
public void keyReleased(KeyEvent ke)
{
System.out.println( ke.getKeyChar());
}
}
When i press keyboard with F1 to 12 or 0 to 9 or A to Z (all buttons). I do not see its capturing my keyboard inputs. How do i fix this?
public class Boot extends JWindow implements KeyListener
{
public Boot()
{
.....
this.addKeyListener(this);
....
}
public void keyTyped(KeyEvent ke)
{
System.out.println( ke.getKeyChar());
}
public void keyPressed(KeyEvent ke)
{
System.out.println( ke.getKeyChar());
/* KEY EVENTS */
// KeyEvent.KEY_TYPED
// KeyEvent.KEY_PRESSED
// int id = id.getId();
}
public void keyReleased(KeyEvent ke)
{
System.out.println( ke.getKeyChar());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
KeyEvent 仅传递给可聚焦的组件。
阅读 JWindow() 构造函数的 API。它指出:
创建一个没有指定所有者的窗口。该窗口将无法获得焦点。
阅读 JWindow(Frame) 构造函数的 API。它指出:
创建具有指定所有者框架的窗口。如果所有者为空,则将使用共享所有者,并且该窗口将不可聚焦。另外,除非其所有者显示在屏幕上,否则该窗口将无法获得焦点。
因此,基本上在使用 JWindow 时您还需要创建一个可见的 JFrame。
我在论坛上看到的一个 hack 是使用:
或者更好的方法是使用未修饰的 JFrame,您不必担心这一点。
KeyEvents are only passed to components that are focusable.
Read the API for the JWindow() constructor. It states:
Creates a window with no specified owner. This window will not be focusable.
Read the API for the JWindow(Frame) constructor. It states:
Creates a window with the specified owner frame. If owner is null, the shared owner will be used and this window will not be focusable. Also, this window will not be focusable unless its owner is showing on the screen.
So basically you also need to create a visible JFrame when using a JWindow.
A hack I've seen on the forums is to use:
Or a better approach is to use an undecorated JFrame and you don't have to worry about this.
[KeyEvent 的 Java API]
getKeyChar 方法始终返回有效的 Unicode 字符或 CHAR_UNDEFINED。字符输入由 KEY_TYPED 事件报告:KEY_PRESSED 和 KEY_RELEASED 事件不一定与字符输入相关。因此,保证 getKeyChar 方法的结果仅对 KEY_TYPED 事件有意义。
对于按键按下和按键释放事件,getKeyCode 方法返回事件的 keyCode。对于按键类型事件,getKeyCode 方法始终返回 VK_UNDEFINED。
在按键释放时使用 getKeyCode。
KeyEvent.F1、F2...可用于功能键。
[Java API of KeyEvent]
The getKeyChar method always returns a valid Unicode character or CHAR_UNDEFINED. Character input is reported by KEY_TYPED events: KEY_PRESSED and KEY_RELEASED events are not necessarily associated with character input. Therefore, the result of the getKeyChar method is guaranteed to be meaningful only for KEY_TYPED events.
For key pressed and key released events, the getKeyCode method returns the event's keyCode. For key typed events, the getKeyCode method always returns VK_UNDEFINED.
Use getKeyCode on key released.
KeyEvent.F1, F2, ... are usable for the function keys.