2

This is the code I'm currently using. (Not my own). It detects when a controller has been plugged in, and outputs a few bits of relevant info. What I can't figure out is how to access the button data. Although it successfully recognises I've plugged in a PS3 controller, the values of the buttons don't seem to change when queried.

package;

import openfl.display.Sprite;
import openfl.events.GameInputEvent;
import openfl.ui.GameInputControl;
import openfl.ui.GameInputDevice;
import openfl.ui.GameInput;
import openfl.events.Event;

class Main extends Sprite {

    private var gameInput:GameInput;

    public function new(){
        super();
        gameInput = new GameInput();
        gameInput.addEventListener(GameInputEvent.DEVICE_ADDED, controllerAdded);
        gameInput.addEventListener(GameInputEvent.DEVICE_REMOVED, controllerRemoved);
        gameInput.addEventListener(GameInputEvent.DEVICE_UNUSABLE, controllerProblem);
    }

    function controllerAdded(e:GameInputEvent){
        //put code here to handle when a device is added

        trace("GameInput.numDevices: "+GameInput.numDevices);//tells you how many gamepads are plugged in
        var myDevice = GameInput.getDeviceAt(0);//1st gamepad is "0" - more gamepads would be "1", "2", "3", etc.
        trace("myDevice.numControls: "+myDevice.numControls); //tells you how many inputs/controls the device has
        myDevice.enabled = true; //enables the device

        var cont = myDevice.getControlAt(12);//input reference (AXIS STICK, BUTTON, TRIGGER, etc) "0" is the 1st input
        trace("id: "+cont.id);//the name of this control. Ex: "AXIS_0"
        trace("value: " + cont.value); //value of this control - Axis: -1 to 1, Button: 0 OR 1, Trigger: 0 to 1
        trace("cont: " + cont.device.name); //the name of the device. ie: "XBOX 360 Controller"
        trace("device: " + cont.device);
        trace("minValue: " + cont.minValue);//the minimum possible value for the control/input
        trace("maxValue: " + cont.maxValue);//the maximum possible value for the control/input
    }

    function controllerRemoved(e:GameInputEvent){
        trace('BLAH BLAH BLAH');
    }

    function controllerProblem(e:GameInputEvent){
        //put code here to handle when there is a problem with the controller
        trace("controller problem");
    }

}
Gama11
  • 31,714
  • 9
  • 78
  • 100
k13ran
  • 147
  • 12
  • It's not clear to me how the "when queried" part you mention is handled - the code snippet you posted just checks a button state for the `DEVICE_ADDED` event, which should only trigger once. – Gama11 Feb 12 '17 at 21:32
  • I haven't been able to implement any code to query the button state because what I tried doesn't work. I added an enterframe event loop that printed out the value of all 21 of the PS3 controller's buttons. At first it prints out all 0's, so it's good it's picking up a value, but none of the values change when any button is pressed on the controller. That's why I didn't include it, because my method could be so far wrong. – k13ran Feb 13 '17 at 00:44

1 Answers1

2

Here's the minimum amount of code needed to get controller input. It works with all controllers I have available (Xbox 360, Xbox One and a Logitech one):

import openfl.display.Sprite;
import openfl.ui.GameInput;
import openfl.ui.GameInputDevice;
import openfl.events.Event;
import openfl.events.GameInputEvent;

class Main extends Sprite {
    public function new() {
        super();
        var gameInput = new GameInput();
        var device:GameInputDevice;
        gameInput.addEventListener(GameInputEvent.DEVICE_ADDED, function(event) {
            device = event.device;
            device.enabled = true;
        });
        stage.addEventListener(Event.ENTER_FRAME, function(event) {
            trace([for (i in 0...device.numControls) device.getControlAt(i).value]);
        });
    }
}

That seems quite similar to what you tried, so I'd say chances are it's a driver issue, not a problem with your code. Note: I used OpenFL 4.7.3 and Lime 3.7.2 for testing.

I don't own a PS3 controller myself, but they're supposedly tricky to get to work on PC. ScpToolkit is often recommended and seems quite popular.

Btw, openfl-samples also has a GamepadInput sample you could give a try.

Gama11
  • 31,714
  • 9
  • 78
  • 100
  • This works perfectly for my xbox one controller (so thank you for that!). Unfortunately it doesn't work for the PS3 controller and I have no idea why. – k13ran Feb 13 '17 at 10:14
  • 1
    I missed an option in the driver menu - this solution works *perfectly*. Thank you so much! – k13ran Feb 13 '17 at 10:32