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

《Java 编程技巧1001条》第419条:检测专用键

2017-12-25 09:18 477 查看



《Java 编程技巧1001条》第11章 事件驱动 第419条 检测专用键

419 Detecting Special Keys
419 检测特殊键
You have learned how to detect normal and modifier keys. For special keys, such as the keyboard arrow and function keys, you must check the key code like you did for normal keys. However, instead of using the ASCII codes, you use the special key constants defined in the Event class. Table 419 lists the Event class special key constants .
你已知道了怎样来检测用户按的正常键和修饰键. 对于键盘上的箭头键和功能键之类的特殊键,你必须象检测正常键那样地来查看它们的代码. 但是,你可以不直接使用它们的ASCII码,而改用由Event类定义的特殊键常量. 表419列出了Event类定义的特殊键常量. 

Special Keys  Description(特殊键描述)——————————————————————F1, F2, F3, F4, F5, F6 —— Function keys(功能键)
F7, F8, F9, F10, F11, F12 —— Function keys(功能键)
Left, Right —— Left and right arrow keys(向左和向右键)
Up, Down —— Up and down arrow keys(向上向下键)

Home, End —— Home and End keys(行首与行尾键)

PgUp, PgDown —— PageUp and PageDown keys(上页与下页键)
——————————————————————————————Table 419 Event class key constants.
表419 Event类定义的特殊键常量

The following program, testApplet.java, demonstrates how to detect special keys.
以下的程序testApplet.java, 说明了怎样检测特殊键:

import java.applet.*;
import java.awt.*;

public class testApplet extends Applet {

    public void init()
      {
         resize(400, 300);
      }
 
    public boolean keyDown(Event evt, int key)
      {
        switch (key) {
            case Event.UP:    System.out.println("Up arrow");
                              break;
            case Event.DOWN:  System.out.println("Down arrow");
                              break;
            case Event.LEFT:  System.out.println("Left arrow");
                              break;
            case Event.RIGHT: System.out.println("Right arrow");
                              break;
         }
        return(true);
      }
  }
  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: