Modding:Modder Guide/APIs/Input

From Stardew Valley Wiki
< Modding:Modder Guide‎ | APIs
Revision as of 19:18, 2 June 2018 by Pathoschild (talk | contribs) (create page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Creating SMAPI mods SMAPI mascot.png


Modding:Index

The following describes the upcoming SMAPI 2.6-beta.16, and may change before release.

The input API lets you check and suppress controller/keyboard/mouse state.

Check input

Button state

You can check if any controller/keyboard/mouse button is currently pressed by calling the IsDown(button) method. For example:

bool isShiftPressed = this.Helper.Input.IsDown(SButton.LeftShift) || this.Helper.Input.IsDown(SButton.RightShift);

Cursor position

The GetCursorPosition() method provides the cursor position in three coordinate systems:

  • ScreenPixels is the pixel position relative to the top-left corner of the visible screen, adjusted for game zoom.
  • Tile is the tile position under the cursor.
  • GrabTile is the tile position that the game considers under the cursor for the purposes of clicking actions. This automatically accounts for controller mode. This may be different than Tile if that's too far from the player.

For example:

// draw text at the cursor position
ICursorPosition cursorPos = this.Helper.Input.GetCursorPosition();
Game1.spriteBatch.DrawString(Game1.smallFont, "some text", cursorPos.ScreenPixels, Color.Black);

Scroll wheel value

You can get the current mouse scroll wheel value by calling the GetMouseScrollValue() method. Note that this is the absolute scroll value; it's only meaningful when comparing to a previous scroll value. For example:

// detect when the player scrolls the mouse wheel.
// This assumes you create a custom LastScrollValue field to keep track of the previous value.
int scrollValue = this.Helper.Input.GetMouseScrollValue();
int scrollChange = scrollValue - this.LastScrollValue;
this.LastScrollValue = scrollValue;

Input suppression

You can prevent the game from handling any controller/keyboard/mouse button press (including clicks) by suppressing it. This suppression will remain in effect until the player releases the button. This won't prevent other mods from handling it.

For example, this will prevent the game from seeing that the LeftShift button is pressed:

this.Helper.Input.Suppress(SButton.LeftShift);

That works for clicks too:

this.Helper.Input.Suppress(SButton.MouseLeft);

You can also check if a button is being suppressed:

if (this.Helper.Input.IsSuppressed(SButton.LeftShift))
   // left shift button is being suppressed

See also