Attaching Keylistener to GLCanvas does not work

Started by Orly, August 14, 2019, 20:04:56

Previous topic - Next topic

Orly

Since I got trouble using the lwjgl Keyboard class, I'm trying to implement my own Keylistener. I've been succesful at creating the opengl context inside a glcanvas/jframe, but I really can't make the keylistener work. Here's the code:
mport javax.swing.JFrame;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;

import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;

import events.Keyboard;

public static void createDisplay() {
		
		GLProfile glprofile = GLProfile.getDefault();
		GLCapabilities glcap = new GLCapabilities(glprofile);
		GLCanvas canvas = new GLCanvas (glcap);
		JFrame frame = new JFrame("best frame ever");
		canvas.addKeyListener(new Keyboard());
		canvas.setVisible(true);
		canvas.setSize(WIDTH,HEIGHT);
		frame.getContentPane().add(canvas);
		frame.pack();
		frame.setVisible(true);
		
		ContextAttribs attribs = new ContextAttribs(3,2);
		try {
			Display.setDisplayMode(new DisplayMode(WIDTH,HEIGHT));
			Display.create(new PixelFormat(), attribs);
			Display.setParent(canvas);
		} catch (LWJGLException e) {
			e.printStackTrace();
			System.err.println("something went wrong creating the display");
		}
		GL11.glViewport(0, 0, WIDTH, HEIGHT);
		GL11.glEnable(GL11.GL_TEXTURE_2D);
		
	}


Keyboard class:
package events;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Keyboard implements KeyListener{
	
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void keyPressed(KeyEvent e) {
		System.out.println("pressed something");
	}

	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

}

KaiHH

Now you seem to be mixing JOGL and LWJGL together. Can you please show the import statements of this code snippet?

Orly

Quote from: KaiHH on August 14, 2019, 20:34:31
Now you seem to be mixing JOGL and LWJGL together. Can you please show the import statements of this code snippet?
Done, I just edited the post

Meanwhile, I also tryed switching from GLCanvas to a normal java Canvas, but it's still not working

Orly

Found out the problem. When I call Display.update(), I must add the false argument -> Display.update(false), so that it doesn't trigger the lwjgl's Keyboard and Mouse classes (or something like that). basically, if you don't use false, lwjgl steals the input from the keylistener to give it to the Keyboard and Mouse class