Tanoda
MouseEvent.cs
Go to the documentation of this file.
1using System;
2using System.Runtime.InteropServices;
3using UnityEngine;
4
5public class MouseEvent : MonoBehaviour
6{
7#if !UNITY_WEBGL
8 #region CsunyaDolgok
9
10 [DllImport("user32.dll")]
11 static extern IntPtr GetForegroundWindow();
12
13 [DllImport("user32.dll")]
14 private static extern IntPtr FindWindow(string className, string windowName);
15
16 [DllImport("user32.dll")]
17 public static extern bool ClientToScreen(IntPtr hWnd, ref System.Drawing.Point lpPoint);
18
19 [Flags]
20 internal enum MOUSEEVENTF : uint
21 {
22 ABSOLUTE = 0x8000,
23 HWHEEL = 0x01000,
24 MOVE = 0x0001,
25 MOVE_NOCOALESCE = 0x2000,
26 LEFTDOWN = 0x0002,
27 LEFTUP = 0x0004,
28 RIGHTDOWN = 0x0008,
29 RIGHTUP = 0x0010,
30 MIDDLEDOWN = 0x0020,
31 MIDDLEUP = 0x0040,
32 VIRTUALDESK = 0x4000,
33 WHEEL = 0x0800,
34 XDOWN = 0x0080,
35 XUP = 0x0100
36 }
37
38 [StructLayout(LayoutKind.Sequential)]
39 public struct POINT
40 {
41 public int X;
42 public int Y;
43
44 public POINT(int x, int y)
45 {
46 this.X = x;
47 this.Y = y;
48 }
49
50 public static implicit operator System.Drawing.Point(POINT p)
51 {
52 return new System.Drawing.Point(p.X, p.Y);
53 }
54
55 public static implicit operator POINT(System.Drawing.Point p)
56 {
57 return new POINT(p.X, p.Y);
58 }
59 }
60
61 [DllImport("user32.dll", SetLastError = true)]
62 [return: MarshalAs(UnmanagedType.Bool)]
63 static extern bool GetCursorPos(out POINT lpPoint);
64 [DllImport("user32.dll")]
65 static extern void mouse_event(MOUSEEVENTF dwFlags, int dx, int dy, uint dwData,
66int dwExtraInfo);
67 #endregion
68
69 public static void MoveMouse(int x, int y)
70 {
71 var hWnd =
72#if UNITY_EDITOR
73 GetForegroundWindow();
74#else
75 FindWindow("UnityWndClass", "VR Skills Toolkit");
76 #endif
77
78 System.Drawing.Point point = new System.Drawing.Point(0, 0);
79
80 if (!ClientToScreen(hWnd, ref point))
81 {
82 Debug.LogError("ERROR: can't get window pos!");
83 return;
84 }
85
86 mouse_event((MOUSEEVENTF)0x8001, (ushort.MaxValue / Display.main.systemWidth) * (point.X + x), (ushort.MaxValue / Display.main.systemHeight) * (point.Y + y), 0, 0);
87 }
88
89 public static void ClickMouse()
90 {
91 mouse_event(MOUSEEVENTF.LEFTDOWN | MOUSEEVENTF.LEFTUP, 0, 0, 0, 0);
92 }
93
94 public static void MouseDown()
95 {
96 mouse_event(MOUSEEVENTF.LEFTDOWN, 0, 0, 0, 0);
97 }
98 public static void Mouseup()
99 {
100 mouse_event(MOUSEEVENTF.LEFTUP, 0, 0, 0, 0);
101 }
102 public static void Scroll(uint value)
103 {
104 mouse_event(MOUSEEVENTF.WHEEL,0,0,value,0);
105 }
106
107#endif
108}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
static bool ClientToScreen(IntPtr hWnd, ref System.Drawing.Point lpPoint)
static void Scroll(uint value)
Definition: MouseEvent.cs:102
static void Mouseup()
Definition: MouseEvent.cs:98
static void MoveMouse(int x, int y)
Definition: MouseEvent.cs:69
static void ClickMouse()
Definition: MouseEvent.cs:89
static void MouseDown()
Definition: MouseEvent.cs:94
POINT(int x, int y)
Definition: MouseEvent.cs:44