https://jinput.dev.java.net/
they wrote the api
Hope it helps

Moderator: Moderators
Code: Select all
package org.egl.spaceControler;
import java.util.Timer;
import net.java.games.input.Component;
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;
/**
* Details for: SpaceNavigator, Stick, Unknown
* Components: (8)
* 0. Z Axis, z, relative, analog, 0.0
* 1. Y Axis, y, relative, analog, 0.0
* 2. X Axis, x, relative, analog, 0.0
* 3. Z Rotation, rz, relative, analog, 0.0
* 4. Y Rotation, ry, relative, analog, 0.0
* 5. X Rotation, rx, relative, analog, 0.0
* 6. Button 0, 0, absolute, digital, 0.0
* 7. Button 1, 1, absolute, digital, 0.0
* No Rumblers
* No subcontrollers
*/
public class SpaceControlerConnexion {
public static final int NUM_BUTTONS = 2;
public static final int ZAXIS = 0;
public static final int YAXIS = 1;
public static final int XAXIS = 2;
public static final int ZROTATION = 3;
public static final int YROTATION = 4; // default value
public static final int XROTATION = 5;
public static final int BUTTON0 = 6;
public static final int BUTTON1 = 7;
private int xAxisIdx, yAxisIdx, zAxisIdx, rxAxisIdx, ryAxisIdx, rzAxisIdx;
private int buttonsIdx[];
private Controller controller;
private Component[] comps;
public SpaceControlerConnexion () throws Exception {
ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
Controller[] cs = ce.getControllers();
if (cs.length == 0) {
System.out.println("No controller found");
throw new Exception("No Controller");
} else System.out.println("Num. controllers: " + cs.length);
controller = findSpaceNavigator(cs);
System.out.println("Space Navigator controller: " + controller.getName() + ", " + controller.getType());
findCompIndices(controller);
}
private Controller findSpaceNavigator (Controller[] cs) throws Exception {
Controller.Type type;
int i = 0;
while (i < cs.length) {
type = cs[i].getType();
System.out.println("Name : "+cs[i].getName());
System.out.println("\tType : "+cs[i].getType());
System.out.println("\tPort : "+cs[i].getPortNumber());
if (cs[i].getName().contains("3Dconnexion")) break;
i++;
}
if (i == cs.length) {
System.out.println("No Space Navigator found");
throw new Exception("No Space Controler available.");
} else System.out.println("No Space Navigator index: " + i);
return cs[i];
}
private void findCompIndices (Controller controller)
/* Store the indices for the analog sticks axes
(x,y) and (z,rz), POV hat, and
button components of the controller.
*/ {
comps = controller.getComponents();
if (comps.length == 0) {
System.out.println("No Components found");
System.exit(0);
} else System.out.println("Num. Components: " + comps.length);
showCompNames(comps);
// get the indices for the axes of the analog sticks: (x,y) and (z,rz)
xAxisIdx = findCompIndex(comps, Component.Identifier.Axis.X, "x",1);
yAxisIdx = findCompIndex(comps, Component.Identifier.Axis.Y, "y",1);
zAxisIdx = findCompIndex(comps, Component.Identifier.Axis.Z, "z",1);
rxAxisIdx = findCompIndex(comps, Component.Identifier.Axis.RX, "rx",1);
if (rxAxisIdx==-1)
rxAxisIdx = findCompIndex(comps, Component.Identifier.Axis.SLIDER_VELOCITY, "slider-velocity",1);
ryAxisIdx = findCompIndex(comps, Component.Identifier.Axis.RY, "ry",1);
if (ryAxisIdx==-1)
ryAxisIdx = findCompIndex(comps, Component.Identifier.Axis.SLIDER_VELOCITY, "slider-velocity",2);
rzAxisIdx = findCompIndex(comps, Component.Identifier.Axis.RZ, "rz",1);
if (rzAxisIdx==-1)
rzAxisIdx = findCompIndex(comps, Component.Identifier.Axis.SLIDER_VELOCITY, "slider-velocity",3);
findButtons(comps);
}
private void showCompNames (Component[] comps) {
Component c;
System.out.println("Components : ");
for (int i = 0; i < comps.length; i++) {
c = comps[i];
System.out.println( "\t"+c.getName() + " ID :" + c.getIdentifier().getName());
}
}
private int findCompIndex (Component[] comps, Component.Identifier id, String nm, int index) {
int counter = 1;
Component c;
for (int i = 0; i < comps.length; i++) {
c = comps[i];
if ((c.getIdentifier() == id))
if ((counter==index)) {
System.out.println("Found " + c.getName() + "; index: " + i);
return i;
}
else
counter ++;
}
System.out.println("No " + nm + " component found");
return -1;
}
/**
* Search through comps[] for NUM_BUTTONS buttons, storing
* their indices in buttonsIdx[]. Ignore excessive buttons.
* If there aren't enough buttons, then fill the empty spots in
* buttonsIdx[] with -1's.
*/
private void findButtons (Component[] comps) {
buttonsIdx = new int[NUM_BUTTONS];
int numButtons = 0;
Component c;
for (int i = 0; i < comps.length; i++) {
c = comps[i];
if (isButton(c)) { // deal with a button
if (numButtons == NUM_BUTTONS) // already enough buttons
System.out.println("Found an extra button; index: " + i + ". Ignoring it");
else {
buttonsIdx[numButtons] = i; // store button index
System.out.println("Found " + c.getName() + "; index: " + i);
numButtons++;
}
}
}
// fill empty spots in buttonsIdx[] with -1's
if (numButtons < NUM_BUTTONS) {
System.out.println("Too few buttons (" + numButtons + "); expecting " + NUM_BUTTONS);
while (numButtons < NUM_BUTTONS) {
buttonsIdx[numButtons] = -1;
numButtons++;
}
}
} // end of findButtons()
/**
* Return true if the component is a digital/absolute button, and
* its identifier name ends with "Button" (i.e. the
* identifier class is Component.Identifier.Button).
*/
private boolean isButton (Component c) {
if (!c.isAnalog() && !c.isRelative()) { // digital and absolute
String className = c.getIdentifier().getClass().getName();
// System.out.println(c.getName() + " identifier: " + className);
if (className.endsWith("Button")) return true;
}
return false;
}
/**
* Return all the buttons in a single array. Each button value is
* a boolean.
*/
public boolean[] getButtons () {
boolean[] buttons = new boolean[NUM_BUTTONS];
float value;
for (int i = 0; i <NUM_BUTTONS> NUM_BUTTONS)) {
System.out.println("Button position out of range (1-" + NUM_BUTTONS + "): " + pos);
return false;
}
if (buttonsIdx[pos - 1] == -1) // no button found at that pos
return false;
float value = comps[buttonsIdx[pos - 1]].getPollData();
// array range is 0-NUM_BUTTONS-1
return ( (value == 0.0f) ? false : true);
} // end of isButtonPressed()
public void poll () {
controller.poll();
}
/**
* X Translation
*
* @return float value between 1613 and -1613
* <p>
* Note: the returned value my not be very logic and correct, I just measured them during pooling
*/
public float getTX () {
return comps[xAxisIdx].getPollData();
}
/**
* Y Translation
*
* @return float value between 1613 and -1613
* <p>
* Note: the returned value my not be very logic and correct, I just measured them during pooling
*/
public float getTY () {
return comps[yAxisIdx].getPollData();
}
/**
* Z Translation
*
* @return float value between 1613 and -1613
* <p>
* Note: the returned value my not be very logic and correct, I just measured them during pooling
*/
public float getTZ () {
return comps[zAxisIdx].getPollData();
}
/**
* X Rotation
*
* @return float value between 1560 and -1560
* <p>
* Note: the returned value my not be very logic and correct, I just measured them during pooling
*/
public float getRX () {
return comps[rxAxisIdx].getPollData();
}
/**
* Y Rotation
*
* @return float value between 1560 and -1560
* <p>
* Note: the returned value my not be very logic and correct, I just measured them during pooling
*/
public float getRY () {
return comps[ryAxisIdx].getPollData();
}
/**
* Z Rotation
*
* @return float value between 1560 and -1560
* <p>
* Note: the returned value my not be very logic and correct, I just measured them during pooling
*/
public float getRZ () {
return comps[rzAxisIdx].getPollData();
}
}