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

241 views Asked by At

Looks like option up can be caught only with AWT (i.e., adding key listener). Is there a way to do it without having to mess around with AWT?

1

There are 1 answers

1
trashgod On BEST ANSWER

The Mac OS X UI delegate for JTextField does not bind alt to any Action. You are free to bind the combination to any action you choose. As shown in How to Use Key Bindings, the example below binds alt to the existing "caret-begin-line" defined for JTextField, which moves the caret 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 may be found here. As shown in the key binding utility cited here, JTextField binds the following WHEN_FOCUSED actions by name to the indicated key(s).

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