您的位置:首页 > 编程语言 > Java开发

《Java 编程技巧1001条》第415条:使用keyDown方法

2017-12-25 08:56 471 查看



《Java 编程技巧1001条》第11章 事件驱动
 第415条 使用keyDown方法 

415 Using the keyDown Method
415 使用键盘事件
As you have learned, Java provides convenience methods that make capturing 
mouse events easy. For example, when the user holds down the mouse button, 
Java will call the mouseDown method. Similarly, when the user presses a 
keyboard key, Java will call the keyDown convenience method. The arguments 
to the keyDown method are the Event object and a key code. The following 
program, keyDownApplet.java, demonstrates how to capture key strokes and 
display them within the status window:
正如你所了解的那样,Java提供的convenience方法使得事件的捕捉变得容易. 例如,
用户按下mouse键时,Java将调用mouseDown方法. 类似地,当用户按键盘上的键时,
Java将调用keyDown方法. keyDown方法的自变量是Event对象和所按键的代码. 以下
keyDownApplet.java程序说明了捕捉按键并将它们显示在状态窗口中: 
import java.applet.*;
import java.awt.*;

public class keyDownApplet extends Applet {

   public boolean keyDown(Event evt, int code)
     {
       System.out.println("keyDown:" + (char)code);
       return(true);
     }
 }

415 Using the keyDown Method
415 使用键盘事件
As you have learned, Java provides convenience methods that make capturing 
mouse events easy. For example, when the user holds down the mouse button, 
Java will call the mouseDown method. Similarly, when the user presses a 
keyboard key, Java will call the keyDown convenience method. The arguments 
to the keyDown method are the Event object and a key code. The following 
program, keyDownApplet.java, demonstrates how to capture key strokes and 
display them within the status window:
正如你所了解的那样,Java提供的convenience方法使得事件的捕捉变得容易. 例如,
用户按下mouse键时,Java将调用mouseDown方法. 类似地,当用户按键盘上的键时,
Java将调用keyDown方法. keyDown方法的自变量是Event对象和所按键的代码. 以下
keyDownApplet.java程序说明了捕捉按键并将它们显示在状态窗口中: 
import java.applet.*;
import java.awt.*;

public class keyDownApplet extends Applet {

   public boolean keyDown(Event evt, int code)
     {
       System.out.println("keyDown:" + (char)code);
       return(true);
     }
 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: