Total members 11894 |It is currently Thu Nov 21, 2024 10:40 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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("");
             }
}





Author:
Newbie
User avatar Posts: 16
Have thanks: 0 time

did you try using?

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



Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

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


Author:
Newbie
User avatar Posts: 16
Have thanks: 0 time

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.


Author:
Newbie
User avatar Posts: 16
Have thanks: 0 time
Post new topic Reply to topic  [ 4 posts ] 

  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     -  
 Using FTP in java     -  
 what is java     -  
 Java course     -  
 What is Java API?!!!     -  
 java or .net     -  
 need help in java     -  



Topic Tags

Java Events
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com