mirror of
https://github.com/shadowfacts/jinput-arm64.git
synced 2025-12-06 08:01:59 +01:00
Added commented examples
This commit is contained in:
parent
795f0d1a3f
commit
4c295d9219
|
|
@ -0,0 +1,92 @@
|
|||
package net.java.games.input.example;
|
||||
|
||||
import net.java.games.input.Component;
|
||||
import net.java.games.input.Controller;
|
||||
import net.java.games.input.ControllerEnvironment;
|
||||
import net.java.games.input.Event;
|
||||
import net.java.games.input.EventQueue;
|
||||
|
||||
/**
|
||||
* This class shows how to use the event queue system in JInput. It will show
|
||||
* how to get the controllers, how to get the event queue for a controller, and
|
||||
* how to read and process events from the queue.
|
||||
*
|
||||
* @author Endolf
|
||||
*/
|
||||
public class ReadAllEvents {
|
||||
|
||||
public ReadAllEvents() {
|
||||
while (true) {
|
||||
/* Get the available controllers */
|
||||
Controller[] controllers = ControllerEnvironment
|
||||
.getDefaultEnvironment().getControllers();
|
||||
if (controllers.length == 0) {
|
||||
System.out.println("Found no controllers.");
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
for (int i = 0; i < controllers.length; i++) {
|
||||
/* Remember to poll each one */
|
||||
controllers[i].poll();
|
||||
|
||||
/* Get the controllers event queue */
|
||||
EventQueue queue = controllers[i].getEventQueue();
|
||||
|
||||
/* Create an event object for the underlying plugin to populate */
|
||||
Event event = new Event();
|
||||
|
||||
/* For each object in the queue */
|
||||
while (queue.getNextEvent(event)) {
|
||||
|
||||
/*
|
||||
* Create a strug buffer and put in it, the controller name,
|
||||
* the time stamp of the event, the name of the component
|
||||
* that changed and the new value.
|
||||
*
|
||||
* Note that the timestamp is a relative thing, not
|
||||
* absolute, we can tell what order events happened in
|
||||
* across controllers this way. We can not use it to tell
|
||||
* exactly *when* an event happened just the order.
|
||||
*/
|
||||
StringBuffer buffer = new StringBuffer(controllers[i]
|
||||
.getName());
|
||||
buffer.append(" at ");
|
||||
buffer.append(event.getNanos()).append(", ");
|
||||
Component comp = event.getComponent();
|
||||
buffer.append(comp.getName()).append(" changed to ");
|
||||
float value = event.getValue();
|
||||
|
||||
/*
|
||||
* Check the type of the component and display an
|
||||
* appropriate value
|
||||
*/
|
||||
if (comp.isAnalog()) {
|
||||
buffer.append(value);
|
||||
} else {
|
||||
if (value == 1.0f) {
|
||||
buffer.append("On");
|
||||
} else {
|
||||
buffer.append("Off");
|
||||
}
|
||||
}
|
||||
System.out.println(buffer.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Sleep for 20 milliseconds, in here only so the example doesn't
|
||||
* thrash the system.
|
||||
*/
|
||||
try {
|
||||
Thread.sleep(20);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new ReadAllEvents();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,86 @@
|
|||
package net.java.games.input.example;
|
||||
|
||||
import net.java.games.input.Component;
|
||||
import net.java.games.input.Controller;
|
||||
import net.java.games.input.ControllerEnvironment;
|
||||
import net.java.games.input.test.ControllerTextTest;
|
||||
|
||||
/**
|
||||
* This class shows how to read the values in a polling loop for the first mouse
|
||||
* detected. It will show how to get the available controllers, how to check the
|
||||
* type of the controller, how to read the components of the controller, and how
|
||||
* to get the data from the component.
|
||||
*
|
||||
* @author Endolf
|
||||
*/
|
||||
public class ReadFirstMouse {
|
||||
|
||||
public ReadFirstMouse() {
|
||||
/* Get the available controllers */
|
||||
Controller[] controllers = ControllerEnvironment
|
||||
.getDefaultEnvironment().getControllers();
|
||||
|
||||
/*
|
||||
* Loop through the controllers, check the type of each one, and save
|
||||
* the first mouse we find.
|
||||
*/
|
||||
Controller firstMouse = null;
|
||||
for (int i = 0; i < controllers.length && firstMouse == null; i++) {
|
||||
if (controllers[i].getType() == Controller.Type.MOUSE) {
|
||||
// Found a mouse
|
||||
firstMouse = controllers[i];
|
||||
}
|
||||
}
|
||||
if (firstMouse == null) {
|
||||
// Couldn't find a mouse
|
||||
System.out.println("Found no mouse");
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
System.out.println("First mouse is: " + firstMouse.getName());
|
||||
|
||||
while (true) {
|
||||
/* Poll the controller */
|
||||
firstMouse.poll();
|
||||
|
||||
/* Get all the axis and buttons */
|
||||
Component[] components = firstMouse.getComponents();
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
|
||||
/* For each component, get it's name, and it's current value */
|
||||
for (int i = 0; i < components.length; i++) {
|
||||
if (i > 0) {
|
||||
buffer.append(", ");
|
||||
}
|
||||
buffer.append(components[i].getName());
|
||||
buffer.append(": ");
|
||||
if (components[i].isAnalog()) {
|
||||
/* Get the value at the last poll of this component */
|
||||
buffer.append(components[i].getPollData());
|
||||
} else {
|
||||
if (components[i].getPollData() == 1.0f) {
|
||||
buffer.append("On");
|
||||
} else {
|
||||
buffer.append("Off");
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println(buffer.toString());
|
||||
|
||||
/*
|
||||
* Sleep for 20 millis, this is just so the example doesn't thrash
|
||||
* the system.
|
||||
*/
|
||||
try {
|
||||
Thread.sleep(20);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new ReadFirstMouse();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue