From 4329266122d5ff87705494790109290031784f85 Mon Sep 17 00:00:00 2001 From: Jared Date: Thu, 19 Nov 2020 17:41:38 -0800 Subject: [PATCH] Add middle-mouse click. --- Desktop.Core/Enums/ButtonAction.cs | 12 +++ .../Interfaces/IKeyboardMouseInput.cs | 10 +- Desktop.Core/Services/DtoMessageHandler.cs | 23 +---- .../Services/KeyboardMouseInputLinux.cs | 51 +++++----- Desktop.Win/Services/KeyboardMouseInputWin.cs | 94 ++++++++++--------- .../src/RemoteControl/InputEventHandlers.ts | 3 - 6 files changed, 92 insertions(+), 101 deletions(-) create mode 100644 Desktop.Core/Enums/ButtonAction.cs diff --git a/Desktop.Core/Enums/ButtonAction.cs b/Desktop.Core/Enums/ButtonAction.cs new file mode 100644 index 00000000..d2d5acfd --- /dev/null +++ b/Desktop.Core/Enums/ButtonAction.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Remotely.Desktop.Core.Enums +{ + public enum ButtonAction + { + Down, + Up + } +} diff --git a/Desktop.Core/Interfaces/IKeyboardMouseInput.cs b/Desktop.Core/Interfaces/IKeyboardMouseInput.cs index 845efac9..b2fe19ea 100644 --- a/Desktop.Core/Interfaces/IKeyboardMouseInput.cs +++ b/Desktop.Core/Interfaces/IKeyboardMouseInput.cs @@ -1,17 +1,17 @@ -namespace Remotely.Desktop.Core.Interfaces +using Remotely.Desktop.Core.Enums; +using Remotely.Desktop.Core.Services; + +namespace Remotely.Desktop.Core.Interfaces { public interface IKeyboardMouseInput { void SendKeyDown(string key); void SendKeyUp(string key); void SendMouseMove(double percentX, double percentY, Services.Viewer viewer); - void SendLeftMouseDown(double percentX, double percentY, Services.Viewer viewer); - void SendLeftMouseUp(double percentX, double percentY, Services.Viewer viewer); - void SendRightMouseDown(double percentX, double percentY, Services.Viewer viewer); - void SendRightMouseUp(double percentX, double percentY, Services.Viewer viewer); void SendMouseWheel(int deltaY); void SendText(string transferText); void ToggleBlockInput(bool toggleOn); void SetKeyStatesUp(); + void SendMouseButtonAction(int button, ButtonAction buttonAction, double percentX, double percentY, Viewer viewer); } } diff --git a/Desktop.Core/Services/DtoMessageHandler.cs b/Desktop.Core/Services/DtoMessageHandler.cs index 25ce04c7..c1faf281 100644 --- a/Desktop.Core/Services/DtoMessageHandler.cs +++ b/Desktop.Core/Services/DtoMessageHandler.cs @@ -1,4 +1,5 @@ using MessagePack; +using Remotely.Desktop.Core.Enums; using Remotely.Desktop.Core.Interfaces; using Remotely.Shared.Enums; using Remotely.Shared.Models.RemoteControlDtos; @@ -197,14 +198,7 @@ namespace Remotely.Desktop.Core.Services private void MouseDown(byte[] message, Services.Viewer viewer) { var dto = MessagePackSerializer.Deserialize(message); - if (dto.Button == 0) - { - KeyboardMouseInput.SendLeftMouseDown(dto.PercentX, dto.PercentY, viewer); - } - else if (dto.Button == 2) - { - KeyboardMouseInput.SendRightMouseDown(dto.PercentX, dto.PercentY, viewer); - } + KeyboardMouseInput.SendMouseButtonAction(dto.Button, ButtonAction.Down, dto.PercentX, dto.PercentY, viewer); } private void MouseMove(byte[] message, Services.Viewer viewer) @@ -216,14 +210,7 @@ namespace Remotely.Desktop.Core.Services private void MouseUp(byte[] message, Services.Viewer viewer) { var dto = MessagePackSerializer.Deserialize(message); - if (dto.Button == 0) - { - KeyboardMouseInput.SendLeftMouseUp(dto.PercentX, dto.PercentY, viewer); - } - else if (dto.Button == 2) - { - KeyboardMouseInput.SendRightMouseUp(dto.PercentX, dto.PercentY, viewer); - } + KeyboardMouseInput.SendMouseButtonAction(dto.Button, ButtonAction.Up, dto.PercentX, dto.PercentY, viewer); } private void MouseWheel(byte[] message) @@ -262,8 +249,8 @@ namespace Remotely.Desktop.Core.Services private void Tap(byte[] message, Services.Viewer viewer) { var dto = MessagePackSerializer.Deserialize(message); - KeyboardMouseInput.SendLeftMouseDown(dto.PercentX, dto.PercentY, viewer); - KeyboardMouseInput.SendLeftMouseUp(dto.PercentX, dto.PercentY, viewer); + KeyboardMouseInput.SendMouseButtonAction(0, ButtonAction.Down, dto.PercentX, dto.PercentY, viewer); + KeyboardMouseInput.SendMouseButtonAction(0, ButtonAction.Up, dto.PercentX, dto.PercentY, viewer); } private void ToggleAudio(byte[] message) diff --git a/Desktop.Linux/Services/KeyboardMouseInputLinux.cs b/Desktop.Linux/Services/KeyboardMouseInputLinux.cs index ac2e7d3b..cf83211a 100644 --- a/Desktop.Linux/Services/KeyboardMouseInputLinux.cs +++ b/Desktop.Linux/Services/KeyboardMouseInputLinux.cs @@ -1,4 +1,5 @@ -using Remotely.Desktop.Core.Interfaces; +using Remotely.Desktop.Core.Enums; +using Remotely.Desktop.Core.Interfaces; using Remotely.Desktop.Core.Services; using Remotely.Desktop.Linux.X11Interop; using Remotely.Shared.Utilities; @@ -57,35 +58,6 @@ namespace Remotely.Desktop.Linux.Services } - public void SendLeftMouseDown(double percentX, double percentY, Viewer viewer) - { - try - { - Init(); - SendMouseMove(percentX, percentY, viewer); - LibXtst.XTestFakeButtonEvent(Display, 1, true, 0); - LibX11.XSync(Display, false); - } - catch (Exception ex) - { - Logger.Write(ex); - } - } - - public void SendLeftMouseUp(double percentX, double percentY, Viewer viewer) - { - try - { - Init(); - SendMouseMove(percentX, percentY, viewer); - LibXtst.XTestFakeButtonEvent(Display, 1, false, 0); - LibX11.XSync(Display, false); - } - catch (Exception ex) - { - Logger.Write(ex); - } - } public void SendMouseMove(double percentX, double percentY, Viewer viewer) { @@ -158,6 +130,25 @@ namespace Remotely.Desktop.Linux.Services } } + public void SendMouseButtonAction(int button, ButtonAction buttonAction, double percentX, double percentY, Viewer viewer) + { + try + { + var isPressed = buttonAction == ButtonAction.Down; + // Browser buttons start at 0. XTest starts at 1. + var mouseButton = (uint)(button + 1); + + Init(); + SendMouseMove(percentX, percentY, viewer); + LibXtst.XTestFakeButtonEvent(Display, mouseButton, isPressed, 0); + LibX11.XSync(Display, false); + } + catch (Exception ex) + { + Logger.Write(ex); + } + } + public void SendText(string transferText) { foreach (var key in transferText) diff --git a/Desktop.Win/Services/KeyboardMouseInputWin.cs b/Desktop.Win/Services/KeyboardMouseInputWin.cs index 0c39d775..9ea0e6ec 100644 --- a/Desktop.Win/Services/KeyboardMouseInputWin.cs +++ b/Desktop.Win/Services/KeyboardMouseInputWin.cs @@ -1,4 +1,5 @@ -using Remotely.Desktop.Core.Interfaces; +using Remotely.Desktop.Core.Enums; +using Remotely.Desktop.Core.Interfaces; using Remotely.Desktop.Core.Services; using Remotely.Shared.Utilities; using Remotely.Shared.Win32; @@ -77,29 +78,60 @@ namespace Remotely.Desktop.Win.Services }); } - public void SendLeftMouseDown(double percentX, double percentY, Viewer viewer) + public void SendMouseButtonAction(int button, ButtonAction buttonAction, double percentX, double percentY, Viewer viewer) { TryOnInputDesktop(() => { + MOUSEEVENTF mouseEvent; + switch (button) + { + case 0: + switch (buttonAction) + { + case ButtonAction.Down: + mouseEvent = MOUSEEVENTF.LEFTDOWN; + break; + case ButtonAction.Up: + mouseEvent = MOUSEEVENTF.LEFTUP; + break; + default: + return; + } + break; + case 1: + switch (buttonAction) + { + case ButtonAction.Down: + mouseEvent = MOUSEEVENTF.MIDDLEDOWN; + break; + case ButtonAction.Up: + mouseEvent = MOUSEEVENTF.MIDDLEUP; + break; + default: + return; + } + break; + case 2: + switch (buttonAction) + { + case ButtonAction.Down: + mouseEvent = MOUSEEVENTF.RIGHTDOWN; + break; + case ButtonAction.Up: + mouseEvent = MOUSEEVENTF.RIGHTUP; + break; + default: + return; + } + break; + default: + return; + } var xyPercent = GetAbsolutePercentFromRelativePercent(percentX, percentY, viewer.Capturer); // Coordinates must be normalized. The bottom-right coordinate is mapped to 65535. var normalizedX = xyPercent.Item1 * 65535D; var normalizedY = xyPercent.Item2 * 65535D; - var union = new InputUnion() { mi = new MOUSEINPUT() { dwFlags = MOUSEEVENTF.ABSOLUTE | MOUSEEVENTF.LEFTDOWN | MOUSEEVENTF.VIRTUALDESK, dx = (int)normalizedX, dy = (int)normalizedY, time = 0, mouseData = 0, dwExtraInfo = GetMessageExtraInfo() } }; - var input = new INPUT() { type = InputType.MOUSE, U = union }; - SendInput(1, new INPUT[] { input }, INPUT.Size); - }); - } - - public void SendLeftMouseUp(double percentX, double percentY, Viewer viewer) - { - TryOnInputDesktop(() => - { - var xyPercent = GetAbsolutePercentFromRelativePercent(percentX, percentY, viewer.Capturer); - // Coordinates must be normalized. The bottom-right coordinate is mapped to 65535. - var normalizedX = xyPercent.Item1 * 65535D; - var normalizedY = xyPercent.Item2 * 65535D; - var union = new InputUnion() { mi = new MOUSEINPUT() { dwFlags = MOUSEEVENTF.ABSOLUTE | MOUSEEVENTF.LEFTUP | MOUSEEVENTF.VIRTUALDESK, dx = (int)normalizedX, dy = (int)normalizedY, time = 0, mouseData = 0, dwExtraInfo = GetMessageExtraInfo() } }; + var union = new InputUnion() { mi = new MOUSEINPUT() { dwFlags = MOUSEEVENTF.ABSOLUTE | mouseEvent | MOUSEEVENTF.VIRTUALDESK, dx = (int)normalizedX, dy = (int)normalizedY, time = 0, mouseData = 0, dwExtraInfo = GetMessageExtraInfo() } }; var input = new INPUT() { type = InputType.MOUSE, U = union }; SendInput(1, new INPUT[] { input }, INPUT.Size); }); @@ -137,34 +169,6 @@ namespace Remotely.Desktop.Win.Services }); } - public void SendRightMouseDown(double percentX, double percentY, Viewer viewer) - { - TryOnInputDesktop(() => - { - var xyPercent = GetAbsolutePercentFromRelativePercent(percentX, percentY, viewer.Capturer); - // Coordinates must be normalized. The bottom-right coordinate is mapped to 65535. - var normalizedX = xyPercent.Item1 * 65535D; - var normalizedY = xyPercent.Item2 * 65535D; - var union = new InputUnion() { mi = new MOUSEINPUT() { dwFlags = MOUSEEVENTF.ABSOLUTE | MOUSEEVENTF.RIGHTDOWN | MOUSEEVENTF.VIRTUALDESK, dx = (int)normalizedX, dy = (int)normalizedY, time = 0, mouseData = 0, dwExtraInfo = GetMessageExtraInfo() } }; - var input = new INPUT() { type = InputType.MOUSE, U = union }; - SendInput(1, new INPUT[] { input }, INPUT.Size); - }); - } - - public void SendRightMouseUp(double percentX, double percentY, Viewer viewer) - { - TryOnInputDesktop(() => - { - var xyPercent = GetAbsolutePercentFromRelativePercent(percentX, percentY, viewer.Capturer); - // Coordinates must be normalized. The bottom-right coordinate is mapped to 65535. - var normalizedX = xyPercent.Item1 * 65535D; - var normalizedY = xyPercent.Item2 * 65535D; - var union = new InputUnion() { mi = new MOUSEINPUT() { dwFlags = MOUSEEVENTF.ABSOLUTE | MOUSEEVENTF.RIGHTUP | MOUSEEVENTF.VIRTUALDESK, dx = (int)normalizedX, dy = (int)normalizedY, time = 0, mouseData = 0, dwExtraInfo = GetMessageExtraInfo() } }; - var input = new INPUT() { type = InputType.MOUSE, U = union }; - SendInput(1, new INPUT[] { input }, INPUT.Size); - }); - } - public void SendText(string transferText) { TryOnInputDesktop(() => diff --git a/Server/wwwroot/src/RemoteControl/InputEventHandlers.ts b/Server/wwwroot/src/RemoteControl/InputEventHandlers.ts index f5874ee3..504483a4 100644 --- a/Server/wwwroot/src/RemoteControl/InputEventHandlers.ts +++ b/Server/wwwroot/src/RemoteControl/InputEventHandlers.ts @@ -249,9 +249,6 @@ export function ApplyInputHandlers() { if (currentPointerDevice == "touch") { return; } - if (e.button != 0 && e.button != 2) { - return; - } e.preventDefault(); var percentX = e.offsetX / viewer.clientWidth; var percentY = e.offsetY / viewer.clientHeight;