I would like to write my own KeyBehavior, but I have problems to identify the pressed key. Can someone help me? E.g. when I press the key "a", I would like to do something.
Here's my code:
[snip]
public void processStimulus(Enumeration criteria) {
WakeupCriterion wakeup; AWTEvent[] event; int eventId;
while (criteria.hasMoreElements()){ wakeup = (WakeupCriterion) criteria.nextElement(); if (wakeup instanceof WakeupOnAWTEvent){ event = ((WakeupOnAWTEvent) wakeup).getAWTEvent(); for (int i=0; i<event.length; i++){ eventId = event[i].getID(); if(eventId == KeyEvent.KEY_PRESSED){ String test = event[i].toString(); Debug.out(deb, "id: "+test); Transform3D tempT3D = navCalculator.makeEyePosition(0.0d, 0.0d); viewTG.setTransform(tempT3D); } } } } this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
}
This is what I had done initially in my project too, but we were getting sluggish response and many keyboard and mouse events were getting lost. In addition, I've decided to go to a single behavior model, where there is only one behavior in the entire system, which wakes up each frame. This way, you are guaranteed that everything you do in your behaviors will be synchronized (show up in the same frame of animation).
So, using the Java 3D utilities KeyNavigatorBehavior and MouseBehavior as a model, I wrote the below class. It queues AWT events. Later, in the processStimulus for your every-frame behavior, read out the events and handle them there. Create a single instance of this object with your Canvas3D and make sure your processStimulus method has access to it, and you will get much better keyboard and mouse response.
-Paul
PS The response from Mike Pilone that you need to use KeyEvent.getKeyChar() to get the character pressed is correct.
package com.YourCompany.YourPackage;
import java.awt.event.*; import java.awt.*; import java.util.*;
import javax.swing.event.MouseInputListener;
/** * AWT event callbacks such as these are called from the AWT thread. * If you take too long handling an event, subsequent events will be * lost. Therefore, the best way to handle AWT events is to queue * them to handle later.<p> * * The other advantage of doing it this way is that we can * synchronize handling keyboard and mouse events with other * updates to the system. We don't need to handle them when * AWT gives them to us, we can handle them whenever we want.<p> * * This code is based on code in the Java 3D utilities source, * KeyNavigatorBehavior.java and MouseBehavior.java. Those * use behavior post to inform other behaviors of the existence * of events. We are just going to check each frame, so we * don't need that additional overhead.<p> * * The code has been optimized so that only the last drag or move * event is in the queue. */ public class AwtEventQueuer implements KeyListener, MouseInputListener {
private LinkedList eventq = new LinkedList();
private boolean enabled = true;
private MouseEvent moveEvent = null; private MouseEvent dragEvent = null;
/** * Enables capturing of AWT events. The default. */ public void enable() { enabled = true; } // End of enable
/** * Disables capturing of AWT events. AWT events are ignored until enable() is * called. Events already in the queue may still be fetched. */ public void disable() { enabled = false; } // End of disable
/** * Add an event to the Event Queue. * @param e The event. */ public void addEvent(InputEvent e) { if (enabled) { synchronized (eventq) {
// Don't want multiple drag or move events in queue if (e instanceof MouseEvent) { int id = ((MouseEvent)e).getID(); if (id == MouseEvent.MOUSE_DRAGGED) { if (dragEvent != null) eventq.remove(dragEvent); dragEvent = (MouseEvent)e; } else if (id == MouseEvent.MOUSE_MOVED) { if (moveEvent != null) eventq.remove(moveEvent); moveEvent = (MouseEvent)e; } } eventq.add(e); } } } // End of addEvent
/** * Get the next InputEvent from the event queue. * @return The event. Returns null if the queue is empty. */ public InputEvent getNextEvent() { InputEvent e; synchronized (eventq) { if (eventq.size() == 0) e = null; else { e = (InputEvent)(eventq.removeFirst()); if (e instanceof MouseEvent) { int id = ((MouseEvent)e).getID(); if (id == MouseEvent.MOUSE_DRAGGED) dragEvent = null; else if (id == MouseEvent.MOUSE_MOVED) moveEvent = null; } } } return e; } // End of getNextEvent
public void keyPressed(KeyEvent e) { addEvent(e); } // End of keyPressed
public void keyReleased(KeyEvent e) { addEvent(e); } // End of keyReleased
public void keyTyped(KeyEvent e) { addEvent(e); } // End of keyTyped
public void mouseClicked(MouseEvent e) { addEvent(e); } // End of mouseClicked
public void mouseEntered(MouseEvent e) { addEvent(e); } // End of mouseEntered
public void mouseExited(MouseEvent e) { addEvent(e); } // End of mouseExited
public void mousePressed(MouseEvent e) { addEvent(e); } // End of mousePressed
public void mouseReleased(MouseEvent e) { addEvent(e); } // End of mouseReleased
public void mouseDragged(MouseEvent e) { addEvent(e); } // End of mouseDragged
public void mouseMoved(MouseEvent e) { addEvent(e); } // End of mouseMoved
public AwtEventQueuer(Component c) { c.addMouseListener(this); c.addMouseMotionListener(this); c.addKeyListener(this); } // End of constructor
} // End of class AwtEventQueuer
=========================================================================== To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff JAVA3D-INTEREST". For general help, send email to [EMAIL PROTECTED] and include in the body of the message "help".