UnityEngine.Input

键盘/鼠标事件

检测指定按键

if (Input.GetKeyDown(KeyCode.A))                //键盘按下
    Debug.Log("按下" + KeyCode.A + "键");
if (Input.GetKey(KeyCode.W))                    //键盘按住
    Debug.Log("按住" + KeyCode.W + "键");
if (Input.GetKeyUp(KeyCode.D))                  //键盘抬起
    Debug.Log("抬起" + KeyCode.D + "键");

if (Input.GetMouseButtonDown(0))                //鼠标按下
    Debug.Log("按下鼠标左键");
if (Input.GetMouseButton(1))                    //鼠标按住
    Debug.Log("按住鼠标右键");
if (Input.GetMouseButtonUp(2))                  //鼠标抬起
    Debug.Log("抬起鼠标滚轮");

if(Input.GetButtonDown("Fire1"))                //虚拟按钮按下
    Debug.Log("按下射击键");
if (Input.GetButton("Jump"))                    //虚拟按钮按住
    Debug.Log("按住跳跃键");
if (Input.GetButtonUp("Fire1"))                 //虚拟按钮抬起
    Debug.Log("抬起射击键");

//根据坐标轴名称返回虚拟坐标系中的值
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
float mouseW = Input.GetAxis("Mouse ScrollWheel");

Input的设置

检测任意按键

方法一:

if (Input.anyKeyDown)                           //任意键被按下(判断鼠标被按下)
    Debug.Log("任意键被按下");
if (Input.anyKey)                               //任意键被按住
    Debug.Log("任意键被按住");

方法二:

private void OnGUI()
{
    Event e = Event.current;
    if (e.isKey)
        Debug.Log("键盘" + e.keyCode + " " + e.type);
    else if (e.isMouse)
        Debug.Log("鼠标" + e.keyCode + " " + e.type);
}

上述代码只能放在OnGUI函数内,不然会报错。
NullReferenceException: Object reference not set to an instance of an object
isKey/isMouse除了在KeyDown和KeyUp触发2次之外,还会触发一次按住(keyCode为None,没有识别具体按键),因为按键按住不动时KeyDown也在同时不停的触发已经识别了keyCode,所以会有三次触发输出。
OGUI内才能执行
键盘
LeftShift/RightShift 不触发
SysReq(PrintScreenSysRq) 只在KeyUp时触发
鼠标
触发2次,keyCode都为None,无法识别具体按键

检测组合按键

if (Event.current.Equals(Event.KeyboardEvent("&F12")))
    Debug.Log("Alt + F12");
if (Event.current.Equals(Event.KeyboardEvent("Keypad4")))
    Debug.Log("Keypad4");
if (Event.current.Equals(Event.KeyboardEvent("[5]")))
    Debug.Log("[5]");

key的字符串是键的名字(同输入管理器一样),选择任意组合键数量的前缀:
& = Alternate,^ = Control,% = Command/Windows key, # = Shift
例如:”&F12” = Alt + F12,”^[0]” = Ctrl + 数字键0,”Keypad0”和”[0]”意义相同

检测按键鼠标双击

方法一:(Event事件–鼠标)

private bool isInObj = false;
private void OnMouseEnter() { isInObj = true; }
private void OnMouseExit() { isInObj = false; }
private void DoubleClickFun1()
{
    Event e = Event.current;
    if(e.isMouse)
    {
        if (e.type == EventType.MouseDown && e.clickCount == 2)
        {
            if(isInObj)
                Debug.Log("鼠标双击 " + transform.name);
            else
                Debug.Log("鼠标双击 屏幕");
        }
    }
}

发现e.clickCount不会大于2

方法二:(计算按键时间差)

private float time1;
private float time2;
private void DoubleClickFun2()
{
    if (Input.GetMouseButtonDown(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            if (hit.transform.name == transform.name)
            {
                time2 = Time.time;
                if (time2 - time1 < 0.2)
                    Debug.Log("鼠标双击 " + transform.name);
                time1 = time2;
                return;
            }
        }
        time2 = Time.time;
        if (time2 - time1 < 0.2)
            Debug.Log("鼠标双击 屏幕");
        time1 = time2;
    }

上面的time1,time2可以定义成System.DateTime,time2 = System.DateTime.Now,
用time2- time1 < new TimeSpan(0, 0, 0, 0, 200)比较。
移动端GetMouseButtonDown()也有效。

方法三:(协程检测)

private void Update()
{
    if (!isDoubleTap)
        StartCoroutine(DoubleTap(0.2f, 1));
}

private bool isDoubleTap = false;
private bool is2Forward = false;
IEnumerator DoubleTap(float tapSpeed, float coolDown)
{
    isDoubleTap = true;
    if (Input.GetKeyDown(KeyCode.W) && !is2Forward)
    {
        Debug.Log("单击" + KeyCode.W + "键");
        yield return StartCoroutine(DoubleForward(tapSpeed, coolDown));
    }
    isDoubleTap = false;
}

IEnumerator DoubleForward(float dTapSpeed, float coolDown)
{
    is2Forward = true;
    float t = 0;
    //yield return new WaitForSeconds(0.1f);
    while (t < dTapSpeed)
    {
        t += Time.deltaTime;
        yield return new WaitForSeconds(0.01f);
        if (Input.GetKeyDown(KeyCode.W))
        {
            Debug.Log("双击" + KeyCode.W + "键");
            yield return new WaitForSeconds(coolDown);
            Debug.Log("可以开始下一次双击检测");
            is2Forward = false;
            yield break;
        }
    }
    is2Forward = false;
}

Tips
1、 string[] _AA = Enum.GetNames(typeof(FontStyle));
FontStyle[] _FontStyleList = (FontStyle[])Enum.GetValues(typeof(FontStyle));
2、 Debug.Log((string)Input.inputString);
使用这个方法可以返回对应按键的小写英文字母,inputString中只包含ASCII码中的字符。

这些其实都是很久以前做的笔记,保存在电脑上,现在在慢慢整理,其他字段属性再补充。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值