《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);
}
}
相关文章推荐
- 《Java 编程技巧1001条》第417条: 检测多个修饰键
- 《Java 编程技巧1001条》第418条:检测多个修饰键的另一方法
- 《Java 编程技巧1001条》第410条: 检测双击的另一种方法
- 《Java 编程技巧1001条》第419条:检测专用键
- 《Java 编程技巧1001条》第412条...: 了解多按钮鼠标问题
- 《Java 编程技巧1001条》 第423条: 检测窗口何时已图标化(Iconified)
- 《Java 编程技巧1001条》第416条:使用keyUp方法
- 《Java 编程技巧1001条》 第420...条: 使用handle event 方法
- 《Java 编程技巧1001条》 第392条: 了解2分查找
- 《Java 编程技巧1001条》 第403条: 了解事件类
- 《Java 编程技巧1001条》第409...条: 使用crickCount方法,
- 《Java 编程技巧1001条》 第387条: 建立多维数组
- 《Java 编程技巧1001条》 第399条:对原始类型数组使用数组复制
- 《Java 编程技巧1001条》 第404条: 了解实用方法
- 《Java 编程技巧1001条》 第390条: 初始化多维数组
- 《Java 编程技巧1001条》 第393条: 使用2分查找
- 《Java 编程技巧1001条》第391条: 在数组中查找一指定值
- 《Java 编程技巧1001条》 第405条: 使用mouseDoun方法
- 《Java 编程技巧1001条》第421条:如何不处理事件
- 《Java 编程技巧1001条》 第388条: 遍历多维数组