Handling Mouse Events
To handle mouse events, you must implement tile Mouse Listener and the Mouse Motion Listener interfaces. The following applet demonstrates the process. It displays the current coordinates of the mouse in the applet’s status window. Each lime .a button is pressed, the word ‘.’Down” is displayed at the location of the mouse pointer. Each time the button is released, the word “Up” is shown. If a button is clicked, the message “Mouse clicked” is displayed in the upper-left comer of the applet display area. As the mouse enters or exits the applet window, a message is displayed in the upper-left comer of the applet display area. When dragging the mouse, a It is shown, which tracks with the mouse pointer as it is dragged. Notice that the two variables, mouse x and mouse Y, store the location of the when a mouse pressed, released, . or dragged event occurs. These coordinates are then used by paint( ) to display output at the point of these occurrences.
Let’s this example. The class extends Applet and implements both the Mouse Listener and Mouse These two interfaces contain methods that receive and process the various types of mouse events. Notice that the applet is both the source and the listener for these events. This works because Component, which supplies the and methods, is a of Applet. Being both source and the listener for events is a common situation for applets. Inside init(), the applet registers itself as a listener for mouse events. This is done by using which, as mentioned, are members of Component. They are shown here:
synchronized Here, 1111 is a reference to the object receiving mouse events, and is a to the object receiving mouse motion events. In this program, the same object is used for both.The applet then implements all of the methods defined by the Mouse Listener and Mouse interfaces. These are the event handlers for the various mouse
events. Each method handles its’ event and then returns.
Handling Keyboard Events
To handle keyboard events, you use the same general architecture as that shown in the mouse event example in the preceding section. The difference, of course, is that you will be implementing the Key Listener interface. Before looking at an example, it is useful to review how key events are generated. When a key is pressed, a KEY PRESSED event is generated. This results in a call to the key Presscd( ) event handler. When the key is released, a KEY_RELEASED event is
generated and the key Released( ) handler is executed. If a character is generated by the keystroke, then a TYPED event is sent and the key Typed() handler is invoked. each time the user presses a key, at least two and often three events are generated .If all you care about actual characters, then you can ignore the information passed by the key press and release events. However, if your program needs to handle special keys, such as the arrow or function keys, then it must watch for them through the key Pressed handler. There-is one other requirement that your program must meet before it can process keyboard events: it must request input focus. To do this, call request Focus which is defined by Component. If you don’t, then your program will not receive keyboard events.
H you want to handle the special keys, such as the arrow or function keys, you need to respond to them within the key Pressed( ) handler. They are not available through key Typed( ). To identify the keys, you use their virtuJI key codes. For example, the next applet outputs the name of a few of the special keys