All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class jcsp.awt.event.KeyEventHandler

java.lang.Object
   |
   +----jcsp.awt.event.KeyEventHandler

public class KeyEventHandler
extends Object
implements CSProcess, KeyListener

Process Diagram

   _________________
  |                 | event
  | KeyEventHandler |--->---
  |_________________|
 

Description

The KeyEventHandler class is not intended to be used by software constructors, instead it is used in the construction of new Active AWT Components that generate KeyEvents (i.e. ActiveButton, ActiveScrollbar).

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.

Channel Protocols

Input Channels
Output Channels
event KeyEvent The KeyEvent generated by the Component.

Example

Extending an Existing 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.

Listening to a Component

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

Author:
P.D.Austin

Constructor Index

 o KeyEventHandler(ChannelOutput)
constructs a new KeyEventHandler with the specified output Channel.

Method Index

 o keyPressed(KeyEvent)
Invoked when the Component the event handler is listening to has a key pressed.
 o keyReleased(KeyEvent)
Invoked when the Component the event handler is listening to has a key released.
 o keyTyped(KeyEvent)
Invoked when the Component the event handler is listening to has a key pressed and then released.
 o run()
The main body of the process.

Constructors

 o KeyEventHandler
 public KeyEventHandler(ChannelOutput event)
constructs a new KeyEventHandler with the specified output Channel.

Parameters:
event - The Channel to send the event notification down

Methods

 o keyTyped
 public void keyTyped(KeyEvent e)
Invoked when the Component the event handler is listening to has a key pressed and then released. Notifies the event process that a KeyEvent has occurred by sending the KeyEvent Object. Some notifications will be lost so there are no guarantees that all events generated will be processed.

Parameters:
e - The parameters associated with this event
 o keyPressed
 public void keyPressed(KeyEvent e)
Invoked when the Component the event handler is listening to has a key pressed. Notifies the event process that a KeyEvent has occurred by sending the KeyEvent Object. Some notifications will be lost so there are no guarantees that all events generated will be processed.

Parameters:
e - The parameters associated with this event
 o keyReleased
 public void keyReleased(KeyEvent e)
Invoked when the Component the event handler is listening to has a key released. Notifies the event process that a KeyEvent has occurred by sending the KeyEvent Object. Some notifications will be lost so there are no guarantees that all events generated will be processed.

Parameters:
e - The parameters associated with this event
 o run
 public void run()
The main body of the process. Executes the functionality described above.


All Packages  Class Hierarchy  This Package  Previous  Next  Index