Unity键盘鼠标监听事件

本文介绍了一个Unity脚本,用于检测用户的键盘和鼠标输入。通过使用Input.GetKeyDown、Input.GetKey、Input.GetKeyUp等方法来响应键盘按键的按下、按住和释放状态,以及使用Input.GetMouseButtonDown、Input.GetMouseButton、Input.GetMouseButtonUp来检测鼠标按钮的状态变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

public class C_3_8_1 : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        // 按下键盘A键
        if (Input.GetKeyDown(KeyCode.A)){
            Debug.Log ("你按下了A");
        }
        // 按住键盘A键
        if (Input.GetKey(KeyCode.A)) {
            Debug.Log ("你按住了A");
        }
        // 抬起键盘A键
        if (Input.GetKeyUp(KeyCode.A)) {
            Debug.Log ("你抬起了A");
        }

        // 按下键盘左Shift键
        if (Input.GetKeyDown(KeyCode.LeftShift)) {
            Debug.Log ("你按下了左Shift");
        }
        // 按住键盘左Shift键
        if (Input.GetKey(KeyCode.LeftShift)) {
            Debug.Log ("你按住了左Shift");
        }
        // 按下键盘左Shift键
        if (Input.GetKeyUp(KeyCode.LeftShift)) {
            Debug.Log ("你抬起了左Shift");
        }

        // 按下鼠标左键
        if (Input.GetMouseButtonDown(0)) {
            Debug.Log ("你按下了鼠标左键");
        }
        // 按住鼠标左键
        if (Input.GetMouseButton(0)) {
            Debug.Log ("你按住了鼠标左键");
        }
        // 抬起鼠标左键
        if (Input.GetMouseButtonUp(0)) {
            Debug.Log ("你抬起了鼠标左键");
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值