How to use keyboard + JInput ?
i did’nt found it on google =9
Examples are welcome. =p
How to use keyboard + JInput ?
i did’nt found it on google =9
Examples are welcome. =p
Here’s random parts of an InputManager class I made.
First you need to find the keyboard and store it for future use.
private static Keyboard findKeyboard()
{
for (int i = 0; i < controllers.length; i++)
if (controllers[i].getType() == Controller.Type.KEYBOARD)
return (Keyboard)controllers[i];
return null;
}
In an initializer somewhere…
keyboard = findKeyboard();
Then every timestep you want to poll the keyboard. This simply goes through all the buttons to find what has been pressed, putting it all in a giant boolean list.
public static boolean[] pollKeyboard()
{
keyboard.poll();
Component[] c = keyboard.getComponents();
boolean[] b = new boolean[c.length];
for (int i = 0; i < c.length; i++)
{
if (c[i].getPollData() != 0)
b[i] = true;
else
b[i] = false;
}
return b;
}
To use the above, you’d do this:
if (pollKeyboard()[net.java.games.input.Component.Identifier.Key.A])
{
doA();
}
Or if you just want to avoid a boolean list (better depending on what you want to do) you can do this:
if ((keyboard.isKeyDown(net.java.games.input.Component.Identifier.Key.A))
{
doA();
}
Hi
Checking every key is a bit over kill, for simple WASD type keyboard control it’s fine, but if you want to capture typing in a dialog box for example, it’s alot more effort than would be nice. There is the event model also. Instead of asking 120 times if a particular key has been pressed, you just ask the device for the events since the last poll.
Check the getting started thread, and for events see this post
HTH
Endolf
I tryed do it. But isn’t work =(
package net.java.games.input.test;
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;
import net.java.games.input.Keyboard;
public class ControllerManager {
private static Keyboard keyboard = findKeyboard();
private static Keyboard findKeyboard()
{
ControllerEnvironment controllerEnvironment =ControllerEnvironment.getDefaultEnvironment();
Controller[] aController = controllerEnvironment.getControllers();
for (int i = 0; i < aController.length; i++)
if (aController[i].getType() == Controller.Type.KEYBOARD)
return (Keyboard)aController[i];
return null;
}
public static void main(String[] args) {
while(true){
if (keyboard.isKeyDown(net.java.games.input.Component.Identifier.Key.A)){
System.out.println("I'm pressed");
}
}
}
}
Hi
On a linux box here when I run that code I get a null pointer because it found no keyboard. What os are you using?, does the applet demo from here work?
Cheers
Endolf
I using Linux too, but i set this “-Djinput.plugins=net.java.games.input.AWTEnvironmentPlugin” in my VM.
How are you launching this?, from an IDE?, or from the command line?
Endolf
From eclipse IDE. “VM arguments”
This post suggests you got something working correctly. It’s not finding anything that admits to being a keyboard. do the controller text/read tests work?
Endolf
no, i only detected the keyboard. And I tryed do this
but did’nt work.
hi zeni,
first of all you forgot to poll the keyboard with keyboard.poll() at the beginning of the while loop. in addition this wont work on my windows vista as well. i tryed to start it from inside eclipse as well as from the command line but it didnt work. as soon as i use the same code in an application that uses a JFrame it starts to work tho. any ideas why it acts like that? (just a wild guess: probably DInput needs a window that implements a callback for the keyboard handling?)
best regards
johannes diemke
Hi
Instead of calling findKeyboard in a static initialiser, try calling it from the constructor of ControllerManager.
JInput needs to do some static initialisation of it’s own, my guess is that by calling yours early on, it’s getting confused.
HTH
Endolf