如何向LWJGL显示添加键监听器?

时间:2014-12-06 02:18:56

标签: java lwjgl keylistener

我正在尝试从LWJGL向我的显示器添加一个关键监听器。我可以看到我的代码中出现错误的地方,我认为问题是因为我正在尝试将我的密钥监听器“input”添加到类中。在我看到的所有示例中,关键侦听器都被添加到JFrame中,但我没有使用它,所以我不知道要将它添加到什么内容。我正在提供我遇到错误的类和我创建的关键监听器。请忽略一些评论,因为大多数评论只是失败的代码片段。我得到的错误是在Initializer的第39行。该行是“this.addKeyListener(input);”。我想添加的另一件事是,目前“/”的长行之间的代码是无用的,所以忽略它们会更好。谢谢你的帮助。

import java.awt.Cursor;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.Random;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;

import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import static org.lwjgl.opengl.GL11.*;

public class Initializer {
    static int height = 800;
    static int width = 600;
    public static Camera cam;
    private InputHandler input;

public void start() {
    try {
        Display.setDisplayMode(new DisplayMode(height,width));
        Display.setTitle("3D OpenGL");
        Display.create();
    } catch (LWJGLException e) {
        e.printStackTrace();
        System.exit(0);
    }

    // init OpenGL here
    cam = new Camera(70, (float)Display.getWidth()/(float)Display.getHeight(), 0.3f, 1000);
    float x = 0;

    //Makes cursor disappear
    Mouse.setGrabbed(true);
    //displayExample.getContentPane().setCursor(c);

    input = new InputHandler();
    this.addKeyListener(input);

    Keyboard.enableRepeatEvents(false);

    while (!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {

        ///////////////////////////////////////////////
        Movement2Vector movement = new Movement2Vector(0, 0);



        //Current issue is that the 2 statements below are not working to check for which key is pressed and if a key is pressed



        int key = Keyboard.getEventKey();
        boolean pressed = Keyboard.getEventKeyState();
        System.out.println(pressed);
        Movement2Vector vec = Direction.of(key);
        //System.out.println(vec.getX() + ", " + vec.getY());
        if (pressed) {
          movement = movement.add2Vector(vec);
          cam.moveCam(movement);
        } else if (movement != new Movement2Vector(0, 0) && movement == vec) {
          movement = new Movement2Vector(0, 0);
        }

        ///////////////////////////////////////////////
        //Checks for movement
        //Movement mov = new Movement();
        //Initialized Delta
        Delta.lastFrame = Delta.getTime();

        // render OpenGL here
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
        cam.useView();

        glPushMatrix();
        glColor3f(1.0f, 0.5f, 0f);
        glTranslatef(0,0,-10);
        glRotatef(x, 2.5f, 1.5f , 0);
        glColor3f(0.0f, 1.0f, 1.0f);
        CreateCube cube = new CreateCube(-1, 1, 1, 1, -1, -1);
        /*glBegin(GL_QUADS);
            //FrontFace
            glVertex3f(-1,-1,1);
            glVertex3f(-1,1,1);
            glVertex3f(1,1,1);
            glVertex3f(1,-1,1);

            //BackFace
            glVertex3f(-1,-1,-1);
            glVertex3f(-1,1,-1);
            glVertex3f(1,1,-1);
            glVertex3f(1,-1,-1);

            //BottomFace
            glVertex3f(-1,-1,-1);
            glVertex3f(-1,-1,1);
            glVertex3f(-1,1,1);
            glVertex3f(-1,1,-1);

            //TopFace
            glVertex3f(1,-1,-1);
            glVertex3f(1,-1,1);
            glVertex3f(1,1,1);
            glVertex3f(1,1,-1);

            //LeftFace
            glVertex3f(-1,-1,-1);
            glVertex3f(1,-1,-1);
            glVertex3f(1,-1,1);
            glVertex3f(-1,-1,1);

            //Right Face
            glVertex3f(-1,1,-1);
            glVertex3f(1,1,-1);
            glVertex3f(1,1,1);
            glVertex3f(-1,1,1);
        glEnd();*/
        glPopMatrix();
        for(int i=-15; i<4; i++){
            for(int y=-1; y<2; y++){
                Random r = new Random();
                glColor3f(0.3f, 1.0f, 0.5f);
                CreateCube cubeTest = new CreateCube(y-1, -4, i-1, y+1, -2, i+1);
            }
        }


        x += 1f;
        Display.update();
        Display.sync(60);
    }

    Display.destroy();
    System.exit(1);
}

public static void main(String[] args) {
    Initializer displayExample = new Initializer();
    displayExample.start();
}
}

Key Listener类

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

import org.lwjgl.input.Keyboard;


public class InputHandler implements KeyListener, FocusListener{

    public boolean[] key = new boolean[68836];

    @Override
    public void focusGained(FocusEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void focusLost(FocusEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(Keyboard.isKeyDown(Keyboard.KEY_W)){
            Initializer.cam.moveForward(0.005f*Delta.getDelta());
        }
        else if(Keyboard.isKeyDown(Keyboard.KEY_S)){
            Initializer.cam.moveForward(-0.005f*Delta.getDelta());
        }
        else if(Keyboard.isKeyDown(Keyboard.KEY_A)){
            Initializer.cam.moveSideways(0.005f*Delta.getDelta());
        }
        else if(Keyboard.isKeyDown(Keyboard.KEY_D)){
            Initializer.cam.moveSideways(-0.005f*Delta.getDelta());
        }
        else if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
            Initializer.cam.moveUp(0.005f*Delta.getDelta());
        }
        else if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)){
            Initializer.cam.moveUp(-0.005f*Delta.getDelta());
        }

        /*int keyCode = e.getKeyCode();
        if(keyCode > 0 && keyCode < key.length){
            key[keyCode] = true;
        }*/

    }

    @Override
    public void keyReleased(KeyEvent e) {       
        /*for(int i = 0; i < key.length; i++){
            key[i] = false;
        }*/

    }

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

    }

}

1 个答案:

答案 0 :(得分:0)

如果您使用的是lwjgl,则可以导入import org.lwjgl.input.Keyboard; 而你可以使用它,而我发现效率更高

if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)){