Jump to content
  • Advertisement
Sign in to follow this  
grumpyOldDude

keyPressed(KeyEvent e) { Globals.c = e.getKeyChar(); } doesn't work

Recommended Posts

I have strip the code down to barest minimum for clarity

With the window up and draw rendering correctly, I set the key press to take the 'x' key, but when I press 'x' and print anywhere (and everywhere) to test, nothing prints to console. 

I scrutinised to see why, but I can't find what is missing.  Can anyone see why its not working? Thanks

public class PuzzleG extends JFrame implements GLEventListener, KeyListener  {
	private static final long serialVersionUID = 1L;
	final private int width = 800;
	final private int height = 600;
	int  numOfUnits;
	 GLU glu= new GLU();

     List<ObjVertices> dataArray;

	public PuzzleG(  int units,  List<ObjVertices> vertXYZ  ) {
		super(" Puzzle game ");
		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.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);
/*=========================== START ===========================================================         
         ... draw object vertices ...
============================= end ============================================================	*/     
         gl.glEnd();
		 gl.glFlush();
    }

    @Override
    public void dispose(GLAutoDrawable drawable) {
    	if( Globals.c == 'x')System.out.println(" 3333 ");
    }

    @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); 
	    if( Globals.c == 'x')System.out.println(" 1111 ");
	}

    @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);
		if( Globals.c == 'x')System.out.println(" 2222 ");
    }

  
	@Override
	public void keyPressed(KeyEvent e) {
		Globals.c = e.getKeyChar();
		if( Globals.c == 'x'){
			System.out.println(" 5555 ");
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}    
}

Just for the sake of checking

I also tried the other key  options as in the code below, but still didn't see any prints to console

	@Override
	public void keyReleased(KeyEvent e) {
		Globals.c = e.getKeyChar();
		if( Globals.c == 'x'){
			System.out.println(" 8888 ");
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {
		Globals.c = e.getKeyChar();
		if( Globals.c == 'x'){
			System.out.println(" 7777 ");
		}
	}    

 

Edited by grumpyOldDude

Share this post


Link to post
Share on other sites
Advertisement

From the API (getKeyChar): 

KEY_PRESSED and KEY_RELEASED events are not intended for reporting of character input. Therefore, the values returned by this method are guaranteed to be meaningful only for KEY_TYPED events.

 

Your KeyTyped method looks like it should work so I'm not sure what's up with that (unless you have caps lock on or something?), but otherwise if you're not printing/logging the actual characters that are inputted and are instead mapping keys to actions, you should use getKeyCode() + keyPressed/keyReleased instead anyway.

Share this post


Link to post
Share on other sites

I tried alternative suggested but no success.

I soon got to learn from this Stack Overflow thread that there is no direct key binding for GLCanvas which is the canvas JOGL uses. This suggests why the key binding for my code in the original post failed. In the same thread there was a hint (link below) that there could be indirect key bindings GLCanvas, though no meaningful clues or code on how to do this was giving

https://stackoverflow.com/a/18860666/1489197

I would appreciate any help and code on how to do this. Thanks for any help

Edited by grumpyOldDude

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!