频道栏目
首页 > 程序开发 > 软件开发 > Java > 正文
Java键盘事件监听(1)
2012-07-12 09:22:41           
收藏   我要投稿
\

[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; 
 
/**
 * 键盘事件监听(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  
                 
                // 写大写字母时,释放按键是不知道是否先释放的是shift还是字母按键;但是  
                // 可以肯定的是按按键时必须是先按的shift。  
                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.setDefaultCloseOperation(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;

/**
 * 键盘事件监听(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
    
    // 写大写字母时,释放按键是不知道是否先释放的是shift还是字母按键;但是
    // 可以肯定的是按按键时必须是先按的shift。
    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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setBounds(0, 0, 600, 300);
 }

}


 作者:Gaowen_HAN
 

点击复制链接 与好友分享!回本站首页
相关TAG标签 键盘 事件
上一篇:Java捕获窗口焦点事件
下一篇:Java键盘事件监听(2)
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站