Unity 模拟键盘输入

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


public class CarControllerTest : MonoBehaviour
{
    [DllImport("user32.dll", EntryPoint = "keybd_event")]
    public static extern void Keybd_event(
          byte bvk,//虚拟键值 ESC键对应的是27
          byte bScan,//0
          int dwFlags,//0为按下,1按住,2释放
          int dwExtraInfo//0
          );
    // Update is called once per frame
    void Update()
    {
        Keybd_event(79, 0, 0, 0);
        Keybd_event(79, 0, 2, 0);
        //用上下箭头来模拟左shift和ctrl事件
        //if (Input.GetKeyDown(KeyCode.UpArrow))
        //{
        //    Keybd_event(79, 0, 0, 0);
        //}
        //if (Input.GetKeyUp(KeyCode.UpArrow))
        //{
        //    Keybd_event(79, 0, 2, 0);
        //}
        //if (Input.GetKeyDown(KeyCode.O))
        //{
        //    print("o 被按下了");
        //}
    }
}

 

你可能感兴趣的:(unity)