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

《Java 编程技巧1001条》第418条:检测多个修饰键的另一方法

2017-12-25 09:14 411 查看



《Java 编程技巧1001条》第11章 事件驱动 第418条 检测多个修饰键的另一方法 

418 An Alternative Way to Detect Keyboard Modifier Keys
418 检测键盘变动键的另一种方法
In the previous tip, you learned how to detect keyboard modifier keys by examining the Event class modifiers variable. An alternative way to check for modifier keys is to use the methods built into the Event class: controlDown, metaDown, or shiftDown. You can use these methods instead  of performing a bitwise AND operation of the modifier constants with the modifiers variable. The following program, altModifierKey.java, demonstrates how to check for modifier keys using the built-in Event class methods:在以上的TIP中,你已知道了怎样利用查看事件类modifier变量的办法来检测键盘的修饰键. 检测修饰键的另一中办法是在Event类中建立的controlDown, metaDown, 或shiftDown三种方法. 你可使用这些方法来代替odifiers变量和modifiers常量之间的按位乘操作. 以下的程序altModifierKey.java说明怎样利用内建的Evenr类方法,来检测修饰键:  
import java.applet.*;
import java.awt.*;

public class altModifierKey extends Applet {

    public void init()
      {
         resize(400, 300);
      }

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