[英]Replace Keybinding In Text Field With Existing Action
I have a JTextPane
.我有一个JTextPane
。 I want my own behavior for Ctrl+Arrow
presses.我想要我自己的Ctrl+Arrow
按下行为。
jTextEntryPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, Toolkit.getDefaultToolkit ().getMenuShortcutKeyMaskEx()), Controls.UP_ACTION);
jTextEntryPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, Toolkit.getDefaultToolkit ().getMenuShortcutKeyMaskEx()), Controls.DOWN_ACTION);
jTextEntryPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, Toolkit.getDefaultToolkit ().getMenuShortcutKeyMaskEx()), Controls.LEFT_ACTION);
jTextEntryPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, Toolkit.getDefaultToolkit ().getMenuShortcutKeyMaskEx()), Controls.RIGHT_ACTION);
jTextEntryPane.getActionMap().put(Controls.UP_ACTION, new MoveAction(Controls.UP_ACTION, this));
jTextEntryPane.getActionMap().put(Controls.DOWN_ACTION, new MoveAction(Controls.DOWN_ACTION, this));
jTextEntryPane.getActionMap().put(Controls.LEFT_ACTION, new MoveAction(Controls.LEFT_ACTION, this));
jTextEntryPane.getActionMap().put(Controls.RIGHT_ACTION, new MoveAction(Controls.RIGHT_ACTION, this));
Ctrl+Up
and Ctrl+Down
work. Ctrl+Up
和Ctrl+Down
工作。
Ctrl+Left
and Ctrl+Right
do not, but instead are being used by the text field (to go to the beginning or end of the input field). Ctrl+Left
和Ctrl+Right
没有,而是被文本字段使用(转到输入字段的开头或结尾)。 I want to replace their behavior, but I do not want to change the regular arrow (left and right) behavior.我想替换他们的行为,但我不想改变常规箭头(左和右)的行为。 How do I do this?我该怎么做呢?
Ctrl+Up and Ctrl+Down are currently not defined as key bindings for the text field so you are just adding new bindings, which is why they work. Ctrl+Up 和 Ctrl+Down 当前未定义为文本字段的键绑定,因此您只是添加新绑定,这就是它们起作用的原因。
However, Ctrl+Left and Ctrl+Right are already defined as bindings for a JTextField.但是,Ctrl+Left 和 Ctrl+Right 已经定义为 JTextField 的绑定。 They are defined for the WHEN_FOCUSED
InputMap which has priority over the WHEN_IN_FOCUSED_WINDOW
InputMap.它们是为WHEN_FOCUSED
InputMap 定义的,它的优先级高于WHEN_IN_FOCUSED_WINDOW
InputMap。
jTextEntryPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)....
You are using the wrong InputMap for all your bindings.您为所有绑定使用了错误的 InputMap。
You should be using:你应该使用:
jTextEntryPane.getInputMap()....
which will use the WHEN_FOCUSED
InputMap.这将使用WHEN_FOCUSED
InputMap。
This will allow you to replace the default Action.这将允许您替换默认操作。
See Key Bindings for a simple program that displays all the default bindings for each component.有关显示每个组件的所有默认绑定的简单程序,请参阅键绑定。
Edit:编辑:
Tabbing is not handled by the Key Bindings.键绑定不处理选项卡。 It is handled by the focus subsystem, so the event is intercepted before it gets to the text field.它由焦点子系统处理,因此事件在到达文本字段之前被拦截。 I believe the following should work:我相信以下应该有效:
textField.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null);
textField.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.