Java keyboard event listening (1)

Source: Internet
Author: User


[Java]
Package com. han;
 
Import java. awt. Container;
Import java. awt. event. KeyEvent;
Import java. awt. event. KeyListener;
 
Import javax. swing. JFrame;
Import javax. swing. JLabel;
Import javax. swing. JScrollPane;
Import javax. swing. JTextArea;
Import javax. swing. SpringLayout;
 
/**
* Keyboard event listening (1)
* @ Author HAN
*
*/
Public class KeyEvent_1 extends JFrame {
 
/**
*
*/
Private static final long serialVersionUID =-6762512303624322086L;
 
Public KeyEvent_1 (){
// TODO Auto-generated constructor stub
Container container = getContentPane ();
SpringLayout springLayout = new SpringLayout ();
Container. setLayout (springLayout );
JLabel remarqueLabel = new JLabel ("remarque :");
JTextArea textArea = new JTextArea (3, 30 );
JScrollPane scrollPane = new JScrollPane ();
ScrollPane. setViewportView (textArea );
Container. add (remarqueLabel );
SpringLayout. putConstraint (SpringLayout. WEST, remarqueLabel, 5,
SpringLayout. WEST, container );
SpringLayout. putConstraint (SpringLayout. NORTH, remarqueLabel, 5,
SpringLayout. NORTH, container );
Container. add (scrollPane );
SpringLayout. putConstraint (SpringLayout. WEST, scrollPane, 5,
SpringLayout. EAST, remarqueLabel );
SpringLayout. putConstraint (SpringLayout. NORTH, scrollPane, 5,
SpringLayout. NORTH, container );
SpringLayout. putConstraint (SpringLayout. EAST, scrollPane,-5,
SpringLayout. EAST, container );
SpringLayout. putConstraint (SpringLayout. SOUTH, scrollPane,-5,
SpringLayout. SOUTH, container );
 
TextArea. addKeyListener (new KeyListener (){
 
@ Override
Public void keyTyped (KeyEvent e ){
// TODO Auto-generated method stub
System. out. println ("The charactor you typed:" + "\""
+ E. getKeyChar () + "\"");
}
 
@ Override
Public void keyPressed (KeyEvent e ){
// TODO Auto-generated method stub

// When writing uppercase letters, do you know whether to release shift or letter keys first?
// It is certain that the first shift is required when you press the key.
If (e. getKeyCode () = KeyEvent. VK_CONTROL |
E. getKeyCode () = KeyEvent. VK_SHIFT |
E. getKeyCode () = KeyEvent. VK_ALT ){
System. out. println ("\ n" + "The Component triggered:" + e. getSource ());
}
If (! (E. isControlDown () | e. isShiftDown () | e. isAltDown ())){
System. out. println ("\ n" + "The Component triggered:" + e. getSource ());
}
String keyText = KeyEvent. getKeyText (e. getKeyCode ());
If (e. isActionKey ()){
System. out. print ("The action key you pressed:" + "\""
+ KeyText + "\"");
} Else {
System. out. print ("The non-action key you pressed:" + "\""
+ KeyText + "\"");
Int keyCode = e. getKeyCode ();
Switch (keyCode ){
Case KeyEvent. VK_CONTROL:
System. out. print (", Control key is pressed ");
Break;
Case KeyEvent. VK_SHIFT:
System. out. print (", Shift key is pressed ");
Break;
Case KeyEvent. VK_ALT:
System. out. print (", Alt key is pressed ");
Break;
}
}
System. out. println ();
}
 
@ Override
Public void keyReleased (KeyEvent e ){
// TODO Auto-generated method stub
// The integer code for an actual key on the keyboard.
// (For KEY_TYPED events, the keyCode is VK_UNDEFINED .)
System. out. println ("The key you released:" + "\" "+
KeyEvent. getKeyText (e. getKeyCode () + "\"");
}
 
});
}
 
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
KeyEvent_1 frame = new KeyEvent_1 ();
Frame. setTitle ("KeyEvent Test ");
Frame. setVisible (true );
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
Frame. setBounds (0, 0,600,300 );
}
 
}

