Receive joystick input in Java LW JFL

The method of receiving joystick input in Java can also be done with a library called JInput, You have to put the native library (DLL) somewhere in your path, I didn't like Mac / Linux, so I didn't like it, so I tried using a library called LWJGL.

First, download LWJGL3 from https://www.lwjgl.org/. The following 4 jars are required for windows.

I think Mac / Linux should use natives-mac or natives-linux I experimented with this code.

JoyStick.java


import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import org.lwjgl.glfw.GLFW;

public class JoyStick {

	public static void main(String[] args) {
		//Initialize the GLFW. If you don't do this, most GLFW functions won't work.
		if(!GLFW.glfwInit())
			throw new IllegalStateException("Unable to initialize GLFW");

		GLFW.glfwPollEvents();
		int stick;
		for(stick = 0; stick <= GLFW.GLFW_JOYSTICK_LAST; stick++) {
			if(!GLFW.glfwJoystickPresent(stick)) continue;
			System.out.println("JoyStick(" + stick + ")Name:" +
					GLFW.glfwGetJoystickName(stick) + " " +
					GLFW.glfwGetGamepadName(stick));
			break;
		}
		if(stick > GLFW.GLFW_JOYSTICK_LAST) return;

		for(int i = 0; i < 1000; i++) {
			int count1 = 0;
			FloatBuffer floatBuffer = GLFW.glfwGetJoystickAxes(stick);
			System.out.print("Axes:");
			while (floatBuffer.hasRemaining()) {
				float axes = floatBuffer.get();
				System.out.print(count1 + "," + axes + " ");
				count1++;
			}

			int count2 = 0;
			System.out.print("Button:");
			ByteBuffer byteBuffer = GLFW.glfwGetJoystickButtons(stick);
			while (byteBuffer.hasRemaining()) {
				byte button = byteBuffer.get();
				System.out.print(count2 + "," + button + " ");
				count2++;
			}
			System.out.println();

			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				throw new RuntimeException(e);
			}
		}
	}

}

The execution result should look like this.

JoyStick(0)Name:PC Game Controller        SVEN X-PAD
Axes:0,1.5259022E-5 1,1.5259022E-5 2,1.5259022E-5 3,1.5259022E-5 4,1.5259022E-5 Button:0,0 1,0 2,0 3,0 4,0 5,0 6,0 7,0 8,0 9,0 10,0 11,0 12,0 13,0 14,0 15,0 
Axes:0,1.5259022E-5 1,1.5259022E-5 2,1.5259022E-5 3,1.5259022E-5 4,1.5259022E-5 Button:0,0 1,0 2,0 3,0 4,0 5,0 6,0 7,0 8,0 9,0 10,0 11,0 12,0 13,0 14,0 15,0 
Axes:0,-0.3515526 1,1.5259022E-5 2,1.5259022E-5 3,1.5259022E-5 4,1.5259022E-5 Button:0,0 1,0 2,0 3,0 4,0 5,0 6,0 7,0 8,0 9,0 10,0 11,0 12,0 13,0 14,0 15,0 
Axes:0,-1.0 1,-0.20311284 2,1.5259022E-5 3,1.5259022E-5 4,1.5259022E-5 Button:0,0 1,0 2,0 3,0 4,0 5,0 6,0 7,0 8,0 9,0 10,0 11,0 12,0 13,0 14,0 15,0 
Axes:0,-1.0 1,-0.17967498 2,1.5259022E-5 3,1.5259022E-5 4,1.5259022E-5 Button:0,0 1,0 2,0 3,0 4,0 5,0 6,0 7,0 8,0 9,0 10,0 11,0 12,0 13,0 14,0 15,0 
Axes:0,-1.0 1,1.5259022E-5 2,1.5259022E-5 3,1.5259022E-5 4,1.5259022E-5 Button:0,0 1,0 2,0 3,0 4,0 5,0 6,0 7,0 8,0 9,0 10,0 11,0 12,0 13,0 14,0 15,0 
Axes:0,-1.0 1,1.5259022E-5 2,1.5259022E-5 3,1.5259022E-5 4,1.5259022E-5 Button:0,0 1,0 2,0 3,0 4,0 5,0 6,0 7,0 8,0 9,0 10,0 11,0 12,0 13,0 14,0 15,0 
Axes:0,-1.0 1,1.0 2,1.5259022E-5 3,1.5259022E-5 4,1.5259022E-5 Button:0,0 1,0 2,0 3,0 4,0 5,0 6,0 7,0 8,0 9,0 10,0 11,0 12,0 13,0 14,0 15,0 
Axes:0,1.5259022E-5 1,1.0 2,1.5259022E-5 3,1.5259022E-5 4,1.5259022E-5 Button:0,0 1,0 2,0 3,0 4,0 5,0 6,0 7,0 8,0 9,0 10,0 11,0 12,0 13,0 14,0 15,0 

You can use up to 16 joysticks in vain. Only the first one is targeted here. The first float buffer that can be obtained with glfwGetJoystickAxes () is the X coordinate of the left stick, and the second is the Y coordinate. Axes has 5 buffers, but the right stick and another axis. what's this? ?? ?? Whether it's analog or digital, the center of the stick isn't 0.0. It seems that 0.0 cannot be obtained unless the absolute value of 0.01 or less is truncated. The byte buffer that can be obtained by glfwGetJoystickButtons () seems to be in the order of button numbers. There are 16 Buttons and 16 buffers, but only the first few will be used.

The point is glfwInit (). I didn't do this and it didn't work. What's wrong with Init? .. ..

According to LWJGL's method, it seems that you have to do something like ╩╗import static org.lwjgl.glfw.GLFW. *;`, But it's not Java-like, so I import it and use it.

The rest is free to boil or bake.

Recommended Posts

Receive joystick input in Java LW JFL
Read standard input in Java
Very simple input reception in Java
Console input in Java (understanding the mechanism)
[Java] How to receive numerical data separated by spaces in standard input
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
How to input / output IBM mainframe files in Java?
[Java] Receive standard input while performing numerical checks, etc.
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Joystick settings in OpenDS
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
java Scanner loop input
Talk about using Java input wait (Scanner) in VS Code