Unity 模拟鼠标及键盘事件

在项目中遇到要模拟键盘事件,在网上查资料,整理了一下模拟键盘按键,顺便把鼠标事件一起整理下,以后也许用的到。

主要还是用到user32.dll。

键盘模拟:

                      键盘键与虚拟键码对照表

      字母和数字键     数字小键盘的键       功能键         其它键
      键   键码     键   键码       键   键码     键      键码
      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  


    [DllImport("user32.dll", EntryPoint = "keybd_event")]
    static extern void keybd_event(
            byte bVk,            //虚拟键值 对应按键的ascll码十进制值  
            byte bScan,          //0
            int dwFlags,         //0 为按下,1按住,2为释放 
            int dwExtraInfo      //0
        );
     //模拟敲击A键
     private void InputA()
     {     	   
        keybd_event(65, 0, 0, 0);
        //keybd_event(65, 0, 1, 0);
        keybd_event(65, 0, 2, 0);
	 }

模拟鼠标事件:

using System;

public class MouseSimulater
{
    #region DLLs
	[DllImport("user32.dll")]
	private static extern int SetCursorPos(int x,int y); //设置光标位置
	[DllImport("user32.dll")]
	private static extern bool GetCursorPos(ref int x,ref int y); //获取光标位置
	[DllImport("user32.dll")]
	static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo); //鼠标事件

    // 方法参数说明
    // VOID mouse_event(
    //     DWORD dwFlags,         // motion and click options
    //     DWORD dx,              // horizontal position or change
    //     DWORD dy,              // vertical position or change
    //     DWORD dwData,          // wheel movement
    //     ULONG_PTR dwExtraInfo  // application-defined information
    // );

    [Flags]
    enum MouseEventFlag : uint
    {
        Move = 0x0001,
        LeftDown = 0x0002,
        LeftUp = 0x0004,
        RightDown = 0x0008,
        RightUp = 0x0010,
        MiddleDown = 0x0020,
        MiddleUp = 0x0040,
        XDown = 0x0080,
        XUp = 0x0100,
        Wheel = 0x0800,
        VirtualDesk = 0x4000,
        Absolute = 0x8000
    }
    #endregion

    // Unity屏幕坐标从左下角开始,向右为X轴,向上为Y轴
    // Windows屏幕坐标从左上角开始,向右为X轴,向下为Y轴

    /// 
    /// 移动鼠标到指定位置(使用Unity屏幕坐标而不是Windows屏幕坐标)
    /// 
    public static bool MoveTo(float x, float y)
    {
        if (x < 0 || y < 0 || x > UnityEngine.Screen.width || y > UnityEngine.Screen.height)
            return true;

        if (!UnityEngine.Screen.fullScreen)
        {
            UnityEngine.Debug.LogError("只能在全屏状态下使用!");
            return false;
        }

        SetCursorPos((int)x, (int)(UnityEngine.Screen.height - y));
        return true;
    }

    // 左键单击
    public static void LeftClick(float x = -1, float y = -1)
    {
        if (MoveTo(x, y))
        {
            mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
            mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
        }
    }

    // 右键单击
    public static void RightClick(float x = -1, float y = -1)
    {
        if (MoveTo(x, y))
        {
            mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero);
            mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
        }
    }

    // 中键单击
    public static void MiddleClick(float x = -1, float y = -1)
    {
        if (MoveTo(x, y))
        {
            mouse_event(MouseEventFlag.MiddleDown, 0, 0, 0, UIntPtr.Zero);
            mouse_event(MouseEventFlag.MiddleUp, 0, 0, 0, UIntPtr.Zero);
        }
    }

    // 左键按下
    public static void LeftDown(float x = -1, float y = -1)
    {
        if (MoveTo(x, y))
        {
            mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
        }
    }

    // 左键抬起
    public static void LeftUp(float x = -1, float y = -1)
    {
        if (MoveTo(x, y))
        {
            mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
        }
    }

    // 右键按下
    public static void RightDown(float x = -1, float y = -1)
    {
        if (MoveTo(x, y))
        {
            mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero);
        }
    }

    // 右键抬起
    public static void RightUp(float x = -1, float y = -1)
    {
        if (MoveTo(x, y))
        {
            mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
        }
    }

    // 中键按下
    public static void MiddleDown(float x = -1, float y = -1)
    {
        if (MoveTo(x, y))
        {
            mouse_event(MouseEventFlag.MiddleDown, 0, 0, 0, UIntPtr.Zero);
        }
    }

    // 中键抬起
    public static void MiddleUp(float x = -1, float y = -1)
    {
        if (MoveTo(x, y))
        {
            mouse_event(MouseEventFlag.MiddleUp, 0, 0, 0, UIntPtr.Zero);
        }
    }

    // 滚轮滚动
    public static void ScrollWheel(float value)
    {
        mouse_event(MouseEventFlag.Wheel, 0, 0, (uint)value, UIntPtr.Zero);
    }
}

你可能感兴趣的:(Unity)