Keyboard & Mouse Input
Keyboard Methods
These methods allow you to read the state of the keyboard keys during the simulation.
bool Input.GetKeyDown(KeyCode.KeyName);
- Description: Returns
trueduring the frame the user starts pressing down the key identified byKeyName. - Example:
if(Input.GetKeyDown(KeyCode.F5))
{
// Action when F5 is pressed
}
bool Input.GetKeyUp(KeyCode.KeyName);
- Description: Returns
trueduring the frame the user releases the key identified byKeyName. - Example:
if(Input.GetKeyUp(KeyCode.O))
{
// Action when 'O' is released
}
bool Input.GetKey(KeyCode.KeyName);
- Description: Returns
truewhile the user holds down the key identified byKeyName. - Example:
if(Input.GetKey(KeyCode.P))
{
// Action while 'P' is held down
}
Mouse Methods
These methods allow you to read mouse button states and wheel/axis movements.
Note
For mouse button methods, use the following mapping:
0: Left Click1: Right Click2: Middle Click (Wheel)
bool Input.GetMouseButton(int button);
- Description: Returns whether the given mouse button is held down.
- Example:
if (Input.GetMouseButton(0))
string txt = "Holding left click.";
bool Input.GetMouseButtonDown(int button);
- Description: Returns
trueduring the frame the user pressed the given mouse button. - Example:
if (Input.GetMouseButtonDown(0))
string txt = "Pressed left click.";
if (Input.GetMouseButtonDown(1))
string txt = "Pressed right click.";
if (Input.GetMouseButtonDown(2))
string txt = "Pressed middle click.";
bool Input.GetMouseButtonUp(int button);
- Description: Returns
trueduring the frame the user releases the given mouse button. - Example:
if (Input.GetMouseButtonUp(0))
string txt = "Released left click.";
float Input.GetAxis(string axisName);
- Description: Returns the value of the virtual axis identified by
axisName. - Supported Axis Names:
"Mouse X","Mouse Y", and"Mouse ScrollWheel". - Example:
float h = Input.GetAxis("Mouse X");
float v = Input.GetAxis("Mouse Y");
float mouseWheel = Input.GetAxis("Mouse ScrollWheel");