Unity Input input class - mouse and keyboard detection

Interacting with user input is very important when you develop Unity games. Unity provides a wealth of input classes to help you get user input and respond accordingly. In this article, I will introduce you to the input classes in Unity, including Input, KeyCode, MouseButton, and Touch.

Input class
The Input class is the most basic input class in Unity, which can obtain input from various types of input devices, such as keyboards, mice, handles, and gravity sensors. When getting input, we can use some methods of the Input class to get the input status of the input device

Get keyboard keys

The GetKey, GetKeyDown, and GetKeyUp methods are used to obtain the input status of the keyboard keys. These methods accept a parameter of the KeyCode enumeration type, which is used to specify the input state of which key to obtain. For example, the following code demonstrates how to detect the user's operation of the A key on the keyboard: 

 

get mouse button

GetMouseButton, GetMouseButtonDown, GetMouseButtonUp methods are used to obtain the input state of the mouse button. Among them, 0, 1, and 2 represent the left, right, and middle buttons of the mouse, respectively. The following code demonstrates how to detect whether the user has pressed the left mouse button:

 

Guess you like

Origin blog.csdn.net/qq_36592993/article/details/130563921