InputService

Provides functions related to user input.

# Methods

[{"index":"void CursorLockMode(CursorLockMode lockMode) (ClientOnly)","0":"Controls the mouse cursor control method. Only Windows PC environment is supported."}]

[{"index":"CursorLockMode GetCursorLockMode() (ClientOnly)","0":"Returns the mouse cursor control method. Only Windows PC environment is supported."}]

[{"index":"Vector2 GetCursorPosition() (ClientOnly)","0":"Gets the current position of the cursor. Returns the centroid if two or more locations are touched simultaneously in a mobile environment."}]

[{"index":"boolean IsAnyKeyPressed() (ClientOnly)","0":"Returns true regardless of which key is pressed currently."}]

[{"index":"boolean IsKeyPressed(KeyboardKey key) (ClientOnly)","0":"Returns true if key is currently pressed."}]

[{"index":"boolean IsPointerOverUI() (ClientOnly)","0":"Gets whether the cursor is on the UI. The UI wherein RaycastTarget is false is ignored."}]

[{"index":"boolean IsPointerOverUI(int32 pointerId) (ClientOnly)","0":"Returns whether the cursor corresponding to pointerId is on the UI. Ignores UI Entity if its RaycastTarget property value is false.Mouse cursor's pointerId is 1. In case of mobile devices, each touch input will be designated in order, starting from 1 (e.g. 1, 2, 3...)."}]

[{"index":"void ResetCursor() (ClientOnly)","0":"Reset the cursor to the default appearance."}]

[{"index":"void SetCursor(string spriteRUID, Vector2 offset) (ClientOnly)","0":"Change the cursor's appearance. spriteRUID is the RUID of the sprite or the animation clip. offset is the value that indicates the cursor's relative position in measures of pixels, and if offset is (0, 0), the sprite's Pivot will be the cursor's position. The cursor's shape will be reset as it moves around the room."}]

[{"index":"void SetCursorVisible(boolean visible) (ClientOnly)","0":"Sets whether to display the mouse cursor."}]

##### inherited from Service:

[{"index":"EventHandlerBase ConnectEvent(string key, IScriptFunction eventHandler)","0":"Links events. When a key-type event occurs in this service, a handler is notified. The handler receives event parameters."}]

[{"index":"EventHandlerBase ConnectEvent(Type eventType, IScriptFunction eventHandler)","0":"Links events. This service notifies a handler when an eventType event occurs. The handler receives event parameters."}]

[{"index":"boolean DisconnectEvent(string key, EventHandlerBase eventHandler)","0":"Disconnects the event with the key-type event and the handler."}]

[{"index":"boolean DisconnectEvent(Type eventType, EventHandlerBase eventHandler)","0":"Disconnects the eventType event from its handler."}]

[{"index":"void SendEvent(EventType sendEvent)","0":"Generates an event."}]

# Events

[{"index":"ExitPopupClosedEvent","0":"This event occurs when closing the exit pop-up."}]

[{"index":"ExitPopupOpenedEvent","0":"This event occurs when opening the exit pop-up."}]

[{"index":"KeyDownEvent","0":"Occurs once upon pressing the Key."}]

[{"index":"KeyHoldEvent","0":"Occurs while pressing the Key. Does not occur with a short press."}]

[{"index":"KeyReleaseEvent","0":"Occurs once upon pressing and releasing the Key. Does not occur with a short press."}]

[{"index":"KeyUpEvent","0":"Occurs once upon pressing and releasing the Key."}]

[{"index":"MenuPopupClosedEvent","0":"This event occurs upon closing the menu pop-up."}]

[{"index":"MenuPopupOpenedEvent","0":"This event occurs upon opening the menu pop-up."}]

[{"index":"MouseMoveEvent","0":"This event occurs from InputService when the mouse moves."}]

[{"index":"MouseScrollEvent","0":"Occurs in InputService with mouse scroll up or down."}]

[{"index":"PinchInOutEvent","0":"Occurs in InputService when you widen or narrow the gap while touching two places on the screen."}]

[{"index":"ScreenTouchEvent","0":"This event occurs when touching the screen or clicking."}]

[{"index":"ScreenTouchHoldEvent","0":"This event occurs while touching the screen or clicking."}]

[{"index":"ScreenTouchReleaseEvent","0":"This event occurs while touching\/holding the screen or clicking\/holding."}]

# Examples

#### Enter Key

This is an example of creating a Component that makes the player grow in size and jump according to how long the Z key is pressed before it is released. The created component must be added to DefaultPlayer to make it work properly.

First, write `KeyDownEvent` as follows to change to a crouching position when the Z key is pressed. To calculate the pressed time, create a property called KeyDownTime and record the current time.
    
    
    @Component
    script CrouchInputHandler extends Component
        property number KeyDownTime = 0
    
        @EventSender("Service", "InputService")
        handler HandleKeyDownEvent(KeyDownEvent event)
        --------------- Native Event Sender Info ----------------
        -- Sender: InputService
        -- Space: Client
        ---------------------------------------------------------
    
        -- Parameters
        local key = event.key
        ---------------------------------------------------------
        if key == KeyboardKey.Z then
        	self.Entity.StateComponent:ChangeState("CROUCH")
        	self.KeyDownTime = _UtilLogic.ElapsedSeconds
        end
        end
    end
    

