?? e862. finding a key binding in a component.txt
字號:
Home > List of Packages > javax.swing [141 examples] > Keystrokes and Input Maps [6 examples]
e862. Finding a Key Binding in a Component
This example searches all of a component's inputmaps and keymaps (if the component is a text component) for a particular keystroke.
FindResult r = find(KeyStroke.getKeyStroke("ctrl pressed C"), component);
r = find(KeyStroke.getKeyStroke("ctrl released C"), component);
r = find(KeyStroke.getKeyStroke("C"), component);
r = find(KeyStroke.getKeyStroke("typed C"), component);
r = find(KeyStroke.getKeyStroke(new Character('\u0002'), 0), component);
// The results of a find are returned in a FindResult object
static class FindResult {
// Non-null if the keystroke is in an inputmap
InputMap inputMap;
// Non-null if the keystroke is in an keymap or default action
Keymap keymap;
// Non-null if the keystroke is in a default action
// The keymap field holds the keymap containing the default action
Action defaultAction;
// If true, the keystroke is in the component's inputMap or keymap
// and not in one of the inputMap's or keymap's parent.
boolean isLocal;
public String toString() {
StringBuffer b = new StringBuffer();
b.append("inputmap="+inputMap+",keymap="+keymap
+",defaultAction="+defaultAction+",isLocal="+isLocal);
return b.toString();
}
}
// Returns null if not found
public static FindResult find(KeyStroke k, JComponent c) {
FindResult result;
result = find(k, c.getInputMap(JComponent.WHEN_FOCUSED));
if (result != null) {
return result;
}
result = find(k, c.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT));
if (result != null) {
return result;
}
result = find(k, c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW));
if (result != null) {
return result;
}
// Check keymaps
if (c instanceof JTextComponent) {
JTextComponent tc = (JTextComponent)c;
result = new FindResult();
// Check local keymap
Keymap kmap = tc.getKeymap();
if (kmap.isLocallyDefined(k)) {
result.keymap = kmap;
result.isLocal = true;
return result;
}
// Check parent keymaps
kmap = kmap.getResolveParent();
while (kmap != null) {
if (kmap.isLocallyDefined(k)) {
result.keymap = kmap;
return result;
}
kmap = kmap.getResolveParent();
}
// Look for default action
if (k.getKeyEventType() == KeyEvent.KEY_TYPED) {
// Check local keymap
kmap = tc.getKeymap();
if (kmap.getDefaultAction() != null) {
result.keymap = kmap;
result.defaultAction = kmap.getDefaultAction();
result.isLocal = true;
return result;
}
// Check parent keymaps
kmap = kmap.getResolveParent();
while (kmap != null) {
if (kmap.getDefaultAction() != null) {
result.keymap = kmap;
result.defaultAction = kmap.getDefaultAction();
return result;
}
kmap = kmap.getResolveParent();
}
}
}
return null;
}
public static FindResult find(KeyStroke k, InputMap map) {
// Check local inputmap
KeyStroke[] keys = map.keys();
for (int i=0; keys != null && i<keys.length; i++) {
if (k.equals(keys[i])) {
FindResult result = new FindResult();
result.inputMap = map;
result.isLocal = true;
return result;
}
}
// Check parent inputmap
map = map.getParent();
while (map != null) {
keys = map.keys();
for (int i=0; keys != null && i<keys.length; i++) {
if (k.equals(keys[i])) {
FindResult result = new FindResult();
result.inputMap = map;
return result;
}
}
map = map.getParent();
}
return null;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -