Simple Use of unity Keyboard Events and Mouse Events

Keyboard events

Press the event: Input.GetKeyDown()
For example:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class shsyhs : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            Debug.Log("You press“ W"");
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            Debug.Log("You press“ S"");
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            Debug.Log("You press“ D"");
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            Debug.Log("You press“ A"");
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("You press "blank"");
        }
    }
}


Uplift event: Input.GetKeyUp() (usage and press time are similar, omitted)
Long press event: Input.GetKey()
The following is an example of detecting the number of consecutive blank press:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class shsyhs : MonoBehaviour
{
    int count = 0;
    void Update()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            count++;
        }
        if (Input.GetKeyUp(KeyCode.Space))
        {
            Debug.Log("You press continuously."+count+"Sub space");
            count = 0;
        }
    }
}

Result:

Arbitrary key events
Input.anyKeyDown
Input.anyKey

Mouse event

press events
Input.GetMouseButtonDown() method: To detect which mouse button is pressed, the return parameters are 0 (left key), 1 (build), 2 (right key).
Input. mousePosition (get the current coordinates of the mouse)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class shsyhs : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("You press the left mouse button, coordinates are"+Input.mousePosition);
        }
        if (Input.GetMouseButtonDown(1))
        {
            Debug.Log("You press the middle mouse button, coordinates are" + Input.mousePosition);
        }
        if (Input.GetMouseButtonDown(2))
        {
            Debug.Log("You press the right mouse button. The coordinates are" + Input.mousePosition);
        }
    }
}


Uplift event: Input.GetMouseButtonUp()
To supplement the above code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class shsyhs : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("You press the left mouse button, coordinates are"+Input.mousePosition);
        }
        if (Input.GetMouseButtonDown(1))
        {
            Debug.Log("You press the middle mouse button, coordinates are" + Input.mousePosition);
        }
        if (Input.GetMouseButtonDown(2))
        {
            Debug.Log("You press the right mouse button. The coordinates are" + Input.mousePosition);
        }
        if (Input.GetMouseButtonUp(0))
        {
            Debug.Log("You raise the left mouse button with coordinates of ____________" + Input.mousePosition);
        }
        if (Input.GetMouseButtonUp(1))
        {
            Debug.Log("You raise the middle mouse button with coordinates of ____________" + Input.mousePosition);
        }
        if (Input.GetMouseButtonUp(2))
        {
            Debug.Log("You raise the right mouse button, coordinates are" + Input.mousePosition);
        }
    }
}

Operation results:

Note: The above code reverses the middle and right keys. 1 is the right key and 2 is the middle key. I'm sorry!! Just change it yourself.

Long press event: Input.GetMouseButton()
Use method and keyboard long press events are basically the same, slightly.

Posted on Wed, 09 Oct 2019 20:30:20 -0700 by Centrek