Write `KeyUpEvent` as follows to jump upon releasing the Z key. The player jumps with a force equal to 10 times the duration of the key press.
    
    
    @Component
    script ChargeJumpHandler extends Component
        @EventSender("Service", "InputService")
        handler HandleKeyUpEvent(KeyUpEvent event)
        --------------- Native Event Sender Info ----------------
        -- Sender: InputService
        -- Space: Client
        ---------------------------------------------------------
    
        -- Parameters
        local key = event.key
        ---------------------------------------------------------
        if key == KeyboardKey.Z then
        local force = (_UtilLogic.ElapsedSeconds - self.KeyDownTime) * 10
        	self.Entity.RigidbodyComponent:SetForce(Vector2(0, force))
        	self.Entity.StateComponent:ChangeState("JUMP")
        end
        end
    end
    

To visually represent the duration of the key press, the player's size will increase proportionally to the press time. Write the `KeyHoldEvent` as follows.
    
    
    @Component
    script ZKeyScaler extends Component
        @EventSender("Service", "InputService")
        handler HandleKeyHoldEvent(KeyHoldEvent event)
        --------------- Native Event Sender Info ----------------
        -- Sender: InputService
        -- Space: Client
        ---------------------------------------------------------
    
        -- Parameters
        local key = event.key
        ---------------------------------------------------------
        if key == KeyboardKey.Z then
        	local keyHoldTime = _UtilLogic.ElapsedSeconds - self.KeyDownTime
        	local scale = 1 + keyHoldTime
        	self.Entity.TransformComponent.Scale = Vector3.one * scale
    
        end
        end
    end
    

To prevent other actions while maintaining the increased size, the `KeyReleaseEvent` is implemented as follows. If `KeyHoldEvent` is not called due to a very short press, the `KeyReleaseEvent` will also not be called.
    
    
    @Component
    script ZKeyScaleResetter extends Component
        @EventSender("Service", "InputService")
        handler HandleKeyReleaseEvent(KeyReleaseEvent event)
        --------------- Native Event Sender Info ----------------
        -- Sender: InputService
        -- Space: Client
        ---------------------------------------------------------
    
        -- Parameters
         local key = event.key
        ---------------------------------------------------------
        if key == KeyboardKey.Z then
        	self.Entity.TransformComponent.Scale = Vector3.one
        end
        end
    end
    

#### Touch Input

This is an example that displays particles at the touched position.
    
    
    @Component
    script MultiTouchIndicator extends Component
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
        local root = _EntityService:GetEntityByPath(EntityPath)
        for i = 1, #root.Children do
        	root.Children[i].Enable = false
        end
        end
    
        @EventSender("Service", "InputService")
        handler HandleScreenTouchEvent(ScreenTouchEvent event)
        --------------- Native Event Sender Info ----------------
        -- Sender: InputService
        -- Space: Client
        ---------------------------------------------------------
    
        -- Parameters
        local TouchId = event.TouchId
        local TouchPoint = event.TouchPoint
        ---------------------------------------------------------
    
        if TouchId < 1 then
        	return
        end
    
        local root = _EntityService:GetEntityByPath(EntityPath)
        root.Children[TouchId].Enable = true
        root.Children[TouchId].BasicParticleComponent.Color = Color.green
        root.Children[TouchId].TransformComponent.WorldPosition = _UILogic:ScreenToWorldPosition(TouchPoint):ToVector3()
        end
    
        @EventSender("Service", "InputService")
        handler HandleScreenTouchReleaseEvent(ScreenTouchReleaseEvent event)
        --------------- Native Event Sender Info ----------------
        -- Sender: InputService
        -- Space: Client
        ---------------------------------------------------------
    
        -- Parameters
        local TouchId = event.TouchId
        local TouchPoint = event.TouchPoint
        ---------------------------------------------------------
        if TouchId < 1 then
        	return
        end
    
        local root = _EntityService:GetEntityByPath(EntityPath)
        root.Children[TouchId].Enable = false
        root.Children[TouchId].BasicParticleComponent.Color = Color.white
        end
    end
    

#### Change Cursor

This is an example that changes the cursor shape depending on the input state of the left mouse button.
    
    
    @Component
    script ClickCursorHandler extends Component
        property string CursorRUID_Normal = "3930c5d2e85b4bff8467aada64fda7f5"
        property string CursorRUID_Click = "79819392249746f88952f3b2321f2f17"
        property boolean IsClicking = nil
    
        @ExecSpace("ClientOnly")
        method void OnUpdate(number delta)
        local click = _InputService:IsKeyPressed(KeyboardKey.Mouse0)
    
        if self.IsClicking == click then
        	return
        end
    
        if click == true then
        	_InputService:SetCursor(self.CursorRUID_Click, Vector2.zero)
        else
        	_InputService:SetCursor(self.CursorRUID_Normal, Vector2.zero)
        end
    
        self.IsClicking = click
        end
    end
    

# SeeAlso

  * BasicParticleComponent
  * RigidbodyComponent
  * StateComponent
  * TransformComponent
  * KeyboardKey
  * KeyDownEvent
  * KeyHoldEvent
  * KeyReleaseEvent
  * KeyUpEvent
  * ScreenTouchEvent
  * ScreenTouchReleaseEvent
  * UILogic
  * UtilLogic
  * Vector3
  * EntityService
  * [Input and Action Using InputService](/docs?postId=209)