Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

How to generate ALT+ENTER in java?

Thu Apr 30, 2009 11:50 am

How to detect that ALT and ENTER are pressed ?
:beg: This is the code to generate "ENTER" key but how if i want to combine ALT+ENTER so that if i press ALT+ENTER the text in txtAreaSend will move to the next line? I duno how to make the text move to next line as well...
Could anyone please help me? Thanks before..


Code:
int key = e.getKeyCode();
if(e.getSource() == txtAreaSend)
         {
             if(key == KeyEvent.VK_ENTER)
             {
                 sendMessage();
                 txtAreaSend.setText("");
             }
}




Re: [ask] How to generate ALT+ENTER in java?

Thu Apr 30, 2009 1:18 pm

did you try using?

Code:
[...]
if(key == KeyEvent.VK_ENTER && e.isAltDown() )
[...]


Re: [ask] How to generate ALT+ENTER in java?

Thu Apr 30, 2009 1:49 pm

I just tried it, but it doesn't work. any other alternative? :confused:

Solved: How to generate ALT+ENTER in java?

Sat May 09, 2009 4:01 pm

I've figured out how to solve this problem of mine. Here is the code
java code
import java.awt.event.*;
import javax.swing.*;

public class TextAreaCtrlEnter extends JFrame implements KeyListener, ActionListener{

JLabel lblDisplay;
JTextArea txtArea;
JButton btnSend;
JPanel panelObject;

public TextAreaCtrlEnter() {
panelObject = new JPanel();
getContentPane().add(panelObject);
lblDisplay = new JLabel("");
txtArea = new JTextArea(5,20);
btnSend = new JButton("Send");
panelObject.add(lblDisplay);
panelObject.add(txtArea);
panelObject.add(btnSend);
setVisible(true);
setSize(250,300);

txtArea.addKeyListener(this);
btnSend.addActionListener(this);

}

//KeyListener Interface
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() != 10){
//
}
else{
if(e.isControlDown() && e.getKeyCode() == 10){
String strBefore = txtArea.getText();
txtArea.setText(strBefore+"\n");
}
else{
lblDisplay.setText(txtArea.getText());
txtArea.setText("");
int caretPosition = txtArea.getCaretPosition()-1;
txtArea.setCaretPosition(caretPosition);
}

}
}

//ActionListener Interface
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnSend) {
lblDisplay.setText(txtArea.getText());
txtArea.setText("");
txtArea.requestFocus();
}
}

public static void main(String args[]) {
TextAreaCtrlEnter obj = new TextAreaCtrlEnter();
}
}

It need to be modified depending on the system u r making but overall the code is correct already. I hope it will be useful for anybody who is facing the same problem.

Post a reply
  Related Posts  to : How to generate ALT+ENTER in java?
 generate password using php     -  
 How to generate dynamic 3d pie chart in php???     -  
 Generate Unique Id As String     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 what is java     -  
 Java course     -  
 What is Java API?!!!     -  
 java or .net     -  
 need help in java     -  
 Using FTP in java     -  

Topic Tags

Java Events