MoonWorks-docs/content/Input/_index.md

3.2 KiB

title date weight
Input 2021-01-23T16:39:09-08:00 3

All input-related information can be retrieved through the Inputs. This class is automatically updated with input information and a reference to it can be retrieved from your Game subclass.

Buttons

There are four properties that buttons can have, and these apply to keyboard, mouse, and gamepad inputs.

IsPressed means that the button was pressed this frame.

IsHeld means that the button has been held down for at least two frames.

IsDown means that the button was pressed or held on this frame.

IsReleased means that the button is not currently being pressed.

If you wanted to check if the A button on Gamepad 0 was pressed this frame, you would do this:

if (Inputs.GetGamepad(0).A.IsPressed)
{
    // do stuff
}

If you wanted to check if the right mouse button was not pressed, you could do this:

if (Inputs.Mouse.RightButton.IsReleased)
{
    // do stuff
}

You can look up keyboard key states with the Keycode enum. For example:

if (Inputs.Keyboard.IsDown(Keycode.S))
{
    // do stuff
}

would tell you that the S key is currently down.

Mouse Input

In addition to the mouse buttons, there are two kinds of positional information: window-relative position and per-frame deltas.

var mouseX = Inputs.Mouse.X;
var deltaMouseY = Inputs.Mouse.DeltaY;

You can also read scroll wheel movement. Negative values are down-scrolls, positive values are up-scrolls.

var wheel = Inputs.Mouse.Wheel;

RelativeMode hides the mouse and does not update the position of the mouse while still returning movement deltas. This is ideal for creating FPS-style controls. To enable it, simply do:

Inputs.Mouse.RelativeMode = true;

Gamepad Input

Checking if a gamepad is connected is easy! Gamepads can be connected to slots 0-3.

if (Inputs.GamepadExists(0))
{
    var gamepad = Inputs.GetGamepad(0);
}

GetGamepad will throw if a gamepad is not connected so you should make sure to check GamepadExists before using it.

MoonWorks assumes that your gamepad is laid out similarly to a standard Xbox 360 controller and that it has a left stick, a right stick, and two triggers. The stick axes are represented in the range [-1f, 1f] and the trigger axes in the range [0f, 1f].

The buttons correspond to the physical location of the buttons on an Xbox 360 controller. So on a DualShock 4 controller, for example, Moonworks would treat the Cross button as an A button input.

var xButtonDown = gamepad.X.IsDown;
var leftStickHorizontal = gamepad.LeftX;
var rightStickVertical = gamepad.RightY;
var leftTrigger = gamepad.LeftTrigger;

You can also set controller vibration by providing left and right motor amounts and a duration in milliseconds.

var leftMotorAmount = 0.2;
var rightMotorAmount = 0.5;
var durationInMilliseconds = 10;
gamepad.SetVibration(leftMotorAmount, rightMotorAmount, durationInMilliseconds)

Text Input

Sometimes you might want to do something like divert keyboard inputs into a text box. Inputs provides a TextInput callback that you can hook into for this purpose.

Inputs.TextInput += c =>
{
    ImGui.GetIO().AddInputCharacter(c);
}