开发者

How do I direct key board input to non-focused JTextField?

Some background: I have a turn based game where you can play several concurrent games in separate windows. Each window/game has it's own chat and also a kind of game action panel that turns up when it's your turn t开发者_开发知识库o act.

The issue: Game windows will steal focus whenever it becomes your turn to act in any of the games you're playing, this is by design but very annoying if you're involved in chatting at any of your games because when focus is lost that chat will no longer receive your key board strokes.

What I want: Some way to dispatch key board events to a JTextField that's no longer the focus owner (and also in a different window/JFrame than the current focus owner). Is there some way to do this? And how?

Plan b would be to set some kind of timer on the chat and let the window refuse to give up focus until x amount of time has passed since the last key stroke in the chat, but it might not be ok to stall the focus switch since your action time is limited already.


You can set alwaysOnTop of the current chat window true while there's text in the chat text field by calling:

java.awt.Window
public final void setAlwaysOnTop(boolean alwaysOnTop)
                         throws SecurityException

When user presses Enter or clicks on Send button reset the alwaysOnTop so other windows could steel the focus.


I might have found what I was looking for here. Remains to be seen if I'll try to use it or if we'll change the requirements :)

See this link to find a working SSCCE on how to redispatch KeyEvents to any text component.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