《Java 编程技巧1001条》第417条: 检测多个修饰键
2017-12-25 09:08
357 查看
《Java 编程技巧1001条》第11章 事件驱动 第417条 检测多个修饰键
417 Detecting Multiple Modifier Keys
417 检测多重变动键
In Tip 414, you learned that Java supports three types of keyboard characters. The first type are normal keys, which you learned to handle in the previous two tips. Another type of keys are the modifier keys. In Tip 415, you learned how to detect if the user has pressed a modifier key during a mouse click. Now, you will learn how to detect if multiple modifier keys were pressed at the same time.
在TIP 414中,你已知道了Java支持三种类型的键盘字符. 第一种类型是正常的键,这一种类型你已在前两个TIP中学过了. 另一种类型的键是修饰键,TIP 415中你已学过了怎样检测用户在击mouse键时按下了一个修饰键. 现在,你将学到怎样检测用户是否同时按下多个修饰键.
The Event class modifiers variable is a bit field that lets a variable contain multiple flags. Within the modifiers variable, Java sets one bit for each of the Shift, Ctrl, and Alt keys. Within your program, you can test which modifiers are set by performing a bitwise AND operation of the modifier constant and the modifiers variable. The following program, multipleModifier.java, demonstrates how to detect the Shift and Ctrl modifier keys when a normal key has been pressed:
Event类的modifiers(修饰键)变量是一个bit域,这样在一个变量中可容纳多个标记.在modifiers变量中,Java为Shift, Ctrl 和 Alt三个键分别设置一个bit. 在你的程序中,你可以对modifiers变量和modifiers常量实行按位乘(AND)操作,来检侧哪一个modifiers(修饰键)被设置了. 以下的程序multipleModifier.java说明怎样来检测用户在按正常键的同时也按下了Shift和Ctrl修饰键:
import java.applet.*;
import java.awt.*;
public class mutitipleModifier extends Applet {
public void init()
{
resize(400, 300);
}
public boolean keyDown(Event evt, int key)
{
if (((evt.modifiers & Event.SHIFT_MASK) > 0) &&
((evt.modifiers & Event.CTRL_MASK) > 0))
System.out.println("SHIFT and CTRL pressed");
return(true);
}
}
相关文章推荐
- 《Java 编程技巧1001条》第418条:检测多个修饰键的另一方法
- 《Java 编程技巧1001条》 第411条: 检测鼠标单击修饰键,
- 《Java 编程技巧1001条》第410条: 检测双击的另一种方法
- 《Java 编程技巧1001条》第419条:检测专用键
- 《Java 编程技巧1001条》 第423条: 检测窗口何时已图标化(Iconified)
- 《Java 编程技巧1001条》 第382+283条: 了解数组的长度和引用
- 《Java 编程技巧1001条》第401条:数组中存不同对象
- 《Java 编程技巧1001条》第416条:使用keyUp方法
- 《Java 编程技巧1001条》 第420...条: 使用handle event 方法
- 《Java 编程技巧1001条》 第389条: 了解不平衡数组
- 《Java 编程技巧1001条》 第395与396条 :了解和使用冒泡排序
- 《Java 编程技巧1001条》 第385条: 了解多维数组
- 《Java 编程技巧1001条》第409...条: 使用crickCount方法,
- 《Java 编程技巧1001条》 第386条: 声明多维数组
- 《Java 编程技巧1001条》 第397+398条:了解和使用快速排序
- 《Java 编程技巧1001条》第402-406条: 了解事件驱动程序,
- 《Java 编程技巧1001条》第421条:如何不处理事件
- 《Java 编程技巧1001条》 第392条: 了解2分查找
- 《Java 编程技巧1001条》 第399条:对原始类型数组使用数组复制
- 《Java 编程技巧1001条》 第403条: 了解事件类