Skip to content

Tutorials

Adérito Silva edited this page Jun 22, 2022 · 15 revisions

This page contains tutorials about how to implement common functionality related to gamepads on your application, using XInputium. Because different game or application frameworks use different means for performing the same tasks, the tutorials in this page will assume a generic fictional application framework, instead of using specific existing frameworks, unless specified otherwise.

How to determine if a device is connected

TODO

How to switch to another connected device, when the current device gets disconnected

TODO

How to switch to a device when it gets any user input activity

TODO

How to get the current input state of a device

TODO

How to perform an action when a button is pressed

TODO

How to perform an action when a button is released

TODO

How to perform an action after a button is held for a certain duration

TODO

How to perform an action when a button is tapped or another when the button is held

TODO

How to make a game character jump higher depending on how long a button is held

XGamepad gamepad = new();

XButtons jumpButton = XButtons.A;  // The jump button.
double maxJumpHoldTime = 750;  // Button hold time for the highest jump, in milliseconds.

// Register a dynamic event that fires when the button is released or when the 
// button is held for the `maxJumpHoldTime`, whatever happens first.
gamepad.RegisterActivationInputEvent(
    () => gamepad.Buttons.IsPressed(jumpButton),
    TimeSpan.Zero, TimeSpan.Zero,
    TimeSpan.FromMilliseconds(maxJumpHoldTime),
    ActivationInputEventTriggerMode.OnDeactivation,
    (s, e) =>
    {
        // Determine the jump force, using the duration of the button hold.
        float jumpForce = (float)(e.PreviousStateDuration.TotalMilliseconds / maxJumpHoldTime);
        // Make the jump force non-linear, so pressing the button shortly will not jump
        // with a too low force. Optionally, we could also set a minimum jump force.
        jumpForce = NonLinearFunctions.QuadraticEaseOut(jumpForce);
        // Call our method that will make the game character jump.
        Jump(jumpForce);
    });

// Call this on every game/application frame.
gamepad.Update();

// Make the character jump, with a force within the 0-1 range.
void Jump(float force)
{
    Debug.WriteLine($"Jump! (force: {force:P0})");
}

How to make an action start repeating while a button is held

TODO

How to perform an action when two buttons are pressed simultaneously

TODO

How to perform an action when any button is pressed

TODO

How to perform an action when a joystick or trigger moves

TODO

How to perform an action that depends on how fast a joystick or trigger moves

TODO

How to make a game character walk or run according to a joystick's position

TODO

How to make a joystick or a trigger more sensitive

TODO

How to make a joystick smoother

TODO

How to set up a dead-zone for a joystick or a trigger

TODO

How to use a joystick as a digital directional pad

TODO

How to make the left and right triggers behave as buttons

TODO

How to make a gamepad vibrate

TODO

How to enable/disable vibration in my game/application

TODO

How to disable input when my game/application loses focus

TODO

How to perform an action when or while a specific condition is met

TODO

Clone this wiki locally