diff --git a/README.md b/README.md index f137354..527efaf 100644 --- a/README.md +++ b/README.md @@ -86,3 +86,14 @@ public void GetKeyStatus() var isCapsLockOn = simulator.InputDeviceState.IsTogglingKeyInEffect(VirtualKeyCode.CAPITAL); } ``` + +## Example: Get current mouse position +```csharp +public void GetMousePosition() +{ + var simulator = new InputSimulator(); + + // Gets the mouse position as System.Drawing.Point + var position = simulator.Mouse.Position; +} +``` diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 25f2fce..d3df9be 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -65,12 +65,13 @@ steps: - powershell: | $testsDirectory = Join-Path -Path '$(Build.SourcesDirectory)' -ChildPath 'tests' $coverageFile = Get-ChildItem -Path "$testsDirectory" -Recurse -File -Filter 'coverage.xml' | Select-Object -First 1 - & "$ENV:USERPROFILE\.nuget\packages\codecov\1.7.1\tools\codecov.exe" -f "$($coverageFile.FullName)" -t '$(CODECOV_TOKEN)' + $codecovExecutable = Get-ChildItem -Path "$ENV:USERPROFILE\.nuget\packages\codecov" -Recurse -File -Filter "codecov.exe" | Sort-Object -Descending -Property FullName | Select-Object -First 1 + & "$($codecovExecutable.FullName)" -f "$($coverageFile.FullName)" -t '$(CODECOV_TOKEN)' displayName: 'Upload coverage to codecov.io' - task: DotNetCoreCLI@2 displayName: 'Pack projects' - condition: and(succeeded(), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), startsWith(variables['Build.SourceBranch'], 'refs/heads/release/'))) + condition: and(succeeded(), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq(variables['Build.SourceBranch'], 'refs/heads/develop'), startsWith(variables['Build.SourceBranch'], 'refs/heads/release/'))) inputs: command: 'pack' packagesToPack: '$(Solution)' @@ -80,14 +81,14 @@ steps: versionEnvVar: 'GITVERSION_NUGETVERSIONV2' - task: NuGetToolInstaller@1 - condition: and(succeeded(), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), startsWith(variables['Build.SourceBranch'], 'refs/heads/release/'))) + condition: and(succeeded(), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq(variables['Build.SourceBranch'], 'refs/heads/develop'), startsWith(variables['Build.SourceBranch'], 'refs/heads/release/'))) inputs: versionSpec: '5.1.0' checkLatest: true - task: NuGetCommand@2 displayName: 'Push *.nupkg and *.snupkg to nuget.org' - condition: and(succeeded(), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), startsWith(variables['Build.SourceBranch'], 'refs/heads/release/'))) + condition: and(succeeded(), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq(variables['Build.SourceBranch'], 'refs/heads/develop'), startsWith(variables['Build.SourceBranch'], 'refs/heads/release/'))) inputs: command: 'push' packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg' @@ -103,8 +104,8 @@ steps: action: 'create' target: '$(Build.SourceVersion)' tagSource: 'manual' - tag: '$(Build.BuildNumber)' - title: '$(Build.BuildNumber)' + tag: 'v$(Build.BuildNumber)' + title: 'v$(Build.BuildNumber)' - task: GitHubRelease@0 condition: and(succeeded(), or(eq(variables['Build.SourceBranch'], 'refs/heads/develop'), startsWith(variables['Build.SourceBranch'], 'refs/heads/release/'))) @@ -114,6 +115,6 @@ steps: action: 'create' target: '$(Build.SourceVersion)' tagSource: 'manual' - tag: '$(Build.BuildNumber)' - title: '$(Build.BuildNumber)' + tag: 'v$(Build.BuildNumber)' + title: 'v$(Build.BuildNumber)' isPreRelease: true diff --git a/src/GregsStack.InputSimulatorStandard/IMouseSimulator.cs b/src/GregsStack.InputSimulatorStandard/IMouseSimulator.cs index 5204368..fe19f0d 100644 --- a/src/GregsStack.InputSimulatorStandard/IMouseSimulator.cs +++ b/src/GregsStack.InputSimulatorStandard/IMouseSimulator.cs @@ -12,6 +12,11 @@ public interface IMouseSimulator /// int MouseWheelClickSize { get; set; } + /// + /// Retrieves the position of the mouse cursor, in screen coordinates. + /// + System.Drawing.Point Position { get; } + /// /// Simulates mouse movement by the specified distance measured as a delta from the current mouse location in pixels. /// diff --git a/src/GregsStack.InputSimulatorStandard/MouseSimulator.cs b/src/GregsStack.InputSimulatorStandard/MouseSimulator.cs index 0db466c..5135886 100644 --- a/src/GregsStack.InputSimulatorStandard/MouseSimulator.cs +++ b/src/GregsStack.InputSimulatorStandard/MouseSimulator.cs @@ -43,6 +43,19 @@ internal MouseSimulator(IInputMessageDispatcher messageDispatcher) /// public int MouseWheelClickSize { get; set; } = 120; + /// + /// + /// Retrieves the position of the mouse cursor, in screen coordinates. + /// + public System.Drawing.Point Position + { + get + { + NativeMethods.GetCursorPos(out Point lpPoint); + return lpPoint; + } + } + /// /// /// Simulates mouse movement by the specified distance measured as a delta from the current mouse location in pixels. diff --git a/src/GregsStack.InputSimulatorStandard/Native/NativeMethods.cs b/src/GregsStack.InputSimulatorStandard/Native/NativeMethods.cs index 84c1737..b06f86e 100644 --- a/src/GregsStack.InputSimulatorStandard/Native/NativeMethods.cs +++ b/src/GregsStack.InputSimulatorStandard/Native/NativeMethods.cs @@ -106,5 +106,13 @@ internal static class NativeMethods /// [DllImport("user32.dll")] public static extern uint MapVirtualKey(uint uCode, uint uMapType); + + /// + /// Retrieves the cursor's position, in screen coordinates. + /// + /// A pointer to a structure that receives the screen coordinates of the cursor. + /// Returns true if successful or false otherwise. + [DllImport("user32.dll", SetLastError = true)] + public static extern bool GetCursorPos(out Point lpPoint); } } diff --git a/src/GregsStack.InputSimulatorStandard/Native/Point.cs b/src/GregsStack.InputSimulatorStandard/Native/Point.cs new file mode 100644 index 0000000..1cc1dd0 --- /dev/null +++ b/src/GregsStack.InputSimulatorStandard/Native/Point.cs @@ -0,0 +1,23 @@ +namespace GregsStack.InputSimulatorStandard.Native +{ + using System.Runtime.InteropServices; + + [StructLayout(LayoutKind.Sequential)] + internal struct Point + { + /// + /// Gets or sets the x-coordinate of this Point. + /// + public int X { get; set; } + + /// + /// Gets or sets the y-coordinate of this Point. + /// + public int Y { get; set; } + + public static implicit operator System.Drawing.Point(Point point) + { + return new System.Drawing.Point(point.X, point.Y); + } + } +} diff --git a/tests/GregsStack.InputSimulatorStandard.Tests/GregsStack.InputSimulatorStandard.Tests.csproj b/tests/GregsStack.InputSimulatorStandard.Tests/GregsStack.InputSimulatorStandard.Tests.csproj index ebd689e..6bc12db 100644 --- a/tests/GregsStack.InputSimulatorStandard.Tests/GregsStack.InputSimulatorStandard.Tests.csproj +++ b/tests/GregsStack.InputSimulatorStandard.Tests/GregsStack.InputSimulatorStandard.Tests.csproj @@ -6,10 +6,10 @@ - + - + all diff --git a/tests/GregsStack.InputSimulatorStandard.Tests/MouseSimulatorTests.cs b/tests/GregsStack.InputSimulatorStandard.Tests/MouseSimulatorTests.cs index d41992f..6523eee 100644 --- a/tests/GregsStack.InputSimulatorStandard.Tests/MouseSimulatorTests.cs +++ b/tests/GregsStack.InputSimulatorStandard.Tests/MouseSimulatorTests.cs @@ -23,5 +23,15 @@ public void NullMessageDispatcherThrowsInvalidOperationException() Assert.Throws(() => this.mouseSimulator = new MouseSimulator(null)); } } + + public class PositionProperty : MouseSimulatorTests + { + [Fact] + public void GetPosition() + { + var position = this.mouseSimulator.Position; + Assert.False(position.IsEmpty); + } + } } } diff --git a/tools/NuGet.Tools/NuGet.Tools.csproj b/tools/NuGet.Tools/NuGet.Tools.csproj index d882577..3b1973a 100644 --- a/tools/NuGet.Tools/NuGet.Tools.csproj +++ b/tools/NuGet.Tools/NuGet.Tools.csproj @@ -6,7 +6,7 @@ - +