Reputation: 409
I'd like to add keyboard shortcuts to my game that uses SWING. So far I've been using the following code:
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent e) { ... }
For testing purposes I'd like to create two instances of this application (testing network play). The problems is that the non-focused window grabs the focus when I use the hotkeys. (The window that was created later.)
I suspect that the problem is that
KeyboardFocusManager.getCurrentKeyboardFocusManager()
is a static method (both instances are started from the same JVM).
The only other option I could be thinking of was letting every component delegate their key-events to the ancestor JFrame, and handle them there at one place, but this would involve lots of boiler-place code, and I think this would be an ugly solution.
Also, the problems wouldn't occur during normal gameplay, it only annoys me while I'm testing.
Upvotes: 0
Views: 179
Reputation: 324108
I'd like to add keyboard shortcuts to my game that uses SWING
Then you should be using Key Bindings.
Upvotes: 5