From 1464b2e5fa0160bfb27dad2882b5c8a0996f9f01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20D=C3=B6llinger?= Date: Tue, 4 Jun 2024 10:12:05 +0200 Subject: [PATCH 1/5] Introduced delay after pressing modifier keys necessary when working remotely with higher latency. --- Desktop.Robot/Windows/Robot.cs | 44 +++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/Desktop.Robot/Windows/Robot.cs b/Desktop.Robot/Windows/Robot.cs index 0db03c4..6d678e1 100644 --- a/Desktop.Robot/Windows/Robot.cs +++ b/Desktop.Robot/Windows/Robot.cs @@ -2,6 +2,7 @@ using System.Drawing; using System.Runtime.InteropServices; using System.Threading; +using System.Threading.Tasks; namespace Desktop.Robot.Windows { @@ -42,13 +43,44 @@ public override void KeyPress(Key key) public override void KeyPress(char key) { - ApplyAutoDelay(); - var keycode = (byte)VkKeyScan(key); - keybd_event(keycode, 0, 0, 0); - keybd_event(keycode, 0, 2, 0); - } + ApplyAutoDelay(); + short result = VkKeyScan(key); + + // Get the high order byte of the result to check for Ctrl and Alt keys + var highOrderByte = (result >> 8) & 0xFF; + + // Check if the key is uppercase or if AltGr (Ctrl + Alt) is needed + if ((highOrderByte & 1) == 1) // Shift + { + KeyDown(Key.Shift); + } + if ((highOrderByte & 2) == 2) // Ctrl + { + KeyDown(Key.Control); + } + if ((highOrderByte & 4) == 4) // Alt + { + KeyDown(Key.Alt); + } + + keybd_event((byte)result, 0, 0, 0); // key down + keybd_event((byte)result, 0, 2, 0); // key up + + if ((highOrderByte & 1) == 1) // Shift + { + KeyUp(Key.Shift); + } + if ((highOrderByte & 2) == 2) // Ctrl + { + KeyUp(Key.Control); + } + if ((highOrderByte & 4) == 4) // Alt + { + KeyUp(Key.Alt); + } + } - public override void KeyUp(Key key) + public override void KeyUp(Key key) { ApplyAutoDelay(); var metadata = key.GetKeycode(); From 131549616a7217039bf8fc1ea1312a0ef1e29c24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20D=C3=B6llinger?= Date: Tue, 4 Jun 2024 11:15:06 +0200 Subject: [PATCH 2/5] Update target framework versions --- .vscode/launch.json | 24 +++++++++++++ .vscode/tasks.json | 36 +++++++++++++++++++ .../Desktop.Robot.TestApp.fsproj | 5 +-- Desktop.Robot/Desktop.Robot.csproj | 6 ++-- Example/Example.csproj | 2 +- 5 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..65ed28a --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + "program": "${workspaceFolder}/Example/bin/Debug/net6.0/Example.dll", + "args": [], + "cwd": "${workspaceFolder}/Example", + "console": "internalConsole", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 4bf1aaf..dfb3edd 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -21,6 +21,42 @@ "isDefault": true }, "detail": "Task generated by Debugger." + }, + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/Desktop.Robot.sln", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary;ForceNoAlign" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/Desktop.Robot.sln", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary;ForceNoAlign" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/Desktop.Robot.sln" + ], + "problemMatcher": "$msCompile" } ], "version": "2.0.0" diff --git a/Desktop.Robot.TestApp/Desktop.Robot.TestApp.fsproj b/Desktop.Robot.TestApp/Desktop.Robot.TestApp.fsproj index 8b74aa8..8f9b0a6 100644 --- a/Desktop.Robot.TestApp/Desktop.Robot.TestApp.fsproj +++ b/Desktop.Robot.TestApp/Desktop.Robot.TestApp.fsproj @@ -1,7 +1,7 @@ Exe - net6.0 + net8.0 3390;$(WarnOn) @@ -19,5 +19,6 @@ - + + \ No newline at end of file diff --git a/Desktop.Robot/Desktop.Robot.csproj b/Desktop.Robot/Desktop.Robot.csproj index acfee29..d8d3d8a 100644 --- a/Desktop.Robot/Desktop.Robot.csproj +++ b/Desktop.Robot/Desktop.Robot.csproj @@ -1,6 +1,6 @@  - net6.0 + net8.0 1.5.0 true Lucas Simas @@ -19,8 +19,8 @@ - - + + diff --git a/Example/Example.csproj b/Example/Example.csproj index e50c7a2..98b5fec 100644 --- a/Example/Example.csproj +++ b/Example/Example.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net8.0 From 4c4f8d40f02795128c2f74e630f4439c00b6fc03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20D=C3=B6llinger?= Date: Tue, 4 Jun 2024 11:36:41 +0200 Subject: [PATCH 3/5] Update project metadata and target framework to .NET 8 --- Desktop.Robot/Desktop.Robot.csproj | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Desktop.Robot/Desktop.Robot.csproj b/Desktop.Robot/Desktop.Robot.csproj index d8d3d8a..1a92a7d 100644 --- a/Desktop.Robot/Desktop.Robot.csproj +++ b/Desktop.Robot/Desktop.Robot.csproj @@ -1,15 +1,18 @@  net8.0 - 1.5.0 + 1.5.1 true - Lucas Simas - A library used to control your mouse and keyboard programmatically in .NET Core + Lucas Simas, Christoph Döllinger + A library used to control your mouse and keyboard programmatically in .NET Core +Updated to .NET 8 https://lucassklp.github.io/Desktop.Robot/ - https://github.com/lucassklp/Desktop.Robot + https://github.com/MrDoe/Desktop.Robot logo.mini.png LICENSE README.md + $(AssemblyName)_Net8_v + $(AssemblyName)_Net8_v From 8d8e07dc08b101ac33a09c76816ced751870a50b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20D=C3=B6llinger?= Date: Thu, 6 Jun 2024 15:48:55 +0200 Subject: [PATCH 4/5] Introduced delay in linux code --- .vscode/launch.json | 2 +- .../Desktop.Robot.TestApp.fsproj | 8 ++-- Desktop.Robot.sln | 28 +++++++------- Desktop.Robot/Desktop.Robot.csproj | 2 +- Desktop.Robot/Linux/Robot.cs | 38 ++++++++++++++++--- Tests/Tests.csproj | 2 +- 6 files changed, 54 insertions(+), 26 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 65ed28a..9792623 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "type": "coreclr", "request": "launch", "preLaunchTask": "build", - "program": "${workspaceFolder}/Example/bin/Debug/net6.0/Example.dll", + "program": "${workspaceFolder}/Example/bin/Debug/net8.0/Example.dll", "args": [], "cwd": "${workspaceFolder}/Example", "console": "internalConsole", diff --git a/Desktop.Robot.TestApp/Desktop.Robot.TestApp.fsproj b/Desktop.Robot.TestApp/Desktop.Robot.TestApp.fsproj index 8f9b0a6..8c02849 100644 --- a/Desktop.Robot.TestApp/Desktop.Robot.TestApp.fsproj +++ b/Desktop.Robot.TestApp/Desktop.Robot.TestApp.fsproj @@ -14,10 +14,10 @@ - - - - + + + + diff --git a/Desktop.Robot.sln b/Desktop.Robot.sln index 69cb8aa..dacca3d 100644 --- a/Desktop.Robot.sln +++ b/Desktop.Robot.sln @@ -7,8 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Desktop.Robot", "Desktop.Ro EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "Example\Example.csproj", "{9B7B0021-B931-4488-84E7-D4DD4C74A555}" EndProject -Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Desktop.Robot.TestApp", "Desktop.Robot.TestApp\Desktop.Robot.TestApp.fsproj", "{05C9255A-C2BE-4F80-9EA1-9038DE424FB4}" -EndProject +#Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Desktop.Robot.TestApp", "Desktop.Robot.TestApp\Desktop.Robot.TestApp.fsproj", "{05C9255A-C2BE-4F80-9EA1-9038DE424FB4}" +#EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -46,17 +46,17 @@ Global {9B7B0021-B931-4488-84E7-D4DD4C74A555}.Release|x64.Build.0 = Release|Any CPU {9B7B0021-B931-4488-84E7-D4DD4C74A555}.Release|x86.ActiveCfg = Release|Any CPU {9B7B0021-B931-4488-84E7-D4DD4C74A555}.Release|x86.Build.0 = Release|Any CPU - {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Debug|Any CPU.Build.0 = Debug|Any CPU - {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Debug|x64.ActiveCfg = Debug|Any CPU - {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Debug|x64.Build.0 = Debug|Any CPU - {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Debug|x86.ActiveCfg = Debug|Any CPU - {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Debug|x86.Build.0 = Debug|Any CPU - {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Release|Any CPU.ActiveCfg = Release|Any CPU - {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Release|Any CPU.Build.0 = Release|Any CPU - {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Release|x64.ActiveCfg = Release|Any CPU - {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Release|x64.Build.0 = Release|Any CPU - {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Release|x86.ActiveCfg = Release|Any CPU - {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Release|x86.Build.0 = Release|Any CPU +# {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU +# {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Debug|Any CPU.Build.0 = Debug|Any CPU +# {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Debug|x64.ActiveCfg = Debug|Any CPU +# {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Debug|x64.Build.0 = Debug|Any CPU +# {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Debug|x86.ActiveCfg = Debug|Any CPU +# {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Debug|x86.Build.0 = Debug|Any CPU +# {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Release|Any CPU.ActiveCfg = Release|Any CPU +# {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Release|Any CPU.Build.0 = Release|Any CPU +# {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Release|x64.ActiveCfg = Release|Any CPU +# {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Release|x64.Build.0 = Release|Any CPU +# {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Release|x86.ActiveCfg = Release|Any CPU +# {05C9255A-C2BE-4F80-9EA1-9038DE424FB4}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/Desktop.Robot/Desktop.Robot.csproj b/Desktop.Robot/Desktop.Robot.csproj index 1a92a7d..b1eba90 100644 --- a/Desktop.Robot/Desktop.Robot.csproj +++ b/Desktop.Robot/Desktop.Robot.csproj @@ -1,7 +1,7 @@  net8.0 - 1.5.1 + 1.5.2 true Lucas Simas, Christoph Döllinger A library used to control your mouse and keyboard programmatically in .NET Core diff --git a/Desktop.Robot/Linux/Robot.cs b/Desktop.Robot/Linux/Robot.cs index 9500b80..1eb2816 100644 --- a/Desktop.Robot/Linux/Robot.cs +++ b/Desktop.Robot/Linux/Robot.cs @@ -44,9 +44,38 @@ public override void KeyPress(Key key) public override void KeyPress(char key) { ApplyAutoDelay(); - var flags = char.IsUpper(key) ? (1 << 0) : 0; - pressKey(key, true, flags); - pressKey(key, false, flags); + // Get the high order byte of the result to check for Ctrl and Alt keys + var highOrderByte = char.IsUpper(key) ? (1 << 0) : 0; + + // Check if the key is uppercase or if AltGr (Ctrl + Alt) is needed + if ((highOrderByte & 1) == 1) // Shift + { + KeyDown(Key.Shift); + } + if ((highOrderByte & 2) == 2) // Ctrl + { + KeyDown(Key.Control); + } + if ((highOrderByte & 4) == 4) // Alt + { + KeyDown(Key.Alt); + } + + pressKey(key, true, highOrderByte); + pressKey(key, false, highOrderByte); + + if ((highOrderByte & 1) == 1) // Shift + { + KeyUp(Key.Shift); + } + if ((highOrderByte & 2) == 2) // Ctrl + { + KeyUp(Key.Control); + } + if ((highOrderByte & 4) == 4) // Alt + { + KeyUp(Key.Alt); + } } public override void KeyUp(Key key) @@ -69,7 +98,6 @@ public override void MouseMove(int x, int y) moveMouse(x, y); } - public override void MouseScroll(int value) { ApplyAutoDelay(); @@ -84,7 +112,7 @@ public override void MouseScroll(int value, TimeSpan duration) public override void MouseScroll(int value, TimeSpan duration, int steps) { ApplyAutoDelay(); - for (int i = 0; i < steps; i++) + for (int i = 0; i < steps; ++i) { DoMouseScroll(value / steps, Convert.ToInt32(duration.TotalMilliseconds) / steps); } diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index c3717b2..3858941 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -6,7 +6,7 @@ - + From ea3d65381a54c714007f1df8b4d6e467f168b973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20D=C3=B6llinger?= <75829913+MrDoe@users.noreply.github.com> Date: Thu, 6 Jun 2024 15:55:21 +0200 Subject: [PATCH 5/5] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fdd3fce..bb674de 100644 --- a/README.md +++ b/README.md @@ -20,13 +20,13 @@ I made this library because the dotnet SDK doesn't support simulation of mouse a If you are using Package Manager: ```bash -Install-Package Desktop.Robot -Version 1.5.0 +Install-Package Desktop.Robot -Version 1.5.2 ``` If you are using .NET CLI ```bash -dotnet add package Desktop.Robot --version 1.5.0 +dotnet add package Desktop.Robot --version 1.5.2 ```