下面列出了javax.swing.text.JTextComponent#KeyBinding ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void checkArray(JTextComponent.KeyBinding[] keyActionArray) {
if (keyActionArray.length != 1) {
throw new RuntimeException("Wrong array lenght!");
}
if (!DefaultEditorKit.upAction.equals(keyActionArray[0].actionName)) {
throw new RuntimeException("Wrong action name!");
}
if (!KeyStroke.getKeyStroke("UP").equals(keyActionArray[0].key)) {
throw new RuntimeException("Wrong keystroke!");
}
}
private static void checkArray(JTextComponent.KeyBinding[] keyActionArray) {
if (keyActionArray.length != 1) {
throw new RuntimeException("Wrong array lenght!");
}
if (!DefaultEditorKit.upAction.equals(keyActionArray[0].actionName)) {
throw new RuntimeException("Wrong action name!");
}
if (!KeyStroke.getKeyStroke("UP").equals(keyActionArray[0].key)) {
throw new RuntimeException("Wrong keystroke!");
}
}
private static void checkArray(JTextComponent.KeyBinding[] keyActionArray) {
if (keyActionArray.length != 1) {
throw new RuntimeException("Wrong array lenght!");
}
if (!DefaultEditorKit.upAction.equals(keyActionArray[0].actionName)) {
throw new RuntimeException("Wrong action name!");
}
if (!KeyStroke.getKeyStroke("UP").equals(keyActionArray[0].key)) {
throw new RuntimeException("Wrong keystroke!");
}
}
private static void checkArray(JTextComponent.KeyBinding[] keyActionArray) {
if (keyActionArray.length != 1) {
throw new RuntimeException("Wrong array lenght!");
}
if (!DefaultEditorKit.upAction.equals(keyActionArray[0].actionName)) {
throw new RuntimeException("Wrong action name!");
}
if (!KeyStroke.getKeyStroke("UP").equals(keyActionArray[0].key)) {
throw new RuntimeException("Wrong keystroke!");
}
}
private JTextComponent.KeyBinding[] getDefaultKeyBindings() {
return new JTextComponent.KeyBinding[] {
new JTextComponent.KeyBinding(KeyStroke.getKeyStroke("control Z"), undoAction),
new JTextComponent.KeyBinding(KeyStroke.getKeyStroke("control Y"), redoAction),
new JTextComponent.KeyBinding(KeyStroke.getKeyStroke("TAB"), indentAction),
new JTextComponent.KeyBinding(KeyStroke.getKeyStroke("shift TAB"), unindentAction)
};
}
/** Loads key to action mappings into this keymap
* @param bindings array of bindings
* @param actions map of [action_name, action] pairs
*/
public void load(JTextComponent.KeyBinding[] bindings, Map actions) {
// now create bindings in keymap(s)
for (int i = 0; i < bindings.length; i++) {
Action a = (Action)actions.get(bindings[i].actionName);
if (a != null) {
boolean added = false;
if (bindings[i] instanceof MultiKeyBinding) {
MultiKeyBinding mb = (MultiKeyBinding)bindings[i];
if (mb.keys != null) {
Keymap cur = delegate;
for (int j = 0; j < mb.keys.length; j++) {
if (j == mb.keys.length - 1) { // last keystroke in sequence
cur.addActionForKeyStroke(mb.keys[j], a);
} else { // not the last keystroke
Action sca = cur.getAction(mb.keys[j]);
if (!(sca instanceof KeymapSetContextAction)) {
sca = new KeymapSetContextAction(JTextComponent.addKeymap(null, null));
cur.addActionForKeyStroke(mb.keys[j], sca);
}
cur = ((KeymapSetContextAction)sca).contextKeymap;
}
}
added = true;
}
}
if (!added) {
if (bindings[i].key != null) {
delegate.addActionForKeyStroke(bindings[i].key, a);
} else { // key is null -> set default action
setDefaultAction(a);
}
}
}
}
}
private static void checkArray(JTextComponent.KeyBinding[] keyActionArray) {
if (keyActionArray.length != 1) {
throw new RuntimeException("Wrong array lenght!");
}
if (!DefaultEditorKit.upAction.equals(keyActionArray[0].actionName)) {
throw new RuntimeException("Wrong action name!");
}
if (!KeyStroke.getKeyStroke("UP").equals(keyActionArray[0].key)) {
throw new RuntimeException("Wrong keystroke!");
}
}
private static void checkArray(JTextComponent.KeyBinding[] keyActionArray) {
if (keyActionArray.length != 1) {
throw new RuntimeException("Wrong array lenght!");
}
if (!DefaultEditorKit.upAction.equals(keyActionArray[0].actionName)) {
throw new RuntimeException("Wrong action name!");
}
if (!KeyStroke.getKeyStroke("UP").equals(keyActionArray[0].key)) {
throw new RuntimeException("Wrong keystroke!");
}
}
private static void checkArray(JTextComponent.KeyBinding[] keyActionArray) {
if (keyActionArray.length != 1) {
throw new RuntimeException("Wrong array lenght!");
}
if (!DefaultEditorKit.upAction.equals(keyActionArray[0].actionName)) {
throw new RuntimeException("Wrong action name!");
}
if (!KeyStroke.getKeyStroke("UP").equals(keyActionArray[0].key)) {
throw new RuntimeException("Wrong keystroke!");
}
}
private static void checkArray(JTextComponent.KeyBinding[] keyActionArray) {
if (keyActionArray.length != 1) {
throw new RuntimeException("Wrong array lenght!");
}
if (!DefaultEditorKit.upAction.equals(keyActionArray[0].actionName)) {
throw new RuntimeException("Wrong action name!");
}
if (!KeyStroke.getKeyStroke("UP").equals(keyActionArray[0].key)) {
throw new RuntimeException("Wrong keystroke!");
}
}
private static void checkArray(JTextComponent.KeyBinding[] keyActionArray) {
if (keyActionArray.length != 1) {
throw new RuntimeException("Wrong array lenght!");
}
if (!DefaultEditorKit.upAction.equals(keyActionArray[0].actionName)) {
throw new RuntimeException("Wrong action name!");
}
if (!KeyStroke.getKeyStroke("UP").equals(keyActionArray[0].key)) {
throw new RuntimeException("Wrong keystroke!");
}
}
private static void checkArray(JTextComponent.KeyBinding[] keyActionArray) {
if (keyActionArray.length != 1) {
throw new RuntimeException("Wrong array lenght!");
}
if (!DefaultEditorKit.upAction.equals(keyActionArray[0].actionName)) {
throw new RuntimeException("Wrong action name!");
}
if (!KeyStroke.getKeyStroke("UP").equals(keyActionArray[0].key)) {
throw new RuntimeException("Wrong keystroke!");
}
}
private static void checkArray(JTextComponent.KeyBinding[] keyActionArray) {
if (keyActionArray.length != 1) {
throw new RuntimeException("Wrong array lenght!");
}
if (!DefaultEditorKit.upAction.equals(keyActionArray[0].actionName)) {
throw new RuntimeException("Wrong action name!");
}
if (!KeyStroke.getKeyStroke("UP").equals(keyActionArray[0].key)) {
throw new RuntimeException("Wrong keystroke!");
}
}
private static void checkArray(JTextComponent.KeyBinding[] keyActionArray) {
if (keyActionArray.length != 1) {
throw new RuntimeException("Wrong array lenght!");
}
if (!DefaultEditorKit.upAction.equals(keyActionArray[0].actionName)) {
throw new RuntimeException("Wrong action name!");
}
if (!KeyStroke.getKeyStroke("UP").equals(keyActionArray[0].key)) {
throw new RuntimeException("Wrong keystroke!");
}
}
private static void checkArray(JTextComponent.KeyBinding[] keyActionArray) {
if (keyActionArray.length != 1) {
throw new RuntimeException("Wrong array lenght!");
}
if (!DefaultEditorKit.upAction.equals(keyActionArray[0].actionName)) {
throw new RuntimeException("Wrong action name!");
}
if (!KeyStroke.getKeyStroke("UP").equals(keyActionArray[0].key)) {
throw new RuntimeException("Wrong keystroke!");
}
}
protected JTextComponent.KeyBinding[] getKeyBindings() {
List<JTextComponent.KeyBinding> keyBindings = new ArrayList<JTextComponent.KeyBinding>();
keyBindings.addAll(Arrays.asList(getDefaultKeyBindings()));
keyBindings.addAll(Arrays.asList(getCustomKeyBindings()));
return keyBindings.toArray(new JTextComponent.KeyBinding[keyBindings.size()]);
}
/**
* JTextPane上でCommand+Cとかでコピペをできるようにする
*/
private void addShortcutForMac() {
if (!PacketProxyUtility.getInstance().isMac()) {
return;
}
JPanel p = (JPanel) getContentPane();
InputMap im = p.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = p.getActionMap();
int hotkey = (KeyEvent.CTRL_MASK | KeyEvent.META_MASK);
registerTabShortcut(KeyEvent.VK_H, hotkey, im, am, Panes.HISTORY.ordinal());
registerTabShortcut(KeyEvent.VK_I, hotkey, im, am, Panes.INTERCEPT.ordinal());
registerTabShortcut(KeyEvent.VK_R, hotkey, im, am, Panes.REPEATER.ordinal());
registerTabShortcut(KeyEvent.VK_B, hotkey, im, am, Panes.BULKSENDER.ordinal());
registerTabShortcut(KeyEvent.VK_O, hotkey, im, am, Panes.OPTIONS.ordinal());
registerTabShortcut(KeyEvent.VK_L, hotkey, im, am, Panes.LOG.ordinal());
JTextComponent.KeyBinding[] bindings1 = {
new JTextComponent.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
DefaultEditorKit.copyAction),
new JTextComponent.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_V, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
DefaultEditorKit.pasteAction),
new JTextComponent.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_X, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
DefaultEditorKit.cutAction),
new JTextComponent.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_A, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
DefaultEditorKit.selectAllAction),
};
JTextPane component_tp = new JTextPane();
Keymap keymap_tp = component_tp.getKeymap();
JTextComponent.loadKeymap(keymap_tp, bindings1, component_tp.getActions());
JTextField component_tf = new JTextField();
Keymap keymap_tf = component_tf.getKeymap();
JTextComponent.loadKeymap(keymap_tf, bindings1, component_tf.getActions());
JTextArea component_ta = new JTextArea();
Keymap keymap_ta = component_ta.getKeymap();
JTextComponent.loadKeymap(keymap_ta, bindings1, component_ta.getActions());
}
/** Constructor for existing KeyBinding */
public MultiKeyBinding(JTextComponent.KeyBinding kb) {
this(kb.key, kb.actionName);
}
@Override
protected KeyBinding[] getCustomKeyBindings() {
return new JTextComponent.KeyBinding[] {
new JTextComponent.KeyBinding(KeyStroke.getKeyStroke("control F"), findReplaceAction),
};
}
protected JTextComponent.KeyBinding[] getCustomKeyBindings() {
return new JTextComponent.KeyBinding[0];
}