MoonWorks-docs/content/Input/_index.md

96 lines
2.5 KiB
Markdown
Raw Normal View History

2021-01-25 05:09:37 +00:00
---
title: "Input"
date: 2021-01-23T16:39:09-08:00
2021-01-25 05:17:02 +00:00
weight: 3
2021-01-25 05:09:37 +00:00
---
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:
```cs
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:
```cs
if (Inputs.Mouse.RightButton.IsReleased)
{
// do stuff
}
```
You can look up keyboard key states with the `Keycode` enum. For example:
```cs
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.
```cs
var mouseX = Inputs.Mouse.X;
var deltaMouseY = Inputs.Mouse.DeltaY;
```
`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:
```cs
Inputs.Mouse.RelativeMode = true;
```
## Gamepad Input
Checking if a gamepad is connected is easy! Gamepads can be connected to slots 0-3.
```cs
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.
2021-01-25 05:09:37 +00:00
MoonWorks assumes your gamepad 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];
```cs
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.
```cs
var leftMotorAmount = 0.2;
var rightMotorAmount = 0.5;
var durationInMilliseconds = 10;
gamepad.SetVibration(leftMotorAmount, rightMotorAmount, durationInMilliseconds)
2021-01-25 05:09:37 +00:00
```