Skip to content

Commit e957226

Browse files
authored
Merge pull request #38 from ovpoddar/main
PR: Issue #34 - Add Mouse Scroll Functionality for Mac, Linux, and Windows
2 parents 72fd3ef + 627e624 commit e957226

File tree

10 files changed

+236
-100
lines changed

10 files changed

+236
-100
lines changed

Desktop.Robot/AbstractRobot.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,6 @@ protected void ApplyAutoDelay()
7575
}
7676
}
7777

78-
}
78+
public abstract void MouseScrollVertical(int value);
79+
}
7980
}

Desktop.Robot/Clicks/Linux/Common.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ static class Common
77
internal const int LEFT_BUTTON = 1;
88
internal const int CENTER_BUTTON = 2;
99
internal const int RIGHT_BUTTON = 3;
10+
internal const int UP_BUTTON = 4;
11+
internal const int DOWN_BUTTON = 5;
1012

1113
[DllImport("./x11.os", EntryPoint = "click")]
1214
internal static extern void Click(bool down, int button);

Desktop.Robot/Desktop.Robot.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
</ItemGroup>
2323

2424
<ItemGroup>
25-
<None Include="osx.os">
25+
<None Include="osx.os">
2626
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
27-
<PackagePath>build\</PackagePath>
28-
<Pack>true</Pack>
29-
</None>
27+
<PackagePath>build\</PackagePath>
28+
<Pack>true</Pack>
29+
</None>
3030
<None Include="x11.os">
3131
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3232
<PackagePath>build\</PackagePath>

Desktop.Robot/IRobot.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public interface IRobot
2020
void Delay(uint ms);
2121
void MouseMove(uint x, uint y);
2222
void MouseMove(Point p);
23+
void MouseScrollVertical(int value);
2324
Point GetMousePosition();
2425
}
2526
}

Desktop.Robot/Linux/Robot.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using System;
1+
using Desktop.Robot.Clicks.Linux;
2+
using System;
23
using System.Drawing;
34
using System.Linq;
45
using System.Runtime.InteropServices;
6+
using System.Threading;
57

68
namespace Desktop.Robot.Linux
79
{
@@ -85,5 +87,20 @@ public override void MouseMove(uint x, uint y)
8587
[DllImport("./x11.os", EntryPoint = "pressKeyCode")]
8688
private static extern IntPtr pressKeyCode(int code, bool down, int flags);
8789

88-
}
90+
public override void MouseScrollVertical(int value)
91+
{
92+
if (value < 0)
93+
{
94+
click(true, Common.UP_BUTTON);
95+
Thread.Sleep(value);
96+
click(false, Common.UP_BUTTON);
97+
}
98+
else
99+
{
100+
click(true, Common.DOWN_BUTTON);
101+
Thread.Sleep(value);
102+
click(false, Common.DOWN_BUTTON);
103+
}
104+
}
105+
}
89106
}

Desktop.Robot/OSX/Robot.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ public override Point GetMousePosition()
6767
return new Point(coords[0], screenRes[1] - coords[1]);
6868
}
6969

70+
public override void MouseScrollVertical(int value)
71+
{
72+
verticalScroll(value);
73+
}
7074

71-
[DllImport("./osx.os", EntryPoint = "setMousePosition")]
75+
[DllImport("./osx.os", EntryPoint = "setMousePosition")]
7276
private static extern void setMousePosition(uint x, uint y);
7377

7478
[DllImport("./osx.os", EntryPoint = "getMousePosition")]
@@ -94,5 +98,9 @@ public override Point GetMousePosition()
9498

9599
[DllImport("./osx.os", EntryPoint = "sendCommandDown")]
96100
private static extern void sendCommandDown(ushort ch);
97-
}
101+
102+
[DllImport("./osx.os", EntryPoint = "verticalScroll")]
103+
private static extern void verticalScroll(int value);
104+
105+
}
98106
}

Desktop.Robot/Robot.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ public void MouseMove(Point p)
102102
robot.MouseMove(p);
103103
}
104104

105-
public void MouseUp(IClick click)
105+
public void MouseScrollVertical(int value)
106+
{
107+
robot.MouseScrollVertical(value);
108+
}
109+
110+
public void MouseUp(IClick click)
106111
{
107112
robot.MouseUp(click);
108113
}

Desktop.Robot/Source/osx/main.mm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,17 @@ void sendCommand(short input){
120120
sendCommandDown(input);
121121
sendCommandUp(input);
122122
}
123+
124+
void verticalScroll(int value){
125+
CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
126+
CGEventRef scrollEvent = CGEventCreateScrollWheelEvent(eventSource,
127+
kCGScrollEventUnitPixel,
128+
1,
129+
value);
130+
131+
CGEventPost(kCGHIDEventTap, scrollEvent);
132+
133+
CFRelease(scrollEvent);
134+
CFRelease(eventSource);
135+
}
123136
}

0 commit comments

Comments
 (0)