
Mirror an Application #97
Chapter 12, Miscellany
|
489
HACK
AWTEvent.ACTION_EVENT_MASK |
AWTEvent.MOUSE_EVENT_MASK
);
}
First,
openSender( )
creates a new
ObjectOutputStream
around the socket’s
output stream. Next, it creates a new
AWTEventListener
that takes each event
and tests if it is a mouse event; if so, this method writes it to the output
stream. Notice that the second argument of
addAWTEventListener( ) is two
event masks ORed together (using the
| operator).
Receive Mouse Events
Receiving events is the reverse of sending them. You must open a server
socket for the (sending) instance to connect to, and then pull the events off
of the network one by one and repost them to the system event queue:
public void openReceiver( ) throws Exception {
// receive events
ServerSocket server = new ServerSocket(6754);
Socket sock = server.accept( );
EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue( );
ObjectInputStream in = new ObjectInputStream(sock.getInputStream( ));
while(true ) {
AWTEvent evt = (AWTEvent) in.readObject( );
if(evt instanceof MouseEvent) {
MouseEvent me = (MouseEvent)evt;
MouseEvent me2 = new MouseEvent(
me.getComponent( ),
me.getID( ),
me.getWhen( ),
me.getModifiers( ),
me.getX( ),
me.getY( ),
me.getClickCount( ),
me.isPopupTrigger( ),
me.getButton( )
);
eq.postEvent(me2);
}
}
}
Notice that the events are not posted directly to the event queue. Since the
objects really belong ...