Last active
August 29, 2015 13:57
-
-
Save zkxs/9576344 to your computer and use it in GitHub Desktop.
Example of how to use JInput to read the state of the POV hat switch.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Initialize JInput and get reference to `controller` | |
* Implementing this is left as an exercise to the reader | |
*/ | |
initializeJInput(); | |
String[] directions = { "NW", "N", "NE", "E", "SE", "S", "SW", "W" }; | |
EventQueue eventQueue = controller.getEventQueue(); | |
Event event = new Event(); // reused by the EventQueue | |
Component hatSwitch = controller.getComponent(Component.Identifier.Axis.POV); | |
float oldValue = 0.0f; // hat neutral state = 0.0 | |
while (controller.poll()) | |
{ | |
/* | |
* Normal range is from 0 to 1. convert to 0 to 8. | |
*/ | |
float value = hatSwitch.getPollData() * 8; | |
if (value != oldValue) // only operate on changes in component value | |
{ | |
oldValue = value; // update old value | |
// make sure the value is an integer in a valid range | |
if (value % 1 != 0 && value >= 0.0f && value <= 8.0f) | |
{ | |
System.err.println("Invalid value: " + value); | |
} | |
else if (value != 0.0f) // if value is a valid index | |
{ | |
System.out.println(directions[(int)value - 1]); | |
} | |
} | |
try | |
{ | |
Thread.sleep(20); // don't poll constantly | |
} | |
catch (InterruptedException e) | |
{} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment