Event Processing

How can you capture keyboard events?


(submitted by Mike M.)
Capturing keyboard events is a rather simple task for an applet.
public void keyDown(Event e, int key) { }
is the standard method for this routine, with the parameter "key" representing the ASCII value of the depressed key.
keyDown
will remain idle until a key is depressed and will gain control if no other dominant threads are running.
(Submitted by Nelson Yu)
To handle keyboard events you can do the following:

public boolean handleEvent(Event evt)
{
    switch(evt.id)
    {
        // We return false because we want Java to continue handling
        // the events
        case Event.KEY_ACTION:
        {
             System.out.println("Keyboard event");
             return false;
        }
        case Event.KEY_PRESS:
        {
            System.out.println("Key was pressed"):
            return false;
        }
        default:
             return false;

}

Component encapsulates key events in the methods keyDown() and keyUp();



What is the framework of the basic event-driven application?



How do you post an event?



How can you define an event and respond to it?


(Submitted by Vivek Pabby)
The following code in an applet will create a new event for the applet and handle it. Any component can post this event to the applet. (Also, using the same technique, any component can be extended with user defined events.)

Select to see code template

Select to see an example


What is the deliverEvent() method for? How is it different from postEvent()?


(Submitted by Vivek Pabby)
To the best of my limited knowledge:

The deliverEvent() method, when invoked on a component is executed immediately. It overrides the normal event queue for the GUI components.

In the normal GUI event handling mechanism, the events get "posted" to an event queue and are handled on a FIFO basis. The deliveEvent() method will make the component handle the event out of the normal queue sequence.

The postEvent() method will post a specified event to a component's event queue and it will be handled after the other events in the queue (if any) are handled.


How can a Component instance notify an enclosing Container that it has received and handled an event, such as a button press?


(Submitted by Vivek Pabby)
There are two ways for a component to let the container know that it has handled an event that it reveived.

1. The event handling functions in the component class (like action(...), handleEvent(...) ) return a boolean value. If the value returned from those functions is true, the event is considered to be handled and is taken off the "event queue" of the component.

2. You can use a mixture of control variables etc to keep track if an event has been handled by a component or not. THIS IS NOT A CLEAN SOLUTION.

I would recommend using the first approach. The code below, if compiled and executed, will show three buttons that will illustrate the event handling procedure.

The first button is a simple Button constructed using the Button class. The simple Button class provided by java will pass on the clicked event to the container. You can test this by clicking on the button.

The second button is constructed using an extension to the Button class. It handles the event and DOES NOT "flag" the event as handled. ( it returns false from the action(...) method ) So the container handles that event again because it has not been taken off the event queue.

The third button is constructed using another extension to the Button class. It handles the event and DOES "flag" the event as handled. ( it returns true from the action(...) method ) So the container does not handle that event again because it has been taken off the event queue.


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

class buttonOne extends Button
{
	TextArea ta1 = null;

	public buttonOne(String s, TextArea ta) {
		super(s);
		ta1 = ta;
	}
	public boolean action(Event e, Object o) {
		ta1.appendText("\n" + "Event handled by COMPONENT - Button 2" + "\n");
		return false;
	}
}

class buttonTwo extends Button
{
	TextArea ta1 = null;

	public buttonTwo(String s, TextArea ta) {
		super(s);
		ta1 = ta;
	}
	public boolean action(Event e, Object o) {
		ta1.appendText("\n" + "Event handled by COMPONENT - Button 3" + "\n");
		return true;
	}
}

public class easyButtons extends Applet 
{
	Label l1 = null;
	TextArea ta1 = null;

        public void init() {
        }
	public easyButtons() {
		setLayout(new BorderLayout());
		Panel p1 = new Panel();
		p1.setLayout(new FlowLayout());
		add("North", p1);
		ta1 = new TextArea(10, 40);			
 		p1.add(new Button("SimpleButton"));	 
 		p1.add(new buttonOne("Button 2", ta1));	 
 		p1.add(new buttonTwo("Button 3", ta1));	 
		p1.add(ta1);
	}
	public boolean action(Event e, Object o) {
		ta1.appendText("Event handled by CONTAINER" + "\n");
		return true;
	}
	public static void main(String args[]) {
		Frame f1 = new Frame("Standalone Application");
		easyButtons s1 = new easyButtons();
		s1.init();
		s1.start();
	        f1.add("Center", s1);
	        f1.resize(300, 300);
	        f1.show();	
	}
}

How do I force an Event to be sent immediately to an applet, even (especially) when there are other cpu-intensive applets running?



What is the order in which events are passed to handleEvent(), action(), and the component-specific event methods such as keyDown()?