替换文本字段中的按键绑定为现有操作。

huangapple go评论78阅读模式
英文:

Replace Keybinding In Text Field With Existing Action

问题

我有一个 JTextPane。我希望在按下 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+UpCtrl+Down 可以正常工作。

Ctrl+LeftCtrl+Right 不起作用,而是被文本字段使用(用于移到输入字段的开头或结尾)。我希望替换它们的行为,但不想改变常规的箭头(左和右)行为。我该如何做到这一点?

英文:

I have a JTextPane. I want my own behavior for Ctrl+Arrow presses.

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+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). 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?

答案1

得分: 2

Ctrl+Up和Ctrl+Down目前未定义为文本字段的键绑定,因此您只是在添加新的绑定,这就是为什么它们起作用。

然而,Ctrl+Left和Ctrl+Right已经定义为JTextField的绑定。它们是在WHEN_FOCUSED输入映射中定义的,该映射的优先级高于WHEN_IN_FOCUSED_WINDOW输入映射。

jTextEntryPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)....

您正在为所有绑定使用错误的输入映射。

您应该使用:

jTextEntryPane.getInputMap()....

这将使用WHEN_FOCUSED输入映射。

这将允许您替换默认的操作。

有关每个组件显示所有默认绑定的简单程序,请参见键绑定

编辑:

Tab键的处理不是由键绑定处理的。它是由焦点子系统处理的,因此事件在到达文本字段之前被拦截。我相信以下内容应该会起作用:

textField.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null);
textField.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null);
英文:

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.

However, Ctrl+Left and Ctrl+Right are already defined as bindings for a JTextField. They are defined for the WHEN_FOCUSED InputMap which has priority over the WHEN_IN_FOCUSED_WINDOW InputMap.

jTextEntryPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)....

You are using the wrong InputMap for all your bindings.

You should be using:

jTextEntryPane.getInputMap()....

which will use the 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);

huangapple
  • 本文由 发表于 2020年9月17日 05:30:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63928201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定