The CCINPUT library allows creative computing to communicate with input devices like joysticks, joypads but also keyboards and mice. Using CCINPUT on MacOSX for example allows you to address more than one mouse. CCINPUT bases on a modified JInput version. To get access to the different devices it has to communicate with native libraries.
To use a device with CCINPUT you should ensure that it is properly installed first. Often a joystick has to be calibrated to work correctly. Use the printDevices() function to get a list of the available devices. There are two ways to use CCINPUT you can access an device and ask for the current state of its buttons and sliders and you can add listeners that handle the events of buttons.
CCINPUT is tested on Windows and MacOSX but should also work on Linux.
download comes with examples, javadoc and src
download p5 packed as processing library including examples, javadoc and src
To use ccinput you first have to initialize it. After that you can query the connected devices. Get devices by name or id and add listeners to react to button events or get the values of sticks and sliders.
package cc.creativecomputing.input.test; import cc.creativecomputing.CCApp; import cc.creativecomputing.CCApplicationManager; import cc.creativecomputing.CCApplicationSettings; import cc.creativecomputing.input.CCInputButtonListener; import cc.creativecomputing.input.CCInputDevice; import cc.creativecomputing.input.CCInputIO; import cc.creativecomputing.input.CCInputStick; public class CCInputTest extends CCApp { public CCInputTest(CCApplicationSettings theSettings) { super(theSettings); } private CCInputStick _myStick1; private CCInputStick _myStick2; public void setup() { // init CCInput CCInputIO.init(this); //print out devices to get your device name and see if the // devices are proper installed CCInputIO.printDevices(); // get the input device // you can do that by name or by id CCInputDevice myJoypad = CCInputIO.device("Logitech RumblePad 2 USB"); // add a listener to the second button // to change color myJoypad.button(1).addListener(new CCInputButtonListener() { public void onRelease() { g.color(255); } public void onPress() { g.color(255,0,0); } }); // get the first stick of the joypad for translation _myStick1 = myJoypad.stick(0); // translate by 100 pixels per second _myStick1.multiplier(100); // get the second stick for rotation _myStick2 = myJoypad.stick(1); // set tolerance to ignore to small values _myStick2.tolerance(0.06f); // rotate by 90 degrees per second _myStick2.multiplier(90f); } public void draw() { g.clear(); // apply translation g.translate(_myStick1.totalX(),-_myStick1.totalY(),0); // apply rotation g.rotateX(_myStick2.totalY()); g.rotateY(_myStick2.totalX()); g.box(200); } static public void main(String[] args) { CCApplicationManager<CCInputTest> myManager = new CCApplicationManager<CCInputTest>(CCInputTest.class); myManager.start(); } }
To start CCInput you call the init() function of CCInputIO.
CCInputIO.init(CCAbstractApp theApp)
This initializes all available control devices. If you use CCInput in creative computing you just pass a reference to your application. If you use it processing you have to create an instance of the CCP5App. Look here for more information about that.
After initialization you can print out a list of all available devices, using the printDevices() method.
CCInputIO.printDevices()
This prints out all devices connected to your machine with its names and ids. Here you see an example output.
<<< available input devices: >>>
0: Apple Internal Keyboard / Trackpad
1: Apple Internal Keyboard / Trackpad
2: Apple Internal Keyboard / Trackpad
3: Logitech RumblePad 2 USB
<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Now you can use one of the device() methods to get the device either by its name or id.
CCInputIO.device(int theID); CCInputIO.device(String theDeviceName)
Referring to the example output if you want to get the Logitech Rumblepad you can it like this.
//get device by id CCInputDevice myRumblePad = CCInputIO.device(3); // get device by name CCInputDevice myRumblePad = CCInputIO.device(Logitech RumblePad 2 USB);
You can query all control elements of a device by calling the printDeviceInfo() method.
myInputDevice.printDeviceInfo()
This prints out all sticks, sliders and buttons of your device. Now you get access to the different elements of the device using this information.
_myInputDevice.stick(int theID); _myInputDevice.stick(String theName);
This returns a stick of the device by its name or id. Once you have a stick you can get its current value. This describes how far a joystick is moved. Dependent to which side you move the stick you get values between -1 and 1 for the X and Y axis.
_myStick.x(); // returns the current value of the x axis _myStick.y(); // returns the current value of the y axis _myStick.value(); // returns values as one vector2f object
Often you not only want to know the actual value of a stick but the value dependent on how you move a stick over time. You can get this by calling totalValue(). The total value of a stick is the sum of all single values.
_myStick.totalX(); // returns the total value of the x axis _myStick.totalY(); // returns the total value of the y axis _myStick.totalValue(); // returns total values as one vector2f object
You can set the total values back to zero using the reset method.
_myStick.reset(); // sets total values back to 0
To change the scale of the stick you can use the multiplier method.
_myStick.multiplier(float theValue);
This sets the range of the stick values from -theValue to theValue instead of -1 to 1. Another problem with joysticks sometimes is that they return values although they are not touched. If this happens first try to calibrate the controller. If this does not help you can define a threshold using the tolerance method. Values below the defined threshold will be ignored.
_myStick.tolerance(float theValue);
Sliders work pretty much like sticks instead of the x() and y() methods to get the current or total value, they only have a value() and totalValue() method return the current or total value as float. The multiplier() and tolerance() methods work like with the stick.
_myInputDevice.slider(int theID); _myInputDevice.slider(String theName);
This returns a slider of the device by its name or id.