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

《Java 编程技巧1001条》第410条: 检测双击的另一种方法

2017-12-24 20:34 375 查看



《Java 编程技巧1001条》第11章 事件驱动 第410条 检测双击的另一种方法
410 An Alternative Way to Detect Double-Clicks

410 检测Double-Clicks的另一种方法
In Tip 409, you learned how to detect double-click operations using the Event class clickCount variable. Unfortunately, depending on your release of Java, you may find that clickCount does not always contain the correct value. An alternative way to detect double-click mouse operations is to determine the time interval between clicks yourself. To do this, you can check the Event class when variable. If two mouse clicks occur within a specific interval of time, your applet can treat the mouse clicks as a double-click operation. The following program, altDoubleClick.java, demonstrates how you might check the interval between mouseDown events using the when variable:
在TIP 409中,你已知道了怎样利用clickCount变量来检测double-click操作. 不幸的是,利用clickCount变量来检测double-click操作,在不同的Java版本下,并不总是提供可靠的值. 检测double-click操作的另一种方法是由你自己来确定击键的时间间隔. 为了做这一事,你可检测Event类. 如果两次mouse 点击事件出现在一个指定的时间间隔内,你的程序就可把它当作一个double-click来处理. 以下的altDouble-Click.java程序将告诉你怎样利用when变量来检测两次mouseDown事件之间的时间间隔:

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

public class altDoubleClick extends Applet {
 
   static int lastclick = 0;
   static long maxdelay = 100;
   static int doubleclick = 0;

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

   public boolean mouseDown(Event evt, int x, int y)
     {
       long delay = evt.when - lastclick;

       if ((delay > maxdelay) || (doubleclick == 0))
         doubleclick = 1;
       else
         doubleclick++;

       if (evt.clickCount == 1)
         System.out.println("mouseDown: single click");
       else if (evt.clickCount == 2)
         System.out.println("mouseDown: double click");
       else if (evt.clickCount == 3)
         System.out.println("mouseDown: tripple click");
       else if (evt.clickCount > 3)
         System.out.println("mouseDown: many clicks");

       return(true);
    }
 }on most operating systems, a user can set the sensitivity for double 
clicks. However, the when variable technique in this tip has no way to 
adjust sensitivity based on the operating-system settings. 
在大多数操作系统中,用户可以设置double clicks的灵敏性. 但是,对本TIP中使
用的when变量没有办法利用操作系统的设置来调整灵敏度.

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);
    }
 }

412 Understanding the Multi-Button Mouse Problem
412 了解多按钮(Multi-Button)鼠标问题
In the previous tips, you have learned how to detect when the user clicks 
the mouse. On some platforms, the mouse has more than one button. 
Unfortunately, Java does not provide a clear way to distinguish between 
mouse buttons. In fact, to date, only undocumented techniques existwhich 
might not work in future versions of Java. Also, if you create a program 
that requires more than one mouse button, your program will be platform 
dependent. Until a solution is built into the language, we recommend that 
you just assume that there is only one mouse button. With that said, the 
following program, multiMouse.java, will now demonstrate an undocumented 
technique for detecting the second and third mouse button: 
在前面的TIP中,你已知道了怎样检测用户是否已按下mouse键. 在有的平台上,mouse
可以有多个button. 不幸的是, Java不提供区别mouse button的确切方法. 事实上,
到目前为止,只有不成文的技术在用, 它们对今后的Java版本有可能不使用. 同样,如
果你创建一个要使用更多button的程序,你的程序就可能和平台有关. 这个问题在语
言中的解决方案出来之前,我们建议你就假设只有一个mouse button. 据此, 以下的
这一个程序, multiMouse.java, 现在只是用来检测第二和第三个mouse button的一
种不规范的方法:
import java.applet.*;
import java.awt.*;

public class multiMouse extends Applet {

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

   public boolean mouseDown(Event evt, int x, int y)
     {
       if ((evt.modifiers & Event.ALT_MASK) > 0)
          System.out.println("mouseDown: middle button");
       else if ((evt.modifiers & Event.META_MASK) > 0)
          System.out.println("mouseDown: right button");
       else
          System.out.println("mouseDown: left button");

       return(true);
     }
 }
As you can see, the program detects the other mouse buttons by checking the 
modifier keys. Currently, the other mouse buttons are equivalent to a 
standard mouse click with a modifier key pressed. Remember, however, these 
techniques may not work in future versions of Java.
你可看到,程序检测其它的mouse button 是利用了modifier键. 这里,其它的mouse
button等价为点击mouse同时按下了一个modifier键. 但要记住, 这些方法在今后的
Java版本中可能不使用.

413 Using mouseEnter and mouseExit
413 使用mouseEnter和mouseExit

