Sending Keyboard Events to Background Window

C

Chris Share

Hi,

I'm developing an application that consists of three windows
(JFrames). Only one of the JFrames handles keyboard input.

At present, if I move one of the other JFrames to the front the
keyboard control stops. I assume that the front JFrame is getting the
keyboard focus.

I'd like to be able to move one of the other JFrames to the front but
still send the key commands to the back window.

How can I do this?

I've tried using frame.setFocusable(false); however this doesn't seem
to make any difference.

Cheers,

Chris
 
A

Andrew Thompson

Chris Share wrote:
...
I'm developing an application that consists of three windows
(JFrames). Only one of the JFrames handles keyboard input.

At present, if I move one of the other JFrames to the front the
keyboard control stops. I assume that the front JFrame is getting the
keyboard focus.

I'd like to be able to move one of the other JFrames to the front but
still send the key commands to the back window.

Why? What would this ability offer to me the end user
(besides being very confusing)?
How can I do this?

Try adding the first frame to the second frame's KeyListener.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1
 
C

Chris Share

Why? What would this ability offer to me the end user
(besides being very confusing)?

The app displays the same data in various formats (2D, 3D, etc.). To
the user, it actually doesn't matter which window is at the front, as
long as the keyboard controls still function.

Cheers,

Chris
 
A

Andrew Thompson

Chris said:
The app displays the same data in various formats (2D, 3D, etc.). To
the user, it actually doesn't matter which window is at the front, as
long as the keyboard controls still function.

(slaps forehead) Keyboard shortcuts and accelerators!
Of course, how silly of me to forget.

I'd be interested to hear how you go with my suggestion.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1
 
B

Bent C Dalager

Hi,

I'm developing an application that consists of three windows
(JFrames). Only one of the JFrames handles keyboard input.

At present, if I move one of the other JFrames to the front the
keyboard control stops. I assume that the front JFrame is getting the
keyboard focus.

I'd like to be able to move one of the other JFrames to the front but
still send the key commands to the back window.

How can I do this?

If you have your own custom key listeners already, then attach them to
all the frames rather than just one of them. If, on the other hand,
you just want the standard Swing stuff to happen, but happen to a
non-focused frame, you could experiment with the following.

I think I have seen various hackish ways of redirecting events in
Swing code that would pass events to different components by calling
e.g. processKeyEvent on them. You might override
JFrame.processKeyEvent on the frames that are not to handle key
presses and implement them along the lines of
processKeyEvent(KeyEvent e) { mainFrame.processKeyEvent(e); }

I don't know if you would have to massage the KeyEvent before passing
it on in this manner though.

Results may vary :)

(Of course, there must be a better way to do this - I just can't think
of one atm ...)

Cheers,
Bent D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top