看起来只有up可以使用AWT来捕获选项(即添加键侦听器).有没有一种方法可以不必麻烦AWT?
解决方法:
用于JTextField的Mac OS X UI委托不会将alt↑绑定到任何Action
.您可以*地将组合绑定到您选择的任何操作.如How to Use Key Bindings所示,下面的示例将alt↑绑定到为JTextField定义的现有“插入符号开始行”,它将插入符号移动到行的开头.
final JTextField jtf = new JTextField("Test");
jtf.getInputMap().put(
KeyStroke.getKeyStroke(KeyEvent.VK_UP, KeyEvent.ALT_MASK),
"caret-begin-line");
可以找到更多示例here.如引用为here的键绑定实用程序中所示,JTextField按名称将以下WHEN_FOCUSED操作绑定到所指示的键.
beep caret-backward LEFT, KP_LEFT, ctrl B caret-begin ctrl P, meta UP, meta KP_UP, HOME caret-begin-line KP_UP, ctrl A, UP, meta KP_LEFT, meta LEFT caret-begin-paragraph caret-begin-word caret-down caret-end ctrl N, END, meta KP_DOWN, ctrl V, meta DOWN caret-end-line DOWN, meta KP_RIGHT, ctrl E, meta RIGHT, KP_DOWN caret-end-paragraph caret-end-word caret-forward RIGHT, ctrl F, KP_RIGHT caret-next-word alt KP_RIGHT, alt RIGHT caret-previous-word alt KP_LEFT, alt LEFT caret-up copy copy-to-clipboard meta C, COPY cut cut-to-clipboard CUT, meta X default-typed delete-next DELETE, ctrl D delete-next-word alt DELETE delete-previous BACK_SPACE, ctrl H delete-previous-word alt BACK_SPACE, ctrl W dump-model insert-break insert-content insert-tab notify-field-accept ENTER page-down page-up paste paste-from-clipboard meta V, PASTE requestFocus select-all meta A select-line select-paragraph select-word selection-backward shift LEFT, shift KP_LEFT selection-begin shift meta KP_UP, shift meta UP, shift HOME selection-begin-line shift UP, shift meta KP_LEFT, shift KP_UP, shift meta LEFT selection-begin-paragraph selection-begin-word selection-down selection-end shift meta DOWN, shift meta KP_DOWN, shift END selection-end-line shift meta KP_RIGHT, shift DOWN, shift KP_DOWN, shift meta RIGHT selection-end-paragraph selection-end-word selection-forward shift KP_RIGHT, shift RIGHT selection-next-word shift alt KP_RIGHT, shift alt RIGHT selection-page-down shift PAGE_DOWN selection-page-left shift meta PAGE_UP selection-page-right shift meta PAGE_DOWN selection-page-up shift PAGE_UP selection-previous-word shift alt LEFT, shift alt KP_LEFT selection-up set-read-only set-writable toggle-componentOrientation shift ctrl O unselect meta BACK_SLASH