MoonWorks-docs/content/Input/_index.md

76 lines
1.9 KiB
Markdown

---
title: "Input"
date: 2021-01-23T16:39:09-08:00
weight: 2
---
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
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= Inputs.GetGamepad(0).LeftX;
var rightStickVertical = Inputs.GetGamepad(0).RightY;
var leftTrigger = Inputs.GetGamepad(0).LeftTrigger;
```