JAVA键盘事件监听(1)

转载自:http://blog.csdn.net/gaowen_han/article/details/7738655

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值