KeyListner是一个继承了Event接口的接口,我们可以通过实现KeyListenre接口来监听键盘按键
public interface KeyListener extends EventListener {
/**
* Invoked when a key has been typed.
* See the class description for {@link KeyEvent} for a definition of
* a key typed event.
*/
public void keyTyped(KeyEvent e);
/**
* Invoked when a key has been pressed.
* See the class description for {@link KeyEvent} for a definition of
* a key pressed event.
*/
public void keyPressed(KeyEvent e);
/**
* Invoked when a key has been released.
* See the class description for {@link KeyEvent} for a definition of
* a key released event.
*/
public void keyReleased(KeyEvent e);
}
我们可以看到,这个接口中有三个方法,也就意味着在实现这个接口时我们要重写这三个方法,而这三个方法的参数都是KeyEvent类的对象。
KeyEvent类中定义了很多常量,每个常量其实就是键盘上的一个键,每个键都有对应的数字来赋值,当我们按下,释放,敲击键盘上的一个键时,就会调用KeyEvent,比如按下一个键时会调用KeyPressed方法,释放一个键时会调用KeyReleased方法。每个keyEvent都有一个相关的按键字符和按键代码,分别由getkeyChar()方法和getkeyCode方法返回.
例如:
当我们按下键盘的“A”键时,此时就会触发KeyEvent,并且返回其对应的按键字符和按键代码,下面举几个常见的常量:
VK_HOME Home键 VK_CONTROL 控制键
VK_END End键 VK_SHIFT shift键
VK_PGUP page up键 VK_BACK_SPACE 退格键
VK_PGDN page down键 VK_CAPS_LOCK 大小写锁定键
VK_UP 上箭头 VK_NUM_LOCK 小键盘锁定键
VK_DOWN 下箭头 VK_ENTER 回车键
VK_LEFT 左箭头 VK_UNDEFINED 未知键
VK_RIGHT 右箭头 VK_F1--VK_F12 F1 -- F12
VK_ESCAPE Esc键 VK_0 --VK_9 0 --- 9
VK_TAB Tab键 VK_A --VK_Z A----Z
KeyEvent类通常配合KeyListenr类使用
要说KeyListener的作用,其实也就是其三个方法的作用
注:KeyListener不能单独使用,必须使用组件当作中间媒介(就好比监视器要装在房屋上一样)
KeyListener的使用流程:
public class MyListener implements KeyListener() {
pulbic void keyPressed() {
........................
}
pulbic void keyTyped() {
........................
}
pulbic void keyRealsed() {
..........................
}
}
JFrame jf = new JFrame()
jf.add(new MyListener);
总结:我们可以把KeyListener想象成一个监听功能的接口,任何实现了MyListener接口的类相当于一个监视器,但监视器本身无法发挥作用,它需要寄托在房屋或者电线杆这样的中间媒介,也就是容器内才能发挥作用,所用我们要将它添加进组件(Applet、JFrame等等),这样这个组件就有了监听的功能了。
package practice;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class MyListener implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
System.out.println("Key Type Event ");
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("Key Press Event ");
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println("Key Release Event ");
}
public static void main(String[] args) {
JFrame jf = new JFrame("MyListener");
jf.addKeyListener(new MyListener());
jf.setBounds(300,300,800,600);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
Key Type Event
然后再键入“A”,控制台打印:
Key Type Event
Key Press Event
Key Type Event
Key Release Event
然后再键入“Shift”,控制台打印:
Key Type Event
Key Press Event
Key Type Event
Key Release Event
Key Press Event
Key Release Event
Key Press Event
Key Release Event
package practice;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class MyListener implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
System.out.println("Key Type Event ");
System.out.println("Key Code: " + e.getKeyCode());
System.out.println("Key Char: " + e.getKeyChar());
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("Key Press Event ");
}
@Override
public void keyReleased(KeyEvent e) {
}
public static void main(String[] args) {
JFrame jf = new JFrame("MyListener");
jf.addKeyListener(new MyListener());
jf.setBounds(300,300,800,600);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
我们输入“a”后
控制台:
Key Press Event
Key Type Event
Key Code: 0
Key Char: a
package practice;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class MyListener implements KeyListener {
static JLabel jl = new JLabel();
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
jl.setLocation(jl.getX()-10,jl.getY());
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
jl.setLocation(jl.getX()+10,jl.getY());
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
jl.setLocation(jl.getX(),jl.getY()-10);
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
jl.setLocation(jl.getX(),jl.getY()+10);
}
}
@Override
public void keyReleased(KeyEvent e) {
}
public static void main(String[] args) {
JFrame jf = new JFrame("Moving Ball");
jf.addKeyListener(new MyListener());
jf.setBounds(300,300,800,600);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon pic = new ImageIcon("C:\\Users\\Chen YI\\Desktop\\Game素材\\病毒.png");
jl.setIcon(pic);
jf.add(jl);
jf.setVisible(true);
}
}
效果图: