Applicationwide key event listener and modal dialogs

Hi,

I want to register an application-wide key-listener for my Swing application, to open a dialog when e.g. the F5 key is pressed.

My application uses a JFrame with a DesktopPane and InternalFrames.

Some application dialogs are displayed as JDialogs (with the application frame as their parent) and not as InternalFrames.

When using InputMap and ActionMap on a Component of the application I always miss keystrokes. When registering keystrokes to the InputMap of the DesktopPane I will not receive the events when a JDialog or the applicationframe itself has the focus.

So I registered my Listener with KeyboardFocusListener.addKeyEventPostProcessor....

This works so I can catch all key events.

The problem I have is, that the action in the postprocessor will open new dialogs even if a modal dialog is active. This should not happen.

How can I avoid opening a new dialog when a modal dialog is open?

Or is there a better way to register an application wide keyevent listener?

Thank you a lot for helping me.

Christian

[1093 byte] By [chrgoosea] at [2007-11-26 17:21:54]
# 1

> How can I avoid opening a new dialog when a modal dialog is open?

I don't think there's a built-in way to detect if a modal dialog is showing on screen. You'll have to track this yourself.

> Or is there a better way to register an application wide keyevent listener?

This is probably the best way, though you may still not see some events if they're consumed prior to the post processor being called. You may want to use a KeyEventDispatcher, though it suffers from a similar restriction. I don't see how this relates to the above problem though; how you process key events has no affect on Dialog behavior.

Jasprea at 2007-7-8 23:49:53 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for your answer,

the way I process key events has some effect on application behavior.

If I use the InputMap / ActionMap mechanism to register keystroke listeners I don't receive the keyevents in the parent window when a modal dialog is showing.

JDialog blocks the EventDispatchThread in this case.

I was just wondering if there was a way to do the same thing with the postprocessor without having to care for modal dialogs myself.

Christian

chrgoosea at 2007-7-8 23:49:53 > top of Java-index,Desktop,Core GUI APIs...
# 3

> If I use the InputMap / ActionMap mechanism to

> register keystroke listeners I don't receive the

> keyevents in the parent window when a modal dialog is

> showing.

of course, sorry, my brain is a bit sluggish this morning...

> JDialog blocks the EventDispatchThread in this case.

well, no. JDialog does not block the EDT - if it did, your JDialog wouldn't be able to paint along with other undesirable effects. JDialog basically intercepts incoming events.

> I was just wondering if there was a way to do the

> same thing with the postprocessor without having to

> care for modal dialogs myself.

Well, theoretically (seriously i haven't tried this) if you're receiving KeyEvents then one of the application windows is in the foreground. And if that's the case and you have a modal dialog, the focus owner is probably the modal dialog or a child of it. Therefore, you can do this in your KeyEvent processing method:

Window w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();

if (w instanceof Dialog && ((Dialog)w).isModal()) {

return; // don't do anything

}

// do other stuff

Jasprea at 2007-7-8 23:49:53 > top of Java-index,Desktop,Core GUI APIs...
# 4
Thanks again,I will try this (your brain works well enough to help me solve the problem ;-)Christian
chrgoosea at 2007-7-8 23:49:53 > top of Java-index,Desktop,Core GUI APIs...
# 5
> I want to register an application-wide key-listener for my Swing application,See replies 2, 3, and 5 for 3 different ways: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=565854
camickra at 2007-7-8 23:49:53 > top of Java-index,Desktop,Core GUI APIs...