melonJS
    Preparing search index...

    Function activeGamepad

    • The index of the pad the player is actually using.

      Slots are handed out in plug order, so index 0 is a guess rather than an answer: a wheel, a spare pad or a keyboard-shaped device left plugged in can hold slot 0 for a whole session while the player presses buttons on slot 1. Nothing in the browser reports which is "the" controller, and the one thing that identifies it is use.

      So this reports the last pad to produce a BOUND input, falling back to the first connected one before anything has been pressed, and to -1 when there is no pad at all. Pass it wherever an index is wanted — input.vibrateGamepad most of all, since rumbling the wrong pad is both useless and impossible to notice in testing.

      It follows bound inputs only, which is what makes it an answer about the player rather than about electrical noise: a resting stick on a worn pad reports movement for ever, and would otherwise claim to be the active one.

      Returns number

      the gamepad index, or -1 when no gamepad is connected

      // rumble whichever pad the player is holding
      input.vibrateGamepad(input.activeGamepad(), { duration: 120 });