Line 126: |
Line 126: |
| } | | } |
| </source> | | </source> |
| + | |} |
| + | |
| + | ===KeybindList=== |
| + | {{SMAPI upcoming|3.9}} |
| + | |
| + | SMAPI's <tt>KeybindList</tt> utility lets you manage an arbitrary set of keybindings. A ''keybind list'' has any number of ''keybinds'', each of which has any number of [[#SButton|button codes]]. For example, the keybind list <code>"F2, LeftShift + S"</code> would be pressed if (a) <tt>F2</tt> is pressed, ''or'' (b) both <tt>LeftShift</tt> and <tt>S</tt> are pressed. |
| + | |
| + | You can use a <tt>KeybindList</tt> directly in [[../Config|your <tt>config.json</tt> model]]: |
| + | |
| + | <source lang="c#"> |
| + | class ModConfig |
| + | { |
| + | public KeybindList ToggleKey { get; set; } = KeybindList.Parse("Shift + F2"); |
| + | } |
| + | </source> |
| + | |
| + | And you can then check whether it's pressed directly in your code. For example, in a [[../Events|<tt>ButtonPressed</tt> event handler]]: |
| + | |
| + | <source lang="c#"> |
| + | private void Input_ButtonPressed(object sender, ButtonPressedEventArgs e) |
| + | { |
| + | if (this.Config.ToggleKey.JustPressed()) |
| + | { |
| + | // perform desired action |
| + | } |
| + | } |
| + | </source> |
| + | |
| + | The <tt>KeybindList</tt> provides a number of methods depending on your use case: |
| + | |
| + | {| class="wikitable" |
| + | |- |
| + | ! member |
| + | ! description |
| + | |- |
| + | | <code>KeybindList.Parse(…)</code><br /><code>KeybindList.TryParse(…)</code> |
| + | | Parse a keybind string like <code>"F2, LeftShift + S"</code> into a keybind list. |
| + | |- |
| + | | <code>IsBound</code> |
| + | | Whether the keybind list has any keys bound. For example, this would be false for the strings <code>"None"</code> or <code>""</code>. |
| + | |- |
| + | | <code>Keybinds</code> |
| + | | The individual keybinds. In most cases you shouldn't use these directly. |
| + | |- |
| + | | <code>GetState()</code> |
| + | | Get the overall [[#Check button state|keybind state relative to the previous tick]]. This state is transitive across keybinds; e.g. if the player releases one keybind and immediately presses another within the list, the overall state is <tt>Held</tt>. |
| + | |- |
| + | | <code>IsDown()</code> |
| + | | Get whether any keybind in the list is currently down (i.e. the player pressed or is holding down the keys). |
| + | |- |
| + | | <code>JustPressed()</code> |
| + | | Get whether the player just activated the keybind during the current tick (i.e. <code>GetState()</code> returns <tt>Pressed</tt> instead of <tt>Held</tt>). |
| + | |- |
| + | | <code>GetKeybindCurrentlyDown()</code> |
| + | | Get the individual <tt>Keybind</tt> in the list which is currently down, if any. If there are multiple keybinds down, the first one is returned. |
| + | |- |
| + | | <code>ToString()</code> |
| + | | Get a string representation of the input binding (e.g. <code>"F2, LeftShift + S"</code>). |
| |} | | |} |
| | | |