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
java jtextarea
dododedodonl
source share