第21行: |
第21行: |
| bool isDown = (state == SButtonState.Pressed || state == SButtonState.Held); | | bool isDown = (state == SButtonState.Pressed || state == SButtonState.Held); |
| </syntaxhighlight> | | </syntaxhighlight> |
− | Available button states:
| + | 可用的按键状态: |
| {| class="wikitable" | | {| class="wikitable" |
| |- | | |- |
第47行: |
第47行: |
| </dl> | | </dl> |
| {{翻译}} | | {{翻译}} |
− | ===Check cursor position=== | + | === 检查光标位置=== |
− | The <samp>GetCursorPosition()</samp> method provides the [[#ICursorPosition|cursor position in three coordinate systems]].
| + | <samp>GetCursorPosition()</samp> 方法提供了[[#ICursorPosition| 三种坐标体系下的光标位置]] 。 |
| | | |
− | For example:
| + | 例如: |
| <syntaxhighlight lang="c#"> | | <syntaxhighlight lang="c#"> |
− | // 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); |
| </syntaxhighlight> | | </syntaxhighlight> |
| | | |
− | ===Suppress input=== | + | === 抑制输入=== |
− | You can prevent the game from handling any [[#SButton|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.
| + | 你可以通过抑制输入来阻止游戏对[[#SButton| 控制器/ 键盘/ 鼠标]] 的响应。这种抑制会在玩家松开按键前持续生效。这种抑制不会阻止其他mod对输入的处理。 |
| | | |
| {| class="wikitable" | | {| class="wikitable" |
| |- | | |- |
− | ! method | + | ! 方法 |
− | ! effect | + | ! 效果 |
| |- | | |- |
| | <code>Suppress</code> | | | <code>Suppress</code> |
− | | Suppress the specified [[#SButton|<samp>SButton</samp>]] value. | + | | 抑制给定的 [[#SButton|<samp>SButton</samp>]] 值。 |
| |- | | |- |
| | <code>SuppressActiveKeybinds</code> | | | <code>SuppressActiveKeybinds</code> |
− | | For the given [[#KeybindList|<samp>KeybindList</samp>]], suppress every button that's part of an activated keybind. | + | | 对于给定的[[#KeybindList|<samp>KeybindList</samp>]] ,抑制其中的每个按键输入。 |
| |- | | |- |
| | <code>IsSuppressed</code> | | | <code>IsSuppressed</code> |
− | | Get whether the specified [[#SButton|<samp>SButton</samp>]] value is currently suppressed. | + | | 检查给定的 [[#SButton|<samp>SButton</samp>]] 值是否正在受到抑制。 |
| |} | | |} |
| | | |
− | For example:
| + | 例如: |
| <syntaxhighlight lang="c#"> | | <syntaxhighlight lang="c#"> |
− | // prevent game from seeing that LeftShift is pressed | + | // 使游戏忽略LeftShift按键的动作 |
| this.Helper.Input.Suppress(SButton.LeftShift); | | this.Helper.Input.Suppress(SButton.LeftShift); |
| | | |
− | // that works for clicks too: | + | // 鼠标单击也可以被抑制 |
| this.Helper.Input.Suppress(SButton.MouseLeft); | | this.Helper.Input.Suppress(SButton.MouseLeft); |
| | | |
− | // check if a button is being suppressed: | + | // 检查某个按键是否被抑制 |
| bool suppressed = this.Helper.Input.IsSuppressed(SButton.LeftShift); | | bool suppressed = this.Helper.Input.IsSuppressed(SButton.LeftShift); |
| </syntaxhighlight> | | </syntaxhighlight> |
| | | |
− | Side-effects:
| + | 副作用: |
| <ul> | | <ul> |
− | <li>The [[Modding:Modder Guide/APIs/Events#Input.ButtonReleased|<samp>ButtonReleased</samp> event]] will be raised on the next tick for the suppressed input.</li> | + | <li>[[Modding:Modder Guide/APIs/Events#Input.ButtonReleased|<samp>ButtonReleased</samp> 事件]] 会在输入被抑制后的下一个 tick 触发。</li> |
− | <li>Methods like <samp>helper.Input.IsDown(button)</samp> and <samp>helper.Input.GetState(button)</samp> will show the button as released for the duration of the suppression, even if it's physically still pressed. You can use <samp>helper.Input.IsSuppressed(button)</samp> to check if that's the case (it will only be true until the button is physically released): | + | <li> 在按键被抑制期间,类似 <samp>helper.Input.IsDown(button)</samp> 和 <samp>helper.Input.GetState(button)</samp> 的方法会表明按键的状态是 released ,即使该按键在现实中处于被按下的状态。你可以使用 <samp>helper.Input.IsSuppressed(button)</samp> 来检查按键是否处于这种情况(它会一直返回 true 直到按键在现实中被松开): |
| <syntaxhighlight 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); |
第96行: |
第96行: |
| </ul> | | </ul> |
| | | |
− | ==Data structures== | + | == 数据结构== |
| ===SButton=== | | ===SButton=== |
− | SMAPI's <samp>SButton</samp> is a constant which includes every [https://docs.microsoft.com/en-us/previous-versions/windows/xna/bb975202(v%3dxnagamestudio.40) controller], [https://docs.microsoft.com/en-us/previous-versions/windows/xna/bb197781(v%3dxnagamestudio.40) keyboard], and [https://docs.microsoft.com/en-us/previous-versions/windows/xna/bb198097(v%3dxnagamestudio.40) mouse] button. SMAPI events use this to let you handle button presses without needing separate code for each. See [[Modding:Player Guide/Key Bindings]] for a list of values. | + | SMAPI 的 <samp>SButton</samp> 是一个涵盖了每种[https://docs.microsoft.com/en-us/previous-versions/windows/xna/bb975202(v%3dxnagamestudio.40) 控制器], [https://docs.microsoft.com/en-us/previous-versions/windows/xna/bb197781(v%3dxnagamestudio.40) 键盘] 和[https://docs.microsoft.com/en-us/previous-versions/windows/xna/bb198097(v%3dxnagamestudio.40) 鼠标] 的按键的常量。SMAPI 事件使用此常量,这允许你处理按键按下时不需要区分每种按键的代码。阅读 [[Modding:Player Guide/Key Bindings]] 来获取值列表。 |
| | | |
− | SMAPI provides extensions to convert any of the other constants to <samp>SButton</samp>: | + | SMAPI 提供了把任意其他常量转换为 <samp>SButton</samp> 的扩展: |
| <syntaxhighlight lang="c#"> | | <syntaxhighlight lang="c#"> |
| SButton key = Keys.A.ToSButton(); // SButton.A | | SButton key = Keys.A.ToSButton(); // SButton.A |
第107行: |
第107行: |
| </syntaxhighlight> | | </syntaxhighlight> |
| | | |
− | You can also convert <samp>SButton</samp> to the other constants. This uses a <samp>TryGet</samp> approach since <samp>SButton</samp> is a superset of the others (''e.g.,'' you can't convert <samp>SButton.ControllerA</samp> to a keyboard value):
| + | 你也可以将 <samp>SButton</samp> 转换为其他常量。这将使用到 <samp>TryGet</samp> 的途径,因为 <samp>SButton</samp> 是其他常量的一个超集(比方说,你不能把 <samp>SButton.ControllerA</samp> 转换成一个 keyboard 值): |
| <syntaxhighlight lang="c#"> | | <syntaxhighlight lang="c#"> |
| SButton value = SButton.A; | | SButton value = SButton.A; |
第118行: |
第118行: |
| </syntaxhighlight> | | </syntaxhighlight> |
| | | |
− | Two last extensions let you check how the button is mapped in the game:
| + | 最后两个扩展帮助你检查按键在游戏中如何布局: |
| <syntaxhighlight lang="c#"> | | <syntaxhighlight lang="c#"> |
| SButton button = SButton.MouseLeft; | | SButton button = SButton.MouseLeft; |
| if (button.IsUseToolButton()) | | if (button.IsUseToolButton()) |
− | // use tool | + | // 使用工具 |
| else if (button.IsActionButton()) | | else if (button.IsActionButton()) |
− | // perform action | + | // 执行动作 |
| </syntaxhighlight> | | </syntaxhighlight> |
| | | |
− | You can use <samp>SButton</samp> values directly in your [[../Config|config model]], but <samp>[[#KeybindList|KeybindList]]</samp> is recommended instead in most cases.
| + | 你可以在[[../Config|config model]] 中直接使用 <samp>SButton</samp> 的值,但是大部分情况下推荐使用 <samp>[[#KeybindList|KeybindList]]</samp> 。 |
| | | |
| ===KeybindList=== | | ===KeybindList=== |
第212行: |
第212行: |
| '''The pixel positions are ''not'' adjusted for [[Modding:Modder Guide/Game Fundamentals#UI scaling|UI scaling]]''' (''i.e.,'' they're non-UI mode). Whether you need UI or non-UI positions depends how you're using them, so you can use <samp>cursorPos.GetScaledAbsolutePixels()</samp> or <samp>cursorPos.GetScaledScreenPixels()</samp> to adjust them automatically for the current mode or <samp>Utility.ModifyCoordinatesForUIScale</samp> to always get UI mode coordinates. | | '''The pixel positions are ''not'' adjusted for [[Modding:Modder Guide/Game Fundamentals#UI scaling|UI scaling]]''' (''i.e.,'' they're non-UI mode). Whether you need UI or non-UI positions depends how you're using them, so you can use <samp>cursorPos.GetScaledAbsolutePixels()</samp> or <samp>cursorPos.GetScaledScreenPixels()</samp> to adjust them automatically for the current mode or <samp>Utility.ModifyCoordinatesForUIScale</samp> to always get UI mode coordinates. |
| | | |
− | ==See also== | + | == 另见== |
| * [[../Events#Input|Input events]] | | * [[../Events#Input|Input events]] |
− | * [[Modding:Player Guide/Key Bindings]] for a list of valid <samp>SButton</samp> values | + | * [[Modding:Player Guide/Key Bindings]] 中有一个可用的 <samp>SButton</samp> 值列表 |
| | | |
| [[en:Modding:Modder Guide/APIs/Input]] | | [[en:Modding:Modder Guide/APIs/Input]] |