Unity学习:按键AI的实现

项目中需要用到代码自动控制我按了键盘某个键功能,百度了一些写法,然后自己实现了想要的功能。代码Copy了可以直接使用。

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class GetKeyCodeKeyAI : MonoBehaviour
{
    [DllImport("user32.dll", EntryPoint = "keybd_event")]
    public static extern void keybd_event(
            byte bVk,    //对应按键的ascll码十进制值
            byte bScan,
            int dwFlags,  //0按下,1按住,2为抬起
            int dwExtraInfo
        );

    // Use this for initialization
    void Start()
    {
        keybd_event(9, 0, 0, 0);
        keybd_event(9, 0, 1, 0);
        keybd_event(9, 0, 2, 0);
    }

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

 

你可能感兴趣的:(Unity3d,unity)