Re: How can a JFrame be aware of keys pressed within controls?
"John B. Matthews" <nospam@nospam.invalid> escribi? en el mensaje
news:nospam-2CDD93.17493017022010@news.aioe.org...
IIUC, the snippet in the "Focus Subsystem" article above uses the
Container's setFocusTraversalKeys() method to add a new KeyStroke;
no new listener is required:
Just for the record, it worked with this:
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class ToggleEnter {
public static void activate(Container c) {
Set forwardKeys =
c.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
Set newForwardKeys = new HashSet(forwardKeys);
newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
newForwardKeys);
}
public static void deactivate(Container c) {
Set forwardKeys =
c.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
Set newForwardKeys = new HashSet(forwardKeys);
try {
newForwardKeys.remove(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
newForwardKeys);
}
catch (Exception ex) {
System.err.println("Exception en ToggleEnter.deactivate: " +
ex.getMessage());
}
}
}
I activate VK_ENTER from the constructor, deactivate it on entry to any
control that requires it (like multiline text editors) and activate it back
on focusLost for those controls.
I haven't any real app written in Java yet, so I guess I have time to
develop a set of descedants of the basic controls in order to add whatever
additional functionality I might require.
Thanks, again.