Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


GetKey

Returns true while the user holds down the key identified by the key KeyCode enum parameter.
Read time 1 minuteLast updated 5 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.InputLegacyModule

GetKey(KeyCode)

Returns true while the user holds down the key identified by the
key
KeyCode enum parameter.
public static bool GetKey(KeyCode key)

Parameters

Returns

Type

Description

bool

Examples

using UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour{ void Update() { if (Input.GetKey(KeyCode.UpArrow)) { print("up arrow key is held down"); } if (Input.GetKey(KeyCode.DownArrow)) { print("down arrow key is held down"); } }}

GetKey(string)

Returns true while the user holds down the key identified by
name
.
public static bool GetKey(string name)

Parameters

name

Returns

Type

Description

bool

Remarks

Note: This API is part of the legacy Input Manager. The recommended best practice is that you don't use this API in new projects. For new projects, use the Input System package. To learn more about input, refer to Input.
Input.GetKey will report the status of the named key. This might be used to confirm a key is used for auto fire. For the list of key identifiers see Input Manager. When dealing with input it is recommended to use Input.GetAxis and Input.GetButton instead since it allows end-users to configure the keys.
iOS, tvOS: Due platform limitations, Input.GetKeyUp event for keyboard events is delayed by about half a second, see UnityView+Keyboard.mm in the generated Xcode project for more information.

Examples

using UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour{ void Update() { if (Input.GetKey("up")) { print("up arrow key is held down"); } if (Input.GetKey("down")) { print("down arrow key is held down"); } }}