Package com. han;

Import java. awt. Container;
Import java. awt. event. KeyEvent;
Import java. awt. event. KeyListener;

Import javax. swing. JFrame;
Import javax. swing. JLabel;
Import javax. swing. JScrollPane;
Import javax. swing. JTextArea;
Import javax. swing. SpringLayout;

/**
* Keyboard event listening (1)
* @ Author HAN
*
*/
Public class KeyEvent_1 extends JFrame {

/**
*
*/
Private static final long serialVersionUID =-6762512303624322086L;

Public KeyEvent_1 (){
// TODO Auto-generated constructor stub
Container container = getContentPane ();
SpringLayout springLayout = new SpringLayout ();
Container. setLayout (springLayout );
JLabel remarqueLabel = new JLabel ("remarque :");
JTextArea textArea = new JTextArea (3, 30 );
JScrollPane scrollPane = new JScrollPane ();
ScrollPane. setViewportView (textArea );
Container. add (remarqueLabel );
SpringLayout. putConstraint (SpringLayout. WEST, remarqueLabel, 5,
SpringLayout. WEST, container );
SpringLayout. putConstraint (SpringLayout. NORTH, remarqueLabel, 5,
SpringLayout. NORTH, container );
Container. add (scrollPane );
SpringLayout. putConstraint (SpringLayout. WEST, scrollPane, 5,
SpringLayout. EAST, remarqueLabel );
SpringLayout. putConstraint (SpringLayout. NORTH, scrollPane, 5,
SpringLayout. NORTH, container );
SpringLayout. putConstraint (SpringLayout. EAST, scrollPane,-5,
SpringLayout. EAST, container );
SpringLayout. putConstraint (SpringLayout. SOUTH, scrollPane,-5,
SpringLayout. SOUTH, container );

TextArea. addKeyListener (new KeyListener (){

@ Override
Public void keyTyped (KeyEvent e ){
// TODO Auto-generated method stub
System. out. println ("The charactor you typed:" + "\""
+ E. getKeyChar () + "\"");
}

@ Override
Public void keyPressed (KeyEvent e ){
// TODO Auto-generated method stub

// When writing uppercase letters, do you know whether to release shift or letter keys first?
// It is certain that the first shift is required when you press the key.
If (e. getKeyCode () = KeyEvent. VK_CONTROL |
E. getKeyCode () = KeyEvent. VK_SHIFT |
E. getKeyCode () = KeyEvent. VK_ALT ){
System. out. println ("\ n" + "The Component triggered:" + e. getSource ());
}
If (! (E. isControlDown () | e. isShiftDown () | e. isAltDown ())){
System. out. println ("\ n" + "The Component triggered:" + e. getSource ());
}
String keyText = KeyEvent. getKeyText (e. getKeyCode ());
If (e. isActionKey ()){
System. out. print ("The action key you pressed:" + "\""
+ KeyText + "\"");
} Else {
System. out. print ("The non-action key you pressed:" + "\""
+ KeyText + "\"");
Int keyCode = e. getKeyCode ();
Switch (keyCode ){
Case KeyEvent. VK_CONTROL:
System. out. print (", Control key is pressed ");
Break;
Case KeyEvent. VK_SHIFT:
System. out. print (", Shift key is pressed ");
Break;
Case KeyEvent. VK_ALT:
System. out. print (", Alt key is pressed ");
Break;
}
}
System. out. println ();
}

@ Override www.2cto.com
Public void keyReleased (KeyEvent e ){
// TODO Auto-generated method stub
// The integer code for an actual key on the keyboard.
// (For KEY_TYPED events, the keyCode is VK_UNDEFINED .)
System. out. println ("The key you released:" + "\" "+
KeyEvent. getKeyText (e. getKeyCode () + "\"");
}

});
}

/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
KeyEvent_1 frame = new KeyEvent_1 ();
Frame. setTitle ("KeyEvent Test ");
Frame. setVisible (true );
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
Frame. setBounds (0, 0,600,300 );
}

}


Author: Gaowen_HAN
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.