|
| 1 | +using System; |
| 2 | +using System.Drawing; |
| 3 | +using System.Linq; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using System.Threading; |
| 6 | + |
| 7 | +namespace Desktop.Robot.OSX.Arm |
| 8 | +{ |
| 9 | + public class Robot : AbstractRobot |
| 10 | + { |
| 11 | + public override void KeyDown(Key key) |
| 12 | + { |
| 13 | + ApplyAutoDelay(); |
| 14 | + var keycode = (ushort)key.GetKeycode().Keycode; |
| 15 | + sendCommandDown(keycode); |
| 16 | + } |
| 17 | + |
| 18 | + public override void KeyDown(char key) |
| 19 | + { |
| 20 | + ApplyAutoDelay(); |
| 21 | + keyDown(key); |
| 22 | + } |
| 23 | + |
| 24 | + public override void KeyPress(Key key) |
| 25 | + { |
| 26 | + ApplyAutoDelay(); |
| 27 | + var keycode = (ushort)key.GetKeycode().Keycode; |
| 28 | + sendCommand(keycode); |
| 29 | + } |
| 30 | + |
| 31 | + public override void KeyPress(char key) |
| 32 | + { |
| 33 | + ApplyAutoDelay(); |
| 34 | + keyPress(key); |
| 35 | + } |
| 36 | + |
| 37 | + public override void KeyUp(Key key) |
| 38 | + { |
| 39 | + ApplyAutoDelay(); |
| 40 | + var keycode = (ushort)key.GetKeycode().Keycode; |
| 41 | + sendCommandUp(keycode); |
| 42 | + } |
| 43 | + |
| 44 | + public override void KeyUp(char key) |
| 45 | + { |
| 46 | + ApplyAutoDelay(); |
| 47 | + keyUp(key); |
| 48 | + } |
| 49 | + |
| 50 | + public override void MouseMove(int x, int y) |
| 51 | + { |
| 52 | + ApplyAutoDelay(); |
| 53 | + setMousePosition(x, y); |
| 54 | + } |
| 55 | + |
| 56 | + public override Point GetMousePosition() |
| 57 | + { |
| 58 | + var pos = Marshal.PtrToStringAnsi(getMousePosition()); |
| 59 | + var coords = pos.Split(";") |
| 60 | + .Select(x => Convert.ToInt32(x)) |
| 61 | + .ToArray(); |
| 62 | + |
| 63 | + var res = Marshal.PtrToStringAnsi(screenResolution()); |
| 64 | + var screenRes = res.Split("x") |
| 65 | + .Select(x => Convert.ToInt32(x)) |
| 66 | + .ToArray(); |
| 67 | + |
| 68 | + return new Point(coords[0], screenRes[1] - coords[1]); |
| 69 | + } |
| 70 | + |
| 71 | + public override void MouseScroll(int value) |
| 72 | + { |
| 73 | + ApplyAutoDelay(); |
| 74 | + DoMouseScroll(value); |
| 75 | + } |
| 76 | + |
| 77 | + public override void MouseScroll(int value, TimeSpan duration) |
| 78 | + { |
| 79 | + MouseScroll(value, duration, 50); |
| 80 | + } |
| 81 | + |
| 82 | + public override void MouseScroll(int value, TimeSpan duration, int steps) |
| 83 | + { |
| 84 | + ApplyAutoDelay(); |
| 85 | + for (int i = 0; i < steps; i++) |
| 86 | + { |
| 87 | + Thread.Sleep(duration / steps); |
| 88 | + DoMouseScroll(value / steps); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private void DoMouseScroll(int value) |
| 93 | + { |
| 94 | + verticalScroll(value); |
| 95 | + } |
| 96 | + |
| 97 | + [DllImport("./osx_arm.os", EntryPoint = "setMousePosition")] |
| 98 | + private static extern void setMousePosition(int x, int y); |
| 99 | + |
| 100 | + [DllImport("./osx_arm.os", EntryPoint = "getMousePosition")] |
| 101 | + private static extern IntPtr getMousePosition(); |
| 102 | + |
| 103 | + [DllImport("./osx_arm.os", EntryPoint = "screenResolution")] |
| 104 | + private static extern IntPtr screenResolution(); |
| 105 | + |
| 106 | + [DllImport("./osx_arm.os", EntryPoint = "keyPress")] |
| 107 | + private static extern void keyPress(char ch); |
| 108 | + |
| 109 | + [DllImport("./osx_arm.os", EntryPoint = "keyUp")] |
| 110 | + private static extern void keyUp(char ch); |
| 111 | + |
| 112 | + [DllImport("./osx_arm.os", EntryPoint = "keyDown")] |
| 113 | + private static extern void keyDown(char ch); |
| 114 | + |
| 115 | + [DllImport("./osx_arm.os", EntryPoint = "sendCommand")] |
| 116 | + private static extern void sendCommand(ushort ch); |
| 117 | + |
| 118 | + [DllImport("./osx_arm.os", EntryPoint = "sendCommandUp")] |
| 119 | + private static extern void sendCommandUp(ushort ch); |
| 120 | + |
| 121 | + [DllImport("./osx_arm.os", EntryPoint = "sendCommandDown")] |
| 122 | + private static extern void sendCommandDown(ushort ch); |
| 123 | + |
| 124 | + [DllImport("./osx_arm.os", EntryPoint = "verticalScroll")] |
| 125 | + private static extern void verticalScroll(int value); |
| 126 | + |
| 127 | + } |
| 128 | +} |
0 commit comments