java - How do I add a key listener to a LWJGL display? -
i trying add key listener display lwjgl. can see getting error in code, , think problem caused because trying trying add key listener, "input", class. in examples have seen key listener added jframe, not using 1 don't know add to. i'm providing class having error in , key listener created. please ignore of comments because majority of them failed pieces of code. error i'm getting on line 39 of initializer. line "this.addkeylistener(input);". thing wanted add, code between long lines of "/"s useless better ignore them. thank help.
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 2 statements below not working check key pressed , if key 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 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 class
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 = 0; < key.length; i++){ key[i] = false; }*/ } @override public void keytyped(keyevent e) { // todo auto-generated method stub } }
if using lwjgl can importimport org.lwjgl.input.keyboard;
, can use instead find more efficient
if(keyboard.iskeydown(keyboard.key_left)){
Comments
Post a Comment