Hello I'm trying to add the select-all, cut, copy and paste command in my application, I managed to work out the cut, copy and paste command but I don't seem to have figured out how to add the cmd-a command...
this worked for my cmd-x command
text.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_X, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "cut");
but when I try this for cmd-a:
text.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_A, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "select");
it doesn't seem to work...
I read something about using this void to select the text, but I don't know how to bind it to the command+a command
myTextfield.selectAll();
Anyone got an idea on how to correctly implement this?
Use the correct String
: "select-all"
instead of "select"
.
You can discover these String
s by investigating the InputMap
, for example by using:
public static void main( String[] args ) {
EventQueue.invokeLater( new Runnable() {
@Override
public void run() {
JTextField textField = new JTextField();
InputMap inputMap = textField.getInputMap( JComponent.WHEN_FOCUSED );
KeyStroke[] keyStrokes = inputMap.allKeys();
for ( int i = 0; i < keyStrokes.length; i++ ) {
KeyStroke keyStroke = keyStrokes[ i ];
Object value = inputMap.get( keyStroke );
System.out.println(keyStroke + "-\"" + value + "\"");
}
}
} );
}
which prints out (not the complete output pasted here)
ctrl pressed BACK_SPACE-"delete-previous-word"
ctrl pressed A-"select-all"
shift pressed KP_RIGHT-"selection-forward"
This shows the String
you are looking for is "select-all"
.
Note that it might be really counter-intuitive for a user to switch the function of the ctrl key and the cmd key . For example on a Mac the cmd+A by default does select all (which is expected), but I would never expect that on a Windows/Linux machine