AWTGLCanvas mouse and keyboard interactions

Started by rainforest, August 16, 2006, 20:02:50

Previous topic - Next topic

rainforest

With the latest version of LWJGL in release, is there any new addition for handling the lwjgl keyboard and mouse events in AWTGLCanvas?

If not then,  :) how can I add keyboard and mouse events to the AWTGLCanvas? Please give me an example.
.:: A journey of thousand miles starts with a single step ::.

Spezi

i just registered to answer :D

i made it this way:

GLInputTest.java
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.AWTGLCanvas;
import org.lwjgl.opengl.GL11;

@SuppressWarnings("serial")
public class GLInputTest extends JFrame implements MouseListener, MouseMotionListener, KeyListener {

	private GLWindow canvas;

	public GLInputTest() throws LWJGLException {
		setTitle("Keyboard & Mouse Input Example");
		setSize(640, 480);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		canvas = new GLWindow();
		add(canvas);

		setVisible(true);

		canvas.addMouseListener(this);
		canvas.addMouseMotionListener(this);

		canvas.addKeyListener(this);
		canvas.requestFocus(); // Focus for Keyboard Events

		new Thread() {
			public void run() {
				for (;;) {
					canvas.repaint();

					try {
						sleep(20);
					} catch (InterruptedException e) {
						break;
					}
				}
			}
		}.start();
	}

	class GLWindow extends AWTGLCanvas {

		public GLWindow() throws LWJGLException {
			super();
		}

		public void paintGL() {
			try {
				makeCurrent();
				GL11.glViewport(0, 0, getWidth(), getHeight());
				GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
				GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

				swapBuffers();
			} catch (LWJGLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	public static void main(String[] args) throws LWJGLException {
		new GLInputTest();
	}

	public void mouseClicked(MouseEvent e) {
		if (e.getID()==MouseEvent.MOUSE_CLICKED) {
			switch (e.getButton()) {
			case 1:
				System.out.println("Left Mouse Button");
				break;
			case 2:
				System.out.println("Middle Mouse Button");
				break;
			case 3:
				System.out.println("Right Mouse Button");
				break;
			}
		}
	}

	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
	public void mouseDragged(MouseEvent e) {}
	public void mouseMoved(MouseEvent e) {}

	public void keyPressed(KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_SPACE:
			System.out.println("SPACE pressed");
			break;
		}
	}

	public void keyReleased(KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_SPACE:
			System.out.println("SPACE released");
			break;
		}
	}

	public void keyTyped(KeyEvent e) {
		if (e.getKeyChar()=='a') {
			System.out.println("a typed");
		} else if (e.getKeyChar()=='A') {
			System.out.println("A typed");
		}
	}

}
Ich bin, ich weiß nicht wer.
Ich komme, ich weiß nicht woher.
Ich gehe, ich weiß nicht wohin.
Mich wundert, dass ich so fröhlich bin.

rainforest

Thank you very much for the post. Well Ive already implemented the keyboard and mouse interactions the same way u ve described (except that Ive extended the panel so that I can make it work in the Applet). I was wondering if there is any lwjgl kind of interaction possible!

Hey, u r a registered member now! :) Welcome!
.:: A journey of thousand miles starts with a single step ::.

rainforest

Here is the application for which I needed the mouse and keyboard integration tips. The application uses lwjgl version 0.99. U can reach the applet in: http://www.site.uottawa.ca/~arahm049/apple/applet.html.
.:: A journey of thousand miles starts with a single step ::.