Line 9: |
Line 9: |
| <dd> | | <dd> |
| You can check if any [[#SButton|controller/keyboard/mouse button]] is currently pressed by calling the <tt>IsDown(button)</tt> method. For example: | | You can check if any [[#SButton|controller/keyboard/mouse button]] is currently pressed by calling the <tt>IsDown(button)</tt> method. For example: |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| bool isShiftPressed = this.Helper.Input.IsDown(SButton.LeftShift) || this.Helper.Input.IsDown(SButton.RightShift); | | bool isShiftPressed = this.Helper.Input.IsDown(SButton.LeftShift) || this.Helper.Input.IsDown(SButton.RightShift); |
− | </source> | + | </syntaxhighlight> |
| </dd> | | </dd> |
| | | |
Line 17: |
Line 17: |
| <dd> | | <dd> |
| For more finetuned control, you can check the [[#SButton|button]] state relative to the previous game tick: | | For more finetuned control, you can check the [[#SButton|button]] state relative to the previous game tick: |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| SButtonState state = this.Helper.Input.GetState(SButton.LeftShift); | | SButtonState state = this.Helper.Input.GetState(SButton.LeftShift); |
| bool isDown = (state == SButtonState.Pressed || state == SButtonState.Held); | | bool isDown = (state == SButtonState.Pressed || state == SButtonState.Held); |
− | </source> | + | </syntaxhighlight> |
| Available button states: | | Available button states: |
| {| class="wikitable" | | {| class="wikitable" |
Line 51: |
Line 51: |
| | | |
| For example: | | For example: |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| // draw text at the cursor position | | // draw text at the cursor position |
| ICursorPosition cursorPos = this.Helper.Input.GetCursorPosition(); | | ICursorPosition cursorPos = this.Helper.Input.GetCursorPosition(); |
| Game1.spriteBatch.DrawString(Game1.smallFont, "some text", cursorPos.ScreenPixels, Color.Black); | | Game1.spriteBatch.DrawString(Game1.smallFont, "some text", cursorPos.ScreenPixels, Color.Black); |
− | </source> | + | </syntaxhighlight> |
| | | |
| ===Suppress input=== | | ===Suppress input=== |
Line 76: |
Line 76: |
| | | |
| For example: | | For example: |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| // prevent game from seeing that LeftShift is pressed | | // prevent game from seeing that LeftShift is pressed |
| this.Helper.Input.Suppress(SButton.LeftShift); | | this.Helper.Input.Suppress(SButton.LeftShift); |
Line 85: |
Line 85: |
| // check if a button is being suppressed: | | // check if a button is being suppressed: |
| bool suppressed = this.Helper.Input.IsSuppressed(SButton.LeftShift); | | bool suppressed = this.Helper.Input.IsSuppressed(SButton.LeftShift); |
− | </source> | + | </syntaxhighlight> |
| | | |
| Side-effects: | | Side-effects: |
Line 91: |
Line 91: |
| <li>The [[Modding:Modder Guide/APIs/Events#Input.ButtonReleased|<tt>ButtonReleased</tt> event]] will be raised on the next tick for the suppressed input.</li> | | <li>The [[Modding:Modder Guide/APIs/Events#Input.ButtonReleased|<tt>ButtonReleased</tt> event]] will be raised on the next tick for the suppressed input.</li> |
| <li>Methods like <tt>helper.Input.IsDown(button)</tt> and <tt>helper.Input.GetState(button)</tt> will show the button as released for the duration of the suppression, even if it's physically still pressed. You can use <tt>helper.Input.IsSuppressed(button)</tt> to check if that's the case (it will only be true until the button is physically released): | | <li>Methods like <tt>helper.Input.IsDown(button)</tt> and <tt>helper.Input.GetState(button)</tt> will show the button as released for the duration of the suppression, even if it's physically still pressed. You can use <tt>helper.Input.IsSuppressed(button)</tt> to check if that's the case (it will only be true until the button is physically released): |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| bool isPhysicallyDown = helper.Input.IsDown(button) || helper.Input.IsSuppressed(button); | | bool isPhysicallyDown = helper.Input.IsDown(button) || helper.Input.IsSuppressed(button); |
− | </source></li> | + | </syntaxhighlight></li> |
| </ul> | | </ul> |
| | | |
Line 101: |
Line 101: |
| | | |
| SMAPI provides extensions to convert any of the other constants to <tt>SButton</tt>: | | SMAPI provides extensions to convert any of the other constants to <tt>SButton</tt>: |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| SButton key = Keys.A.ToSButton(); // SButton.A | | SButton key = Keys.A.ToSButton(); // SButton.A |
| SButton button = Buttons.A.ToSButton(); // SButton.ControllerA | | SButton button = Buttons.A.ToSButton(); // SButton.ControllerA |
| SButton input = new InputButton(true).ToSButton(); // SButton.MouseLeft | | SButton input = new InputButton(true).ToSButton(); // SButton.MouseLeft |
− | </source> | + | </syntaxhighlight> |
| | | |
| You can also convert <tt>SButton</tt> to the other constants. This uses a <tt>TryGet</tt> approach since <tt>SButton</tt> is a superset of the others (e.g. you can't convert <tt>SButton.ControllerA</tt> to a keyboard value): | | You can also convert <tt>SButton</tt> to the other constants. This uses a <tt>TryGet</tt> approach since <tt>SButton</tt> is a superset of the others (e.g. you can't convert <tt>SButton.ControllerA</tt> to a keyboard value): |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| SButton value = SButton.A; | | SButton value = SButton.A; |
| if (value.TryGetKeyboard(out Keys key)) | | if (value.TryGetKeyboard(out Keys key)) |
Line 116: |
Line 116: |
| if (value.TryGetStardewInput(out InputButton input)) | | if (value.TryGetStardewInput(out InputButton input)) |
| ...; | | ...; |
− | </source> | + | </syntaxhighlight> |
| | | |
| Two last extensions let you check how the button is mapped in the game: | | Two last extensions let you check how the button is mapped in the game: |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| SButton button = SButton.MouseLeft; | | SButton button = SButton.MouseLeft; |
| if (button.IsUseToolButton()) | | if (button.IsUseToolButton()) |
Line 125: |
Line 125: |
| else if (button.IsActionButton()) | | else if (button.IsActionButton()) |
| // perform action | | // perform action |
− | </source> | + | </syntaxhighlight> |
| | | |
| You can use <tt>SButton</tt> values directly in your [[../Config|config model]], and they'll be represented by their names: | | You can use <tt>SButton</tt> values directly in your [[../Config|config model]], and they'll be represented by their names: |
| {| class="wikitable" | | {| class="wikitable" |
− | | <source lang="c#"> | + | | <syntaxhighlight lang="c#"> |
| internal class ModConfig | | internal class ModConfig |
| { | | { |
| public SButton DoThingButton { get; set; } = SButton.LeftControl; | | public SButton DoThingButton { get; set; } = SButton.LeftControl; |
| } | | } |
− | </source> | + | </syntaxhighlight> |
| | → | | | → |
− | | <source lang="json"> | + | | <syntaxhighlight lang="json"> |
| { | | { |
| "DoThingButton": "LeftControl" | | "DoThingButton": "LeftControl" |
| } | | } |
− | </source> | + | </syntaxhighlight> |
| |} | | |} |
| | | |
Line 150: |
Line 150: |
| You can use a <tt>KeybindList</tt> directly in [[../Config|your <tt>config.json</tt> model]]: | | You can use a <tt>KeybindList</tt> directly in [[../Config|your <tt>config.json</tt> model]]: |
| | | |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| class ModConfig | | class ModConfig |
| { | | { |
| public KeybindList ToggleKey { get; set; } = KeybindList.Parse("Shift + F2"); | | public KeybindList ToggleKey { get; set; } = KeybindList.Parse("Shift + F2"); |
| } | | } |
− | </source> | + | </syntaxhighlight> |
| | | |
| And you can then check whether it's pressed directly in your code. For example, in a [[../Events|<tt>ButtonsChanged</tt> event handler]]: | | And you can then check whether it's pressed directly in your code. For example, in a [[../Events|<tt>ButtonsChanged</tt> event handler]]: |
| | | |
− | <source lang="c#"> | + | <syntaxhighlight lang="c#"> |
| private void OnButtonsChanged(object sender, ButtonsChangedEventArgs e) | | private void OnButtonsChanged(object sender, ButtonsChangedEventArgs e) |
| { | | { |
Line 167: |
Line 167: |
| } | | } |
| } | | } |
− | </source> | + | </syntaxhighlight> |
| | | |
| The <tt>KeybindList</tt> provides a number of methods depending on your use case: | | The <tt>KeybindList</tt> provides a number of methods depending on your use case: |