Hello. I am wondering what is the best way to have Java call some function when a button is pressed on a game controller. Is there some ControllerEventListener that can receive an event when this happens?
Thank you.
Hello. I am wondering what is the best way to have Java call some function when a button is pressed on a game controller. Is there some ControllerEventListener that can receive an event when this happens?
Thank you.
I’ve looked through the API and it seems that the component’s getPollData() is what I’m looking for. But I have tested this, and I do not receive changes in poll data when I press buttons or move the axes. I have tried with my mouse and my game controller using the following code.
import java.util.*;
import net.java.games.input.*;
public class TestJInput {
static Timer t;
static Component cp[];
static Controller co;
public static void instantiateController(Controller c) {
co = c;
cp = co.getComponents();
t = new Timer();
t.schedule(new TimerTask() {
public void run() {
for (Component i : cp)
System.out.println(i.getName() + " polls data: " + i.getPollData());
}}, 0, 1000);
}
public static void main(String[] args) {
Controller[] ca = ControllerEnvironment.getDefaultEnvironment().getControllers();
for(int i =0;i<ca.length;i++){
if(ca[i].getName().startsWith("Controller")) {
instantiateController(ca[i]);
break;
}
}
}
}
It finds the components but the poll data always returns 0.0 no matter what I do with the controller.
Any help would be greatly appreciated. Thanks.
I haven’t used jinput directly (only through engines), but it usually works. Have you read the getting started thread?