3

I'm using JInput for Gamepad control on Win7 64bit. I ran into a problem: Once I get the DefaultEnvironment the controller list doesn't get updated or refreshed.

    for (Controller c : ControllerEnvironment.getDefaultEnvironment().getControllers()) {
        if (c.getType() == Controller.Type.GAMEPAD) {
            pluggedControllers.put(c);
        }
    }

So if a controller gets plugged in or out after i called ControllerEnvironment.getDefaultEnvironment() nothing changes. The list will still provide a dead controller and new controllers can't be added.

Currently I'm using this workaround which is quite ugly I think. Any ideas how I can make it work with out this hack:

if (System.getProperty("os.name").equals("Windows 7") &&
    System.getProperty("os.arch").equals("amd64"))
        try {
            Class<?> clazz = Class.forName("net.java.games.input.DefaultControllerEnvironment");
            Constructor<?> defaultConstructor = clazz.getDeclaredConstructor();
            defaultConstructor.setAccessible(true); // set visibility to public

            Field defaultEnvironementField = ControllerEnvironment.class.getDeclaredField("defaultEnvironment");
            defaultEnvironementField.setAccessible(true);
            defaultEnvironementField.set(ControllerEnvironment.getDefaultEnvironment(), defaultConstructor.newInstance());
        } catch (Exception e) {
            e.printStackTrace();
        }
atx
  • 266
  • 3
  • 15

2 Answers2

0

This has been asked for occasionally in the past. 2 ways to solve this.

1) Plug all your controllers in before you start the game. Not all existing games support this either.

2) Implement the interface interface that exists. I've asked a number of times for a volunteer to do the work, but whenever I do, this suddenly becomes less of a priority.

Endolf
  • 120
  • 9
  • **1)** is not an option **2)** didn't get it. The workaround above solves the problem, but I'm not proud of it, since it uses API which shouldn't be used... – atx Oct 30 '12 at 10:39
0

Get your controllers by calling

DirectAndRawInputEnvironmentPlugin directEnv = new DirectAndRawInputEnvironmentPlugin();
controllers = directEnv.getControllers();
Manuel Jordan
  • 97
  • 1
  • 6