All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object | +----jcsp.awt.event.KeyEventHandler
_________________ | | event | KeyEventHandler |--->--- |_________________|
The KeyEventHandler class implements the KeyListener interface and can be used for any Component which allows the addition of a KeyListener. When the Component is created a new instance of the KeyEventHandler should be created and added as a KeyListener to the Component. The process must be set running (either asynchronously using ProcessNetwork or as part of a Parallel construct).
The main body of this process waits to be notified by a component invoking the keyTyped(), keyPress() or keyRelease() methods. The process then wakes up and sends the KeyEvent object down the event Channel. Any further calls to the methods while the process is writing will be discarded so as not to block the main Java event Thread. When another process reads from the Channel this process loops round and waits to be notified again.
Input Channels | ||
---|---|---|
Output Channels | ||
event | KeyEvent | The KeyEvent generated by the Component. |
import java.awt.*; import jcsp.lang.*; import jcsp.awt.event.*; public class ChannelComponent extends Component implements CSProcess { KeyEventHanlder handler; public ChannelComponent() { } public void addComponentEventChannel(ChannelOutput event) { if (event !=null) { handler = new KeyEventHanlder(event); addKeyListener(handler); } } public void run() { if (handler != null) { handler.run(); } } }NOTE: This is in fact how the jcsp awt components add Channels for other event types.
import java.awt.*; import jcsp.awt.event.*; import jcsp.lang.*; : : { final Channel event = new One2OneChannel(); Component c = new Button("Active"); add(c); KeyEventHanlder handler = new KeyEventHanlder(event); c.addKeyListener(handler); new Parallel(new CSProcess[] { c, handler, new CSProcess() { public void run() { while (true) { Object o = event.read(); System.out.println("KeyEvent '" + o); } } } }).run(); } }
public KeyEventHandler(ChannelOutput event)
public void keyTyped(KeyEvent e)
public void keyPressed(KeyEvent e)
public void keyReleased(KeyEvent e)
public void run()
All Packages Class Hierarchy This Package Previous Next Index