简体   繁体   中英

How do I get gamepad input in Java? (Windows 10)

I'm creating a pretty basic game in my free time, and before I really get into it I want to learn how to include controller support. I've tried plenty of tutorials and different libraries, but they all in some way seem to require JInput (and LWJGL, they seem to related in some way?). This wouldn't be an issue, and generally speaking I've figured out how to work with JInput. However, I can't find a single version of JInput that is compatible with windows 10. Even if I were to find one, I have no way of knowing that the test code I wrote would even work with the newer versions of the library, so I have no way of testing or even knowing if they work with windows 10.

I would prefer to use JInput as it seems standard, but if you know of any way to add controller support in Java, then I will jump at the chance to try it out.

Here's my code and error - maybe I'm just doing something wrong.

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Controller;
import org.lwjgl.input.Controllers;

    public class GamePadTut {

        static Controller cont;

        public static void main(String[] args) {
            try {
                Controllers.create();
            } catch (LWJGLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Controllers.poll();

            for(int i =0; i < Controllers.getControllerCount(); i++) {
                cont = Controllers.getController(i);
                System.out.println(cont.getName());
            }
        }
    }

and the errors

WARNING: Found unknown Windows version: Windows 10
Attempting to use default windows plug-in.
Loading: net.java.games.input.DirectAndRawInputEnvironmentPlugin
java.lang.UnsatisfiedLinkError: no jinput-dx8_64 in java.library.path
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.loadLibrary0(Unknown Source)
    at java.lang.System.loadLibrary(Unknown Source)
    at net.java.games.input.DirectInputEnvironmentPlugin$1.run(DirectInputEnvironmentPlugin.java:75)
    at java.security.AccessController.doPrivileged(Native Method)
    at net.java.games.input.DirectInputEnvironmentPlugin.loadLibrary(DirectInputEnvironmentPlugin.java:67)
    at net.java.games.input.DirectInputEnvironmentPlugin.<clinit>(DirectInputEnvironmentPlugin.java:109)
    at net.java.games.input.DirectAndRawInputEnvironmentPlugin.<init>(DirectAndRawInputEnvironmentPlugin.java:45)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at net.java.games.input.DefaultControllerEnvironment.getControllers(DefaultControllerEnvironment.java:157)
    at org.lwjgl.input.Controllers.create(Controllers.java:71)
    at GamePadTut.main(GamePadTut.java:11)
java.lang.UnsatisfiedLinkError: no jinput-raw_64 in java.library.path
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.loadLibrary0(Unknown Source)
    at java.lang.System.loadLibrary(Unknown Source)
    at net.java.games.input.RawInputEnvironmentPlugin$1.run(RawInputEnvironmentPlugin.java:75)
    at java.security.AccessController.doPrivileged(Native Method)
    at net.java.games.input.RawInputEnvironmentPlugin.loadLibrary(RawInputEnvironmentPlugin.java:67)
    at net.java.games.input.RawInputEnvironmentPlugin.<clinit>(RawInputEnvironmentPlugin.java:109)
    at net.java.games.input.DirectAndRawInputEnvironmentPlugin.<init>(DirectAndRawInputEnvironmentPlugin.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at net.java.games.input.DefaultControllerEnvironment.getControllers(DefaultControllerEnvironment.java:157)
    at org.lwjgl.input.Controllers.create(Controllers.java:71)
    at GamePadTut.main(GamePadTut.java:11)

I believe you are missing the .dll in your native library path. the jinput-dx8_64 is a .dll file if I'm not mistaken.

http://wiki.lwjgl.org/wiki/Setting_Up_LWJGL_with_Eclipse.html

^ that should help you get that set up, if you're using eclipse. If you're using another IDE I don't really know haha.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM