|
| 1 | +# Orbit Input |
| 2 | + |
| 3 | +Orbit.Input provides the ability to interact with both Keyboards and Game Controllers inside .NET MAUI based applications. |
| 4 | + |
| 5 | +## Game Controller |
| 6 | + |
| 7 | +Add an image of a game controller and explain the button layout... |
| 8 | + |
| 9 | +### Example usage |
| 10 | + |
| 11 | +This section aims at explaining how to add game controller support to your project. |
| 12 | + |
| 13 | +### Registering with the `MauiAppBuilder` |
| 14 | + |
| 15 | +The first step is to register the game engine in your `MauiProgram.cs` file using the `UseOrbitGameController` extension method: |
| 16 | + |
| 17 | +```csharp |
| 18 | +builder |
| 19 | + .UseMauiApp<App>() |
| 20 | + .UseOrbitGameController() |
| 21 | +``` |
| 22 | + |
| 23 | +```csharp |
| 24 | +builder.Services.AddSingleton(GameControllerManager.Current); |
| 25 | +``` |
| 26 | + |
| 27 | +### Discovering connected controllers |
| 28 | + |
| 29 | +```csharp |
| 30 | +await GameControllerManager.Current.StartDiscovery(); |
| 31 | +``` |
| 32 | + |
| 33 | +### Checking if a button is pressed |
| 34 | + |
| 35 | +The `GameController` class provides a set or properties making it easy to check the state of buttons or sticks. |
| 36 | + |
| 37 | +```csharp |
| 38 | +if (gameController.ButtonSouth.Value) |
| 39 | +{ |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +```csharp |
| 44 | +if (gameController.LeftStick.XAxis.Value > 0.0000001f) |
| 45 | +{ |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +### Responding to a button press |
| 50 | + |
| 51 | +The `GameController` class provides both the `ButtonChanged` and `ValueChanged` events that can be subscribed to in order to receive notifications. |
| 52 | + |
| 53 | +#### `ButtonChanged` |
| 54 | + |
| 55 | +```csharp |
| 56 | +this.gameController.ButtonChanged += GameControllerOnButtonChanged; |
| 57 | + |
| 58 | +private void GameControllerOnButtonChanged(object? sender, GameControllerButtonChangedEventArgs e) |
| 59 | +{ |
| 60 | + if (e.ButtonName == gameController.South.Name) |
| 61 | + { |
| 62 | + if (e.IsPressed) |
| 63 | + { |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +#### `ValueChanged` |
| 73 | + |
| 74 | +```csharp |
| 75 | +this.gameController.ValueChanged += GameControllerOnValueChanged; |
| 76 | + |
| 77 | +private void GameControllerOnValueChanged(object? sender, GameControllerValueChangedEventArgs e) |
| 78 | +{ |
| 79 | + if (e.ButtonName == gameController.LeftStick.XAxis.Name) |
| 80 | + { |
| 81 | + if (e.Value < 0.0000001f) |
| 82 | + { |
| 83 | + } |
| 84 | + else if (e.Value > 0.0000001f) |
| 85 | + { |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +## Keyboard |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +### Example usage |
| 96 | + |
| 97 | +This section aims at explaining how to add keyboard support to your project. |
| 98 | + |
| 99 | +### Registering with the `MauiAppBuilder` |
| 100 | + |
| 101 | +The first step is to register the game engine in your `MauiProgram.cs` file using the `UseOrbitKeyboard` extension method: |
| 102 | + |
| 103 | +```csharp |
| 104 | +builder |
| 105 | + .UseMauiApp<App>() |
| 106 | + .UseOrbitKeyboard() |
| 107 | +``` |
| 108 | + |
| 109 | +The library provides the `KeyboardManager.Current` property that can be used throughout your application. If you prefer to register the implementation with your dependency injection layer you can do so as follows: |
| 110 | + |
| 111 | +```csharp |
| 112 | +builder.Services.AddSingleton(KeyboardManager.Current); |
| 113 | +``` |
| 114 | + |
| 115 | +### Checking if a key is pressed |
| 116 | + |
| 117 | +The `KeyboardManager` class provides an indexer method to check whether a specific `KeyboardKey` is pressed. |
| 118 | + |
| 119 | +```csharp |
| 120 | +KeyboardManager.Current[KeyboardKey.ShiftLeft]; |
| 121 | +``` |
| 122 | + |
| 123 | +### Modifier keys |
| 124 | + |
| 125 | +The shift, alt and control keys are considered modifiers as they modify the behavior of other keys when pressed. You can determine whether modifer keys are pressed through the `Modifiers` property. |
| 126 | + |
| 127 | +```csharp |
| 128 | +KeyboardManager.Current.Modifiers.HasFlag(KeyboardModifier.ShiftLeft); |
| 129 | +``` |
| 130 | + |
| 131 | +### Responding to a key press |
| 132 | + |
| 133 | +The `KeyboardManager` class provides both the `KeyDown` and `KeyUp` events that can be subscribed to in order to receive notifications. |
| 134 | + |
| 135 | +##### `KeyDown` |
| 136 | + |
| 137 | +```csharp |
| 138 | +KeyboardManager.Current.KeyDown += KeyboardManagerOnKeyDown; |
| 139 | + |
| 140 | +private void KeyboardManagerOnKeyDown(object? sender, KeyboardKey e) |
| 141 | +{ |
| 142 | + if (e == KeyboardKey.KeyD) |
| 143 | + { |
| 144 | + } |
| 145 | + else if (e == KeyboardKey.KeyA) |
| 146 | + { |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +##### `KeyUp` |
| 152 | + |
| 153 | +```csharp |
| 154 | +KeyboardManager.Current.KeyUp += KeyboardManagerOnKeyUp; |
| 155 | + |
| 156 | +private void KeyboardManagerOnKeyUp(object? sender, KeyboardKey e) |
| 157 | +{ |
| 158 | + if (e == KeyboardKey.KeyD) |
| 159 | + { |
| 160 | + } |
| 161 | + else if (e == KeyboardKey.KeyA) |
| 162 | + { |
| 163 | + } |
| 164 | +} |
| 165 | +``` |
0 commit comments