keyboard control

Started by urd, June 11, 2003, 05:30:45 AM

Previous topic - Next topic

urd

hi dear everyone,i have a trouble situation controling my keyboard/_\..
i am adding some new ability:"camera control by keyboard" to JPCT example TerrainGl as a practice.i have some trouble getting keyboard work:
1.i import org.lwjgl.input.Keyboard ,and use
try()
{
 Keyboard.create();
}
catch(Exception e)
{
  System.out.println("keyboard creat fail");
}
in private TerrainGL()

but when run it,it prints "keyboard creat fail"and my switch(Keyboard.isKeyDown(key) )
{}didnt work.>_<
2.i use KeyListener,TerrainGl extends Component implments KeyListener
,and i am sure i did "this.addKeyListener(this)"in private TerrainGL()
in public void Keypressed(Keyevent e)
{
 switch(e)
{
.......
}
}
but the same didnt work/_\..

can someone help me,did i do what wrong ,or i should use other way to write?

EgonOlsen

First, i suggest to add an "e.printStackTrace()" in the catch-block to see what kind of exception actually occurs.
IIRC, LWJGL requires that the GLWindow has been constructed before creating the keyboard, because the keyboard needs a "component" to focus to. Try creating the keyboard after enabling the OpenGL-renderer in jPCT. Otherwise, it won't work.
Your second approach should work when using the software renderer, but not when using the OpenGL renderer, because the LWJGL window isn't a component in the thinking of Java. Therefor, it's not possible to bind a keylistener (or mouselistener or whatever) to it.
This is a kind of dilemma: When rendering into a standard Java component (using the software renderer), you may use the keylistener and you can't use the LWJGL keyboard polling, while you have to use it when using the OpenGL renderer (and can't use the listener in this case). The best way IMHO would be to encapsulate the keyboard related stuff in a class that supports both ways somehow and uses what's appropriate in the given situation. While i wouldn't include this in jPCT directly, it would be a nice addon for it...so if you decide to write such thing, i would be greatly interested in your solution.

Anonymous

Quote from: "EgonOlsen"First, i suggest to add an "e.printStackTrace()" in the catch-block to see what kind of exception actually occurs.
IIRC, LWJGL requires that the GLWindow has been constructed before creating the keyboard, because the keyboard needs a "component" to focus to.
oh..i got it! i think its just the situation so i didnt get my Keyboard correctly functioned(i put Keyboard.creat() in the most front in  public TerrainGl())
:oops:
Quote from: "EgonOlsen"
The best way IMHO would be to encapsulate the keyboard related stuff in a class that supports both ways somehow and uses what's appropiate in the given situation. While i wouldn't include this in jPCT directly, it would be a nice addon for it...so if you decide to write such thing, i would be greatly interested in your solution.
m..i got it.. i think this may be some work on two kinds keycode mapping,and one have no buffer(J2SE Keylistener) implements ,another have(Keyboard in lwjgl)etc.if i have time in next several weeks,i'll try to solute it  :P

urd

sorry last guest is me,automatically logging  didnt work :oops: the same to the guest in next thread^^||

urd

lucky!i have Keyboard.create successfully.it works!
EgonOlsen thank u a lot~