You have learned that your applets can handle mouseMove events when a user 
moves a mouse around the screen. Within your programs, you can use the 
event coordinates to manually check when a mouse enters a window or 
control. However, Java has two functions that can do this for you: 
mouseEnter and mouseExit. Java calls the mouseEnter when a mouse enters a 
component. Likewise, Java calls the mouseExit when the mouse leaves that 
component. For example, you might use these methods to display a pop-up 
help message that tells a user about a particular control or button. The 
following program, mouseEnterExit.java, demonstrates how your programs can 
detect when a mouse enters and leaves an applet window:
你已知道了,当用户利用mouse在屏幕上移动mouse光标时,你的应用程序是怎样来处理
mouseMove事件的. 在你的程序中,你可以利用mouse事件的坐标来检测一个mouse是进
入一个窗口或者是一个控制对象图标. 但是,Java有两个供你使用的功能:mouseEnter
和 mouseExit. Java在mouse进入某一成员范围时调用mouseEnter,同样, Java在mouse
离开某一成员范围时调用了mouseExit. 例如,你可以用这些方法来显示一个弹出式消
息,告诉用户有关的特殊控制或按钮. 以下的mouseEnterExit.java程序说明了你的程
序怎样来检测mouse何时进入/退出一个应用程序窗口:
import java.awt.*;
import java.applet.*;
  
public class mouseEnterExit extends Applet {

   public boolean mouseEnter(Event evt, int x, int y)
     {
        System.out.println("mouseEnter");
        return(true);
     }

   public boolean mouseExit(Event evt, int x, int y)
     {
        System.out.println("mouseExit");
        return(true);
     }
 }
The first message will cause the status window (the window that displays 
the message "mouseEnter") to be in front of the applet. For the mouseEnter 
and mouseExit methods to work, you must click the applet so that it becomes 
the front-most window. 
这里的第一个消息将使状态窗口(显示消息"mouseEnter"的窗口)变为应用程序的front
窗口. 为使mouseEnter 和 mouseExit 方法工作,你必须点击你的应用程序,使它成为
最前端的那个窗口.

414 Understanding Keyboard Events
414 了解键盘事件
In the previous tips, you learned about the events that are generated by 
the mouse. As you will learn, another set of important events is generated 
from the keyboard. When Java generates a keyboard event, it passes an Event 
object that contains information about the key pressed. Java recognizes 
normal keys, modifier keys, and special keys. For normal keys, the event 
object contains the keys ASCII value. For the function, arrow, and other 
special keys, there are no ASCII codes, so Java uses special Java-defined 
codes. In Tip 411, you learned how to detect modifier keys. The modifier 
keys are stored in the modifiers variable in the Event object. The 
following tips describe how to handle various keyboard events.
在以上的TIP中,你已知道了由mouse生成的有关事件. 你将会了解, 另一类重要的事件
是由键盘产生的.当Java产生一键盘事件时,它传递了包含有关按下键信息的事件对象.
Java将辨识出所按的一般的键还是修饰键,或者还是特殊键. 对于功能键,光标移动用
的箭头键,以及另一些特殊键,它们是没有自己的ASCII码的,所以Java使用了由自己定
义的专用代码. 在TIP 411中你将知道怎样检测修饰键. 修饰键存储在Event对象的修
饰字变量(modifiers variable)中. 以下的TIPS描述了怎样处理各种键盘事件.

415 Using the keyDown Method
415 使用键盘事件
As you have learned, Java provides convenience methods that make capturing 
mouse events easy. For example, when the user holds down the mouse button, 
Java will call the mouseDown method. Similarly, when the user presses a 
keyboard key, Java will call the keyDown convenience method. The arguments 
to the keyDown method are the Event object and a key code. The following 
program, keyDownApplet.java, demonstrates how to capture key strokes and 
display them within the status window:
正如你所了解的那样,Java提供的convenience方法使得事件的捕捉变得容易. 例如,
用户按下mouse键时,Java将调用mouseDown方法. 类似地,当用户按键盘上的键时,
Java将调用keyDown方法. keyDown方法的自变量是Event对象和所按键的代码. 以下
keyDownApplet.java程序说明了捕捉按键并将它们显示在状态窗口中: 
import java.applet.*;
import java.awt.*;

public class keyDownApplet extends Applet {

   public boolean keyDown(Event evt, int code)
     {
       System.out.println("keyDown:" + (char)code);
       return(true);
     }
 }

416 Using the keyUp Method
416 使用keyUp方法
As you have learned, Java provides separate events for mouse-down and 
mouse-up operations. Thus, it should not surprise you that Java provides 
separate events for key-down and key-up operations. When the user releases 
a keyboard key, Java calls the keyUp convenience method. The arguments to 
the keyUp method are the Event object and a key code. The following 
program, keyUpApplet.java, demonstrates how to detect a key up event:
正如你所了解的那样,Java为mouse-down和mouse-up操作独立地提供了事件. 因此,你
也不会奇怪Java为key-down和key-up操作提供两类不同的事件. 当用户释放某个键时,
Java调用了key-Up convenience 方法. key-Up方法的自变量是Event对象和所按键的
代码. 以下keyUpApplet.java程序说明了怎样检测按键的释放事件:  
import java.applet.*;
import java.awt.*;

public class keyUpApplet extends Applet {

   public boolean keyDown(Event evt, int code)
     {
       System.out.println("keyDown:" + (char) code);
       return(true);
    }

   public boolean keyUp(Event evt, int code)
     {
       System.out.println("keyUp:" + (char) code);
       return(true);
     }
 }

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);
      }
  }

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三种方法. 你可使用这些方法来代替modifiers变量和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);
      }
  }

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);
      }
  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: