Skip to main content

Custom Keyboard Navigation

No replies
axiumcog
Offline
Joined: 2011-03-09
Points: 0

This is supposed to update the TG location on arrow-key events. For whatever reason it doesnt work. Help please!

package uu_Main;

import java.awt.AWTEvent;
import java.awt.event.KeyEvent;
import java.util.Enumeration;

import javax.media.j3d.Behavior;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.WakeupCondition;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupOnAWTEvent;
import javax.media.j3d.WakeupOr;
import javax.vecmath.Vector3f;

public class KeyNavBehavior extends Behavior {

    private Vector3f translate = new Vector3f();
    private Transform3D T3D=new Transform3D();
    private TransformGroup targetTG;
    private WakeupCriterion[] criterion={new WakeupOnAWTEvent(KeyEvent.VK_UP),new WakeupOnAWTEvent(KeyEvent.VK_DOWN),new WakeupOnAWTEvent(KeyEvent.VK_LEFT),new WakeupOnAWTEvent(KeyEvent.VK_RIGHT)};
    private WakeupCondition trigger=new WakeupOr(criterion);
  
    KeyNavBehavior(TransformGroup tg){
        targetTG=tg;
    }
  
    @Override
    public void initialize() {
        this.wakeupOn(trigger);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void processStimulus(Enumeration criteria) {
        translate.set(0.0f, 0.0f, 0.0f);
        WakeupCriterion ster;
        while (criteria.hasMoreElements())
        {
            ster=(WakeupCriterion) criteria.nextElement();
            if (ster instanceof WakeupOnAWTEvent)
            {
                processAWTEvent(((WakeupOnAWTEvent)ster).getAWTEvent());
            }
        }  
        this.wakeupOn(trigger);
    }
  
    private void processAWTEvent(AWTEvent[] events)
    {
        for (int n=0;n<events.length;n++)//for each event
        {
            if (events[n] instanceof KeyEvent)//if its a key event
            {
                KeyEvent ek = (KeyEvent) events[n];//
                if (ek.getID() == KeyEvent.KEY_PRESSED)
                {
                    switch(ek.getKeyCode())
                    {
                    case KeyEvent.VK_UP: translate.set(new Vector3f(0.0f, 0.0f, -1.0f)); break;
                    case KeyEvent.VK_DOWN: translate.set(new Vector3f(0.0f, 0.0f, 1.0f)); break;
                    case KeyEvent.VK_LEFT: translate.set(new Vector3f(-1.0f, 0.0f, 0.0f)); break;
                    case KeyEvent.VK_RIGHT: translate.set(new Vector3f(1.0f, 0.0f, 0.0f)); break;
                    }
                    T3D.setTranslation(translate);
                    targetTG.setTransform(T3D);
                }
                /*
                else if (ek.getID() == KeyEvent.KEY_RELEASED)
                {
                    switch(ek.getKeyCode()){
                      
                    }
                }*/
            }
        }
    }  
}