• Unity实现模拟按键


     模拟键盘输入首先要用到一个API函数:keybd_event。我现在要实现模拟ESC键按钮,通过下面的键值对照表可知ESC键的键码是27,然后我在下面实现了一个简单的例子。

    附:常用模拟键的键值对照表。

                          键盘键与虚拟键码对照表

          字母和数字键     数字小键盘的键       功能键         其它键
          键   键码     键   键码       键   键码     键      键码
          A   65      0   96        F1   112     Backspace    8
          B   66      1   97        F2   113     Tab        9
          C   67               2   98        F3   114     Clear        12
          D   68      3   99                     F4   115     Enter       13
          E   69       4   100                   F5   116     Shift        16
          F   70       5   101                   F6   117     Control       17
          G   71       6   102        F7   118       Alt          18
          H   72       7   103        F8   119     Caps Lock     20
          I   73       8   104       F9   120      Esc         27
          J   74       9   105       F10  121     Spacebar    32
          K   75                 *   106       F11  122     Page Up      33
          L   76                 +   107       F12  123     Page Down    34
          M   77       Enter  108       --   --     End         35
          N   78                 -    109       --   --       Home      36
          O   79       .   110       --   --       Left Arrow     37
          P   80       /    111       --   --      Up Arrow      38
          Q   81       --   --       --   --        Right Arrow     39
          R   82       --   --       --   --        Down Arrow     40
          S   83       --   --       --   --        Insert           45
          T   84       --   --       --   --        Delete          46
          U   85       --   --       --   --        Help          47
          V   86       --   --       --   --        Num Lock      144
          W   87          
          X   88      
          Y   89      
          Z   90      
          0   48      
          1   49      
          2   50       
          3   51       
          4   52       
          5   53       
          6   54       
          7   55       
          8   56       
          9   57  

    ----------------------------------

    例子:using UnityEngine;

    using System.Collections;

    using System.Runtime.InteropServices;

     

    public class KeybdEvent : 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

            );

     

        void Start()

        {

            Keybd_event(27,0,0,0);

            Keybd_event(27, 0, 1, 0);

            Keybd_event(27, 0, 2, 0);

        }

     

        void Update()

        {

            if (Input.GetKeyDown(KeyCode.Escape))

            {

                print("按下了ESC键");

            }

            if (Input.GetKey(KeyCode.Escape))

            {

                print("按住了ESC键");

            }

            if(Input.GetKeyUp(KeyCode.Escape))

            {

                print("松开了ESC键");

            }

        }

    }

  • 相关阅读:
    如何查看linux端口被哪个进程占用
    Beego 结合 GORM 操作 Mysql 数据库
    Linux Go proxy 设置
    working directory is not part of a module
    依赖注入 gin项目的目录结构说明
    详解django中使用定时任务的方法
    input 原声上传文件 file转化为binary对象发送给后台
    vue篇之事件总线(EventBus)
    小程序路由遇到的问题(eventChannel.emit is not a function报错)
    小程序组件(弹窗组件以及插槽使用)
  • 原文地址:https://www.cnblogs.com/AlanCheng/p/5703531.html
Copyright © 2020-2023  润新知