Hello Guest

Attaching Keylistener to GLCanvas does not work

  • 3 Replies
  • 4041 Views
Attaching Keylistener to GLCanvas does not work
« on: August 14, 2019, 20:04:56 »
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:
Code: [Select]
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:
Code: [Select]
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

}

}
« Last Edit: August 17, 2019, 08:43:12 by Orly »

*

Offline KaiHH

  • ****
  • 334
Re: Attaching Keylistener to GLCanvas does not work
« Reply #1 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?

Re: Attaching Keylistener to GLCanvas does not work
« Reply #2 on: August 15, 2019, 12:06:27 »
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
« Last Edit: August 15, 2019, 12:08:15 by Orly »

Re: Attaching Keylistener to GLCanvas does not work
« Reply #3 on: August 17, 2019, 08:42:41 »
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