Getting started with Java Applet
YY435
Pacific Network Academy
Fourth day
Mouse and keyboard event handler
The so-called event means that there are some things we care in the system (for example: mouse moving, the user presses a button ..., etc.), and then the system will notify us to deal with these things. Such concepts are very common in the operating system that generally adopts the window user interface environment.
In Java, all events are defined within the class EVENT (Java.awt.event), if the reader is interested, you can refer to the Java API.
1) Mouse event.
Mouse time is generally: mousedown: When the button is pressed, the mouse_down time occurs, this time the system is called method MOUSEDOWN to process the event. Method MOUSEDOWN is:
Public Boolean MouseDown (Event E, INT X, INT Y) {......
It can be seen from the above, and the method mousedown has three parameters: E, X, Y. E is the event itself, we look "event" as an object entity, but its class is Event, at the same time, the event is not different from the usual object entity. The X, Y is the coordinate value of the mouse when this time occurs. It is important to note that its method return value is the value of the Boolean type, you have to pay attention! In addition, since there are only one button of some machines, considering the Java's and platform-independent, in this method, there is no function of our common right click. It does not distinguish around the button.
Mouseup: In contrast, this method is the process function when the mouse is released, and for use, the same is identical to MouseDown. I don't have much to say here!
Mousemove: mouse_move and mouse_drag are events generated by mouse movements, and method MouseMove is used to process mouse_move events. The method is:
Public Boolean MouseMove (Event EVT, INT X, INT Y) {......}
The parameters and methods used by MOUSEDOWN and MOUSEUP are exactly the same.
MouseDrag: From the above, this method is of course used to handle the mouse_drag event. The method is:
Public Boolean MouseDrag (Event E, INT X, INT Y) {.....}
MouseEnter: This method is used to handle the mouse_enter event. When the mouse enters the window range of the small application, the mouse_enter event occurs. The method of processing is:
Public Boolean (Event EVT, INT X, INT Y) {...
The parameters used are the same as the previous, not much.
MouseExit: This method is used to handle mouse_exit events. When the mouse is removed from the small application window, a mouse_exit event occurs. The method of processing is:
Public Boolean (Event E, INT X, INT Y) {....
Ok, let's take a look at the specific example: [Please click here to enter]
2) Keyboard events: Compared with the mouse, the time generated by the keyboard is much simple. Generally speaking, what we care about is what the user presses, and then processes, but does not have to have a mobile event like the mouse. deal with. Its main events are:
Key_Press: Generate when pressing; key_release: When the keyboard button is released. Treating them method methods are: public boilean keydown (Event EVT, INT X, INT Y) {...}
Public Loolean Keyup (Event EVT, INT X, INT Y) {......}
The EVT in the parameter is still an object of the event itself, and Key is the case that is pressed or released by the user. Java has already defined some of the key values of the key in class Event, as follows:
The key variable represented by the key variable represented the "upper" "" button Event.f3 "F3" key event.f4 "F4" button Event.FT direction The "left" "left" end. Event.f5 "right" key Event.f6 "f6" "" "button Event.home" Home "key Event.end" F7 "key event.end" End.f8 "f8" key Event.pgup "Page Up" Event.f9 "f9" Event.pgdn "Page Down" Event.f10 "f10" key Event.f1 "f1" key Event.f11 "F11" key Event.f2 "f2" button Event.f12 "f12" key