key input Keylistener doesn't work for JOGL

Started by tragic_xxx, November 06, 2017, 17:31:45

Previous topic - Next topic

tragic_xxx

For some good reason i am using JOGL ....  Every other thing works well, including the static renderings, but the Input key binding doesn't work, I have got no clues why. I am hoping expert eyes inspection would be able to see whats wrong.  I am hoping this code is small enough and clear enough for someone to be able to inspect with ease and no stress
Many thanks


public class Game extends JFrame implements GLEventListener, KeyListener  {
	private static final long serialVersionUID = 1L;
	final private int width = 800;
	final private int height = 600;
	int  right=-100, bottom=-100, top=100, left=100,       numOfUnits;
	 GLU glu= new GLU();
     List<CreateObjVertices> dataArray;
     static Frame frame = new Frame();

	public Game(  int units,  List<CreateObjVertices> vertXYZ  ) {
		super("Minimal OpenGL");
		Globals.camera = new Point3D(0.0f, 1.4f, 0.0f); 
		Globals.view = new Point3D(0.0f,  -1.0f, -3.0f);
		GLProfile profile = GLProfile.get(GLProfile.GL2);
		GLCapabilities capabilities = new GLCapabilities(profile);
		GLCanvas canvas = new GLCanvas(capabilities);
		//-------------------------------  
		//-------------------------------
		canvas.addGLEventListener(this);
		this.addKeyListener(this);    
	    this.setFocusable(true);  // To receive key event
	    this.requestFocus();

		this.setName("Minimal OpenGL");
		this.getContentPane().add(canvas);		
		this.setSize(width, height);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setResizable(false);
		dataArray = vertXYZ;
		numOfUnits  =  units;
		canvas.requestFocusInWindow();
	}
	
	public void play() {
	}
	
    @Override
    public void display(GLAutoDrawable drawable) {
   	 	 GL2 gl = drawable.getGL().getGL2();
		 gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
	
		 gl.glMatrixMode(GL2.GL_MODELVIEW);             
		 gl.glLoadIdentity();      
		 glu.gluLookAt( Globals.camera.x, Globals.camera.y, Globals.camera.z,     Globals.view.x, Globals.view.y, Globals.view.z,    0.0f, 1.0f, 0.0f);
		 gl.glTranslatef(0.0f, 0.0f, -3.0f);
		 
         gl.glBegin(GL.GL_LINE_LOOP);
//==============        Note: I have edited this draw for clarity and it draws to my satisfaction, the key binding is the only problem in the code
         CreateObjVertices vertices;
         
	     for( int t=0; t<numOfUnits; t++ ){

		   if( (t >= 0) ){
	        	vertices = dataArray.get( t );
		    	gl.glColor3f(1, 0, 1);   

				if( vertices.objectArrayLength > 0 ){     
					for(int k=0, s=0; k<vertices.objectArrayLength; k++, s=s+3){		   
					   
					   gl.glVertex3f( vertices.objectVertexArray[k].x*1,   vertices.objectVertexArray[k].y*1,  vertices.objectVertexArray[k].z*1 );
		
				   }
				}
			}   		
	     }
//=====
         gl.glEnd();
		 gl.glFlush();
    }

    @Override
    public void dispose(GLAutoDrawable drawable) {
   
    }

    @Override
    public void init(GLAutoDrawable drawable) {
    	GL2 gl = drawable.getGL().getGL2();
    	gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        gl.glClearDepthf(1.0f);            
        gl.glEnable(GL2.GL_DEPTH_TEST);   
        gl.glDepthFunc(GL2.GL_LEQUAL);   
        gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);  
        gl.glShadeModel(GL2.GL_SMOOTH);        
	    gl.glEnableClientState(GL2.GL_VERTEX_ARRAY); 
	}

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width,  int height) {
    	GL2 gl = drawable.getGL().getGL2();
		if (height == 0) height = 1;  
		float aspect = (float)width / height;
		gl.glViewport(0, 0, width, height);
		   
		gl.glMatrixMode(GL2.GL_PROJECTION); 
		gl.glLoadIdentity();                 
		glu.gluPerspective( 45, aspect, 0.1f, 100.0f);
    }

  
	@Override
	public void keyPressed(KeyEvent e) {
		
		if( e.getKeyChar() == 'x'){
			System.out.println(" xxxx ");
		}
		if (e.getKeyChar() == KeyEvent.VK_LEFT) {
			System.out.println(" LEFT ");			
		}
		if (e.getKeyChar() == KeyEvent.VK_D) {
			System.out.println(" DDDD ");			
		}
		System.out.println(" LEFT ");
	}

	@Override
	public void keyReleased(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			System.out.println(" LEFT ");			
		}
		System.out.println(" LEFT ");
	}

	@Override
	public void keyTyped(KeyEvent e) {		
		if( e.getKeyCode() == 'x'){
			System.out.println(" xxxx ");
		}
		if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			System.out.println(" LEFT ");			
		}
		if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			System.out.println(" RIGHT ");			
		}
		if (e.getKeyCode() == KeyEvent.VK_UP) {
			System.out.println(" UP ");			
		}
		if (e.getKeyCode() == KeyEvent.VK_DOWN) {
			System.out.println(" DOWN ");			
		}
		System.out.println(" LEFT ");
	}    
}

KaiHH

You probably did not notice, but this is the LWJGL forum. It has nothing to do with JOGL. You should probably ask those kinds of questions on the JOGL forums?