JTextArea new line in shift + enter

I added a keyword to the JTextArea field, but it does not behave as I expected.

inputTextArea.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent k) { //If the return button is hit, only set to a new line if shift is also down. if(k.getKeyChar() == KeyEvent.VK_ENTER) { if(k.isShiftDown()) { inputTextArea.append(" \n"); } else { //Send The Message... boolean cleanTextField = false; try { sendMessage(inputTextArea.getText()); cleanTextField = true; msgScrollPane.setAutoscrolls(true); JScrollBar vbar = msgScrollPane.getVerticalScrollBar(); if ((vbar.getValue() + vbar.getVisibleAmount()) == vbar.getMaximum()) { msgPane.setCaretPosition(msgDoc.getLength()); } } catch (Exception ex) { ex.printStackTrace(); cleanTextField = false; } finally { if(cleanTextField) { inputTextArea.setText(""); } } } } } }); 

I want: - If the back button is pressed and shift down: add a new line. - If the return button is pressed and the shift button does not fall: there is no new line, but send.

Now it behaves like this: - If I press the return button and shift down: not a single line is added. Nothing has happened. - If I press the return button, and the shift will not be down: sent, but if I start typing again, it will start from a new line.

Does anyone know how to do what I want?

EDIT:

I tried another code to determine if the toggle button is off:

  if((k.getModifiersEx() == KeyEvent.SHIFT_DOWN_MASK) || (k.getModifiers() == KeyEvent.SHIFT_DOWN_MASK)) { 

That doesn't work either

+7
java jtextarea
source share
3 answers

You can use InputMap and ActionMap for JTextArea to map key strokes to actions:

 private static final String TEXT_SUBMIT = "text-submit"; private static final String INSERT_BREAK = "insert-break"; ... private void initialize() { InputMap input = inputTextArea.getInputMap(); KeyStroke enter = KeyStroke.getKeyStroke("ENTER"); KeyStroke shiftEnter = KeyStroke.getKeyStroke("shift ENTER"); input.put(shiftEnter, INSERT_BREAK); // input.get(enter)) = "insert-break" input.put(enter, TEXT_SUBMIT); ActionMap actions = inputTextArea.getActionMap(); actions.put(TEXT_SUBMIT, new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { submitText(); } }); } ... private void submitText() { // TODO } 

The original action for ENTER - "insert-break" - is used for shift ENTER .

+21
source share

Try using keyTyped rather than keyPressed. I beleive keyPressed gives you an event for change and for input, while keyTyped gives you one combined event with a modifier.

+1
source share

Instead of taking actions immediately after receiving the event, do it later by posting them using SwingUtilities.invokeLater (). The code should look like this:

 if(k.isShiftDown()) { SwingUtilities.invokeLater(new Runnable() { public void run() { inputTextArea.append(" \n"); } }); } else { SwingUtilities.invokeLater(new Runnable() { public void run() { //rest of the else body here } }); } 

In my opinion, the problems that arise here are related to the fact that the actions defined by the application and the internal actions are not ordered properly, which leads to the renaming that occurred before the text was changed.

0
source share

All Articles