How can I map an option action (Mac keyboard) to a JTextField?

It looks like the up option can only be captured from AWT (i.e. with the addition of a keyboard listener). Is there a way to do this without having to mess with AWT?

+3


source to share


1 answer


The UI divider for Mac OS X for JTextField

doesn't snap alt to anyone Action

. You can bind a combination to any action you choose. As shown in How to Use Key Bindings , the example below binds alt to an existing "caret-begin-line"

one defined for JTextField

, which moves the cursor to the beginning of the line.

final JTextField jtf = new JTextField("Test");
jtf.getInputMap().put(
    KeyStroke.getKeyStroke(KeyEvent.VK_UP, KeyEvent.ALT_MASK),
    "caret-begin-line");

      



More examples can be found here . As shown in the Key Binding Utility provided here , JTextField

binds the following actions WHEN_FOCUSED

by name to the specified keys.

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
+3


source







All Articles