# SInput

**成员** 24

|      |                                                         |                                                                                                        |
| ---- | ------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
| 函数 | [GetKeyDown](#getkeydown)                               | Returns true if 'key' was pressed during this frame.                                                   |
| 函数 | [GetKeyUp](#getkeyup)                                   | Returns true if 'key' is no longer pressed during this frame (but was in the last).                    |
| 函数 | [GetKey](#getkey)                                       | Returns true if 'key' is being held pressed.                                                           |
| 函数 | [GetMouseDown](#getmousedown)                           | Returns true if mouse 'button' was pressed during this frame.                                          |
| 函数 | [GetMouseUp](#getmouseup)                               | Returns true if mouse 'button' is no longer pressed during this frame (but was in the last).           |
| 函数 | [GetMouseHold](#getmousehold)                           | Returns true if mouse 'button' is being held pressed.                                                  |
| 函数 | [Vibrate](#vibrate)                                     | Vibrates the controller (or phone, or gamepad) if a rumble device is present.                          |
| 属性 | [ScrollWheel](#scrollwheel)                             | Returns a non-0 value if the mouse wheel is being scrolled, value usually ranges from -1 to 1\*\*.\*\* |
| 属性 | [MousePosition](#mouseposition)                         | Returns the current position of the mouse in screen pixels.                                            |
| 属性 | [MovementAxis](#movementaxis)                           | Returns left/right (A/D) movement on X, forward/back (W/S) on Y, and up/down (E/C) on Z.               |
| 属性 | [TurnAxis](#turnaxis)                                   | Returns the Turn Axis (-1 to 1 on Vector's X value).                                                   |
| 属性 | [MouseLook](#mouselook)                                 | Enable or disable Mouse Look feature.                                                                  |
| 属性 | [MouseX](#mousex)                                       | Returns a float representing the Mouse/Right Joystick horizontal movement.                             |
| 属性 | [MouseY](#mousey)                                       | Returns a float representing the Mouse/Right Joystick vertical movement.                               |
| 属性 | [ClickToWalk](#clicktowalk)                             | Returns the Turn Axis (-1 to 1 on Vector's X value).                                                   |
| 属性 | [Fire](#fire)                                           | Return true if primary fire is pressed (Mouselook Mode).                                               |
| 属性 | [AltFire](#altfire)                                     | Return true if alternative fire is pressed (Mouselook Mode)                                            |
| 属性 | [CursorInUI](#cursorinui)                               | Returns true if the cursor is over the Sinespace UI.                                                   |
| 属性 | [IsVRAvailable](#isvravailable)                         | Returns true if VR is available.                                                                       |
| 属性 | [IsVRActive](#isvractive)                               | Returns true if VR is active.                                                                          |
| 属性 | [VRLeftControllerPosition](#vrleftcontrollerposition)   | Returns position of Left VR Controller.                                                                |
| 属性 | [VRRightControllerPosition](#vrrightcontrollerposition) | Returns position of Right VR Controller.                                                               |
| 属性 | [VRLeftControllerRotation](#vrleftcontrollerrotation)   | Returns rotation of Left VR Controller.                                                                |
| 属性 | [VRRightControllerRotation](#vrrightcontrollerrotation) | Returns rotation of Right VR Controller.                                                               |

## 函数

### GetKeyDown

bool GetKeyDown(string key)

Returns true if 'key' was pressed during this frame.

参数

| 参数 | 类型   | 说明                                                                                                      |
| ---- | ------ | --------------------------------------------------------------------------------------------------------- |
| key  | string | [Input key name](/docs/zh-Hans/scripting/client-scripting-api-reference/client/sinput.md#input-key-names) |

返回值

`bool`

示例

**Lua**

```lua
thisGameObject = Space.Host.ExecutingObject

OnUpdate = function()
  if Space.Input.GetKeyDown("space") then
 Space.Log("Jump Key Down")
  end
end

thisGameObject.OnUpdate(OnUpdate) 

```

### GetKeyUp

bool GetKeyUp(string key)

Returns true if 'key' is no longer pressed during this frame (but was in the last).

参数

| 参数 | 类型   | 说明                                                                                                      |
| ---- | ------ | --------------------------------------------------------------------------------------------------------- |
| key  | string | [Input key name](/docs/zh-Hans/scripting/client-scripting-api-reference/client/sinput.md#input-key-names) |

返回值

`bool`

示例

**Lua**

```lua
thisGameObject = Space.Host.ExecutingObject

OnUpdate = function()
  if Space.Input.GetKeyUp("space") then
 Space.Log("Jump Key Released")
  end
end

thisGameObject.OnUpdate(OnUpdate)

```

### GetKey

bool GetKey(string key)

Returns true if 'key' is being held pressed.

参数

| 参数 | 类型 | 说明                                                                                                      |
| ---- | ---- | --------------------------------------------------------------------------------------------------------- |
| key  | bool | [Input key name](/docs/zh-Hans/scripting/client-scripting-api-reference/client/sinput.md#input-key-names) |

返回值

`bool`

示例

**Lua**

```lua
thisGameObject = Space.Host.ExecutingObject

OnUpdate = function()
  if Space.Input.GetKey("space") then
 Space.Log("Jump Key Held")
  end
end

thisGameObject.OnUpdate(OnUpdate)

```

### GetMouseDown

bool GetMouseDown(int button)

Returns true if mouse 'button' was pressed during this frame.

参数

| 参数   | 类型 | 说明                                                                                                                       |
| ------ | ---- | -------------------------------------------------------------------------------------------------------------------------- |
| button | int  | *button* values are 0 for the primary button (often the left button), 1 for secondary button, and 2 for the middle button. |

返回值

`bool`

示例

**Lua**

```lua
if Space.Input.GetMouseDown(1) then
  Space.Log("Right Mouse button pressed in this frame")
end

```

**Lua**

```lua
--this script will make this object jump to wherever you right click
thisGameObject = Space.Host.ExecutingObject

OnUpdate = function()
  if Space.Input.GetMouseDown(1) then
   clickRay = Space.Camera.ScreenCoordinatesToRay(Space.Input.MousePosition)
  rayCastHit = Space.Physics.RayCastSingle(clickRay.Origin, clickRay.Direction, 50.0)
  thisGameObject.WorldPosition = rayCastHit.Position
  end
end

thisGameObject.OnUpdate(OnUpdate)  

```

### GetMouseUp

bool GetMouseUp(int button)

Returns true if mouse 'button' is no longer pressed during this frame (but was in the last).

参数

| 参数   | 类型 | 说明                                                                                                                       |
| ------ | ---- | -------------------------------------------------------------------------------------------------------------------------- |
| button | int  | *button* values are 0 for the primary button (often the left button), 1 for secondary button, and 2 for the middle button. |

返回值

`bool`

示例

**Lua**

```lua
if Space.Input.GetMouseUp(1) then
  Space.Log("Right Mouse button was just unpressed")
end

```

### GetMouseHold

bool GetMouseHold(int button)

Returns true if mouse 'button' is being held pressed.

参数

| 参数   | 类型 | 说明                                                                                                                       |
| ------ | ---- | -------------------------------------------------------------------------------------------------------------------------- |
| button | int  | *button* values are 0 for the primary button (often the left button), 1 for secondary button, and 2 for the middle button. |

返回值

`bool`

示例

**Lua**

```lua
if Space.Input.GetMouseHold(1) then
  Space.Log("Right Mouse button being held")
end

```

**Lua**

```lua
--this script will make this object will follow your ovement while holding right click

thisGameObject = Space.Host.ExecutingObject


OnUpdate = function()
  if Space.Input.GetMouseHold(1) then
   clickRay = Space.Camera.ScreenCoordinatesToRay(Space.Input.MousePosition)
  rayCastHit = Space.Physics.RayCastSingle(clickRay.Origin, clickRay.Direction, 50.0)
  thisGameObject.WorldPosition = rayCastHit.Position
  end
end

thisGameObject.OnUpdate(OnUpdate)  

```

### Vibrate

void Vibrate(float intensity, float duration, bool leftHand)

Vibrates the controller (or phone, or gamepad) if a rumble device is present.

参数

| 参数      | 类型  | 说明                                                                                             |
| --------- | ----- | ------------------------------------------------------------------------------------------------ |
| intensity | float | Intensity of the vibration from 0.0 to 1.0                                                       |
| duration  | float | Duration of the vibration. Maximum 1.0 second.                                                   |
| leftHand  | bool  | If true the vibration will be on the left hand and if false on the right hand (where applicable) |

返回值

`void`

示例

**Lua**

```lua
Space.Input.Vibrate(1,1,false)

```

## 属性

### ScrollWheel

float ScrollWheel {get; }

Returns a non-0 value if the mouse wheel is being scrolled, value usually ranges from -1 to 1\*\*.\*\*

示例

**Lua**

```lua
scrollWheel = Space.Input.ScrollWheel

```

**Lua**

```lua
--Object will move up and down with scroll wheel movement
thisGameObject = Space.Host.ExecutingObject

OnUpdate = function()
  thisGameObject.WorldPosition = thisGameObject.WorldPosition + (thisGameObject.Up * Space.Input.ScrollWheel)
end

thisGameObject.OnUpdate(OnUpdate) 

```

### MousePosition

[SVector](/docs/zh-Hans/scripting/client-scripting-api-reference/types/svector.md) MousePosition {get; }

Returns the current position of the mouse in screen pixels. If on a touch screen device, this will also return the location of the first finger being pressed.

示例

**Lua**

```lua
currentMousePos = Space.Input.MousePosition

```

**Lua**

```lua
--Object will follow your mouse movement while holding right click
thisGameObject = Space.Host.ExecutingObject

OnUpdate = function()
  if Space.Input.GetMouseHold(1) then
    clickRay = Space.Camera.ScreenCoordinatesToRay(Space.Input.MousePosition)
    rayCastHit = Space.Physics.RayCastSingle(clickRay.Origin, clickRay.Direction, 50.0)
    thisGameObject.WorldPosition = rayCastHit.Position
  end
end

thisGameObject.OnUpdate(OnUpdate)   

```

### MovementAxis

[SVector](/docs/zh-Hans/scripting/client-scripting-api-reference/types/svector.md) MovementAxis {get; }

Returns left/right (A/D) movement on X, forward/back (W/S) on Y, and up/down (E/C) on Z.

示例

**Lua**

```lua
moveAxis  = Space.Input.MovementAxis

```

**Lua**

```lua
--Object moves according to your AWSD presses

thisGameObject = Space.Host.ExecutingObject

OnUpdate = function()
  if Space.Input.MovementAxis.Y > 0 then
    thisGameObject.WorldPosition = thisGameObject.WorldPosition + thisGameObject.Forward
  elseif Space.Input.MovementAxis.Y < 0 then  
    thisGameObject.WorldPosition = thisGameObject.WorldPosition - thisGameObject.Forward 
  end

  if Space.Input.MovementAxis.X > 0 then
    thisGameObject.WorldPosition = thisGameObject.WorldPosition + thisGameObject.Right
  elseif Space.Input.MovementAxis.X < 0 then  
    thisGameObject.WorldPosition = thisGameObject.WorldPosition - thisGameObject.Right
  end
end

thisGameObject.OnUpdate(OnUpdate) 

```

### TurnAxis

[SVector](/docs/zh-Hans/scripting/client-scripting-api-reference/types/svector.md)? TurnAxis {get; }

Returns the Turn Axis (-1 to 1 on Vector's X value).

示例

**Lua**

```lua
turnAxis = Space.Input.TurnAxis

```

**Lua**

```lua
--makes this object color red if player is turning left
--and color green if player is turning right
--and blue if player is not turning
thisGameObject = Space.Host.ExecutingObject

OnUpdate = function()
  if Space.Input.TurnAxis.X > 0 then
    thisGameObject.Renderer.Material.SetColor("_Color",Color.Red)
  elseif Space.Input.TurnAxis.X < 0 then
    thisGameObject.Renderer.Material.SetColor("_Color",Color.Green)
  elseif Space.Input.TurnAxis.X == 0 then
    thisGameObject.Renderer.Material.SetColor("_Color",Color.Blue)
  end
end

thisGameObject.OnUpdate(OnUpdate)

```

***

### MouseLook

bool MouseLook {get; set; }

Enable or disable Mouse Look feature.

示例

**Lua**

```lua
Space.Input.MouseLook = true

```

**Lua**

```lua
--the below script will put your player into MouseLook mode if you are near the object 
--and return you to normal mode if you are far from the object 

thisGameObject = Space.Host.ExecutingObject

function OnUpdate()
  positionAvatar = Space.Scene.PlayerAvatar.GameObject.WorldPosition
  positionObject = thisGameObject.WorldPosition

  if positionAvatar.InRange(positionObject, 5.0) then
    if Space.Input.MouseLook == false then
      Space.Input.MouseLook = true
    end
  else
    if Space.Input.MouseLook == true then
      Space.Input.MouseLook = false
    end
  end

end  

thisGameObject.OnUpdate(OnUpdate)

```

### MouseX

float MouseX {get; }

Returns a float representing the Mouse/Right Joystick horizontal movement. If the value is positive the movement is upwards. If the value is negative the movement is downwards. The value is derived by multiplying mouse delta with axis sensitivity. This is frame-rate independent; you do not need to be concerned about varying frame-rates when using this value.

示例

**Lua**

```lua
MouseX = Space.Input.MouseX

```

**Lua**

```lua
--the below script will rotate this object along the Y axis 
--according to your mouse/right joystick horizontal movement

thisGameObject = Space.Host.ExecutingObject

function OnUpdate()
  currentY = thisGameObject.WorldRotation.EulerAngles.Y 
  newY = currentY + Space.Input.MouseX
  thisGameObject.WorldRotation = Quaternion.Euler(0, newY, 0)
end

thisGameObject.OnUpdate(OnUpdate) 

```

### MouseY

float MouseY {get; }

Returns a float representing the Mouse/Right Joystick vertical movement. If the value is positive the movement is to the right. If the value is negative the movement is to the left. The value is derived by multiplying mouse delta with axis sensitivity. This is frame-rate independent; you do not need to be concerned about varying frame-rates when using this value.

示例

**Lua**

```lua
MouseY = Space.Input.MouseY

```

**Lua**

```lua
--the below script will rotate this object along the Y axis 
--according to your mouse/right joystick vertical movement

thisGameObject = Space.Host.ExecutingObject

function OnUpdate()
  currentX = thisGameObject.WorldRotation.EulerAngles.X 
  newX = currentX + Space.Input.MouseY
  thisGameObject.WorldRotation = Quaternion.Euler(newX , 0, 0)
end

thisGameObject.OnUpdate(OnUpdate) 

```

### ClickToWalk

[SVector](/docs/zh-Hans/scripting/client-scripting-api-reference/types/svector.md)? TurnAxis {get; }

Returns the Turn Axis (-1 to 1 on Vector's X value).

示例

**Lua**

```lua
turnAxis = Space.Input.TurnAxis

```

**Lua**

```lua
--makes this object color red if player is turning left

```

### Fire

bool Fire {get; }

Return true if primary fire is pressed (Mouselook Mode).

示例

**Lua**

```lua
fireIsPressed = Space.Input.Fire

```

**Lua**

```lua
--Create a GameObject from resource when Primary Fire button is pressed
--[Add resource "bullet" as a resource in scripting runtime component]

thisGameObject = Space.Host.ExecutingObject
bullet = Space.GetResource("bullet") 

OnUpdate = function()
  if Space.Input.Fire then
  Space.Scene.CreateGameObject(bullet) 
  end
end


thisGameObject.OnUpdate(OnUpdate)

```

### AltFire

bool AltFire {get; }

Return true if alternative fire is pressed (Mouselook Mode)

示例

**Lua**

```lua
altFireIsPressed = Space.Input.AltFire

```

**Lua**

```lua
--Create a GameObject from resource when Alternate Fire button is pressed
--[Add resource "bullet" as a resource in scripting runtime component]

thisGameObject = Space.Host.ExecutingObject
bullet = Space.GetResource("bullet") 

OnUpdate = function()
  if Space.Input.AltFire then
    Space.Scene.CreateGameObject(bullet) 
  end
end

thisGameObject.OnUpdate(OnUpdate)

```

### CursorInUI

bool CursorInUI {get; }

Returns true if the cursor is over the Sinespace UI.

示例

**Lua**

```lua
cursorInUI = Space.Input.CursorInUI

```

**Lua**

```lua
--this object is going to listen to your scroll wheel movement and move accordingly
--UNLESS your cursor is on the sinespace UI
--(Example: Scrolling chat won't also move the object)

thisGameObject = Space.Host.ExecutingObject

OnUpdate = function()
  if Space.Input.CursorInUI == false then
    thisGameObject.WorldPosition = thisGameObject.WorldPosition + (thisGameObject.Up * Space.Input.ScrollWheel)
  end
end

thisGameObject.OnUpdate(OnUpdate)  

```

### IsVRAvailable

bool IsVRAvailable {get; }

Returns true if VR is available.

示例

**Lua**

```lua
isAvailable = Space.Input.IsVRAvailable

```

### IsVRActive

bool IsVRActive {get; }

Returns true if VR is active.

示例

**Lua**

```lua
turnAxis = Space.Input.TurnAxis

```

### VRLeftControllerPosition

[SVector](/docs/zh-Hans/scripting/client-scripting-api-reference/types/svector.md)? VRLeftControllerPosition {get; }

Returns position of Left VR Controller.

示例

**Lua**

```lua
posVRleft = Space.Input.VRLeftControllerPosition

```

### VRRightControllerPosition

[SVector](/docs/zh-Hans/scripting/client-scripting-api-reference/types/svector.md)? VRRightControllerPosition {get; }

Returns position of Right VR Controller.

示例

**Lua**

```lua
posVRright = Space.Input.VRRightControllerPosition

```

### VRLeftControllerRotation

[SQuaternion](/docs/zh-Hans/scripting/client-scripting-api-reference/types/squaternion.md)? VRLeftControllerRotation {get; }

Returns rotation of Left VR Controller.

示例

**Lua**

```lua
rotVRleft = Space.Input.VRLeftControllerRotation

```

### VRRightControllerRotation

[SQuaternion](/docs/zh-Hans/scripting/client-scripting-api-reference/types/squaternion.md)? VRRightControllerRotation {get; }

Returns rotation of Right VR Controller.

示例

**Lua**

```lua
rotVRright = Space.Input.VRRightControllerRotation

```

## Input Key Names

Key names follow these naming conventions:

| **Key family** | **Naming convention**                                                                                               |
| -------------- | ------------------------------------------------------------------------------------------------------------------- |
| Letter keys    | `a`, `b`, `c`…                                                                                                      |
| Number keys    | `1`, `2`, `3`…                                                                                                      |
| Arrow keys     | `up`, `down`, `left`, `right`                                                                                       |
| Numpad keys    | `[1]`, `[2]`, `[3]`, `[+]`, `[equals]`…                                                                             |
| Modifier keys  | `right shift`, `left shift`, `right ctrl`, `left ctrl`, `right alt`, `left alt`, `right cmd`, `left cmd`            |
| Special keys   | `backspace`, `tab`, `return`, `escape`, `space`, `delete`, `enter`, `insert`, `home`, `end`, `page up`, `page down` |
| Function keys  | `f1`, `f2`, `f3`…                                                                                                   |

Mouse buttons are named `mouse 0, mouse 1, mouse 2,` and so on.

Joystick buttons follow these naming conventions:

| **Button origin**                        | **Naming convention**                                                |
| ---------------------------------------- | -------------------------------------------------------------------- |
| A specific button on any joystick        | `joystick button 0`, `joystick button 1`, `joystick button 2`…       |
| A specific button on a specific joystick | `joystick 1 button 0`, `joystick 1 button 1`, `joystick 2 button 0`… |
