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

《Java 编程技巧1001条》 第411条: 检测鼠标单击修饰键,

2017-12-24 20:42 441 查看


《Java 编程技巧1001条》第11章 事件驱动 第411条 检测鼠标单击修饰键《Java 编程技巧1001条》

411 Detecting Mouse Click Modifier Keys
411 检测鼠标点击修饰键
From the time of the first graphical-user interface, there have been mouse-click and key combinations that users perform for special purposes. For example, in many graphical programs, drawing with the Shift key depressed causes an oval tool to draw circles. 
为了实现特殊的用途,自从第一个图形用户接口诞生时起,就有把鼠标键和键盘键组合在一起的用法. 例如,在大多数图形程序中, 如果鼠标作图时同时按下了Shift键,将使椭圆工具(oval tool)画成圆.
To detect a mouse-click modifier key within a Java applet, you check the Event class modifiers variable. Java defines a set of constants in the Event class that your programs can use to check for a specific modifier key such as SHIFT_MASK, CTRL_MASK, and ALT_MASK. To determine if the user has pressed a specific key, you perform a bitwise AND operation of the corresponding constant and the modifier variable. The following program, singleModifier.java, demonstrates how your programs detect if a user 
modified the mouse operation by holding down the Shift key:
在Java应用程序中,为了检测鼠标点击修饰键,你可检查Event类的modifiers变量. Java在事件类中定义了一组供你用来检测特殊修饰键的常量,如SHIFT_MASK, CTRL_MASK,和 ALT_MASK. 为了检测用户是否已按下某个特殊键,你可以对modifiers变量和对应的常量实行按位乘(AND)运算. 以下的singleModifier.java程序告诉你怎样检查用户
在利用mouse操作时是否同时按下了Shift键.
import java.awt.*;
import java.applet.*;
  
public class singleModifier extends Applet {

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