美文网首页
Unity通过代码实现按键点击

Unity通过代码实现按键点击

作者: 李狗多 | 来源:发表于2020-03-24 21:44 被阅读0次

首先要使用一个Api函数

keybd_event

通过设置对应按键的ascll码十进制值 达到实现按键点击的效果。

常用模拟键对照表

clipboard.png
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
 
public class GetKeyCodeKey : MonoBehaviour {
    [DllImport("user32.dll", EntryPoint = "keybd_event")]
    public static extern void keybd_event(
            byte bVk,    //虚拟键值 对应按键的ascll码十进制值
            byte bScan,// 0
            int dwFlags,  //0 为按下,1按住,2为释放
            int dwExtraInfo  // 0
        );         
 
 
    // Use this for initialization
    void Start () {
            keybd_event(65, 0, 0, 0);
            keybd_event(65, 0, 1, 0);
            keybd_event(65, 0, 2, 0);
    }
    
    // Update is called once per frame
    void Update () {
            if (Input.GetKeyDown(KeyCode.A))
            {
                    Debug.Log("按下了A键");
            }
            if (Input.GetKey(KeyCode.A))
            {
                    Debug.Log("按住了A键");
            }
        if (Input.GetKey(KeyCode.A))        
        {            
            Debug.Log("按住了A键");        
        }   
        }
}

相关文章

网友评论

      本文标题:Unity通过代码实现按键点击

      本文链接:https://www.haomeiwen.com/subject/claayhtx.html