Skip to content

Commit

Permalink
Add middle-mouse click.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Nov 20, 2020
1 parent 5484ea4 commit 80e3d08
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 101 deletions.
12 changes: 12 additions & 0 deletions Desktop.Core/Enums/ButtonAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Remotely.Desktop.Core.Enums
{
public enum ButtonAction
{
Down,
Up
}
}
10 changes: 5 additions & 5 deletions Desktop.Core/Interfaces/IKeyboardMouseInput.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
23 changes: 5 additions & 18 deletions Desktop.Core/Services/DtoMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -197,14 +198,7 @@ private void KeyUp(byte[] message)
private void MouseDown(byte[] message, Services.Viewer viewer)
{
var dto = MessagePackSerializer.Deserialize<MouseDownDto>(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)
Expand All @@ -216,14 +210,7 @@ private void MouseMove(byte[] message, Services.Viewer viewer)
private void MouseUp(byte[] message, Services.Viewer viewer)
{
var dto = MessagePackSerializer.Deserialize<MouseUpDto>(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)
Expand Down Expand Up @@ -262,8 +249,8 @@ private void SetKeyStatesUp()
private void Tap(byte[] message, Services.Viewer viewer)
{
var dto = MessagePackSerializer.Deserialize<TapDto>(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)
Expand Down
51 changes: 21 additions & 30 deletions Desktop.Linux/Services/KeyboardMouseInputLinux.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -57,35 +58,6 @@ public void SendKeyUp(string key)

}

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)
{
Expand Down Expand Up @@ -158,6 +130,25 @@ public void SendRightMouseUp(double percentX, double percentY, Viewer viewer)
}
}

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)
Expand Down
94 changes: 49 additions & 45 deletions Desktop.Win/Services/KeyboardMouseInputWin.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -77,29 +78,60 @@ public void SendKeyUp(string key)
});
}

public void SendLeftMouseDown(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.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)
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.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);
});
Expand Down Expand Up @@ -137,34 +169,6 @@ public void SendMouseWheel(int deltaY)
});
}

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(() =>
Expand Down
3 changes: 0 additions & 3 deletions Server/wwwroot/src/RemoteControl/InputEventHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 80e3d08

Please sign in to comment.