Skip to content

Commit

Permalink
Allowing to choose which mouse button to use when dragging closes #58
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementGre committed Jul 22, 2024
1 parent 40de4e2 commit a2cce51
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
8 changes: 8 additions & 0 deletions ThreeFingerDragOnWindows/settings/SettingsData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ public class SettingsData{
// Three finger drag Settings
public bool ThreeFingerDrag { get; set; } = true;

public enum ThreeFingerDragButtonType {
NONE,
LEFT,
RIGHT,
MIDDLE,
}
public ThreeFingerDragButtonType ThreeFingerDragButton { get; set; } = ThreeFingerDragButtonType.LEFT;

public bool ThreeFingerDragAllowReleaseAndRestart { get; set; } = true;
public int ThreeFingerDragReleaseDelay { get; set; } = 500;

Expand Down
12 changes: 12 additions & 0 deletions ThreeFingerDragOnWindows/settings/ThreeFingerDragSettings.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@

</wuc:SettingsExpander>

<wuc:SettingsCard
Description="Keep the left button to be able to select text and drag objects."
HeaderIcon="{wuc:FontIcon FontFamily={StaticResource SymbolThemeFontFamily}, Glyph=&#xE962;}"
Header="Mouse button to be hold while dragging">
<ComboBox x:Name="ButtonType" SelectedIndex="{x:Bind ButtonTypeProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="240">
<x:String>None</x:String>
<x:String>Left</x:String>
<x:String>Right</x:String>
<x:String>Middle</x:String>
</ComboBox>
</wuc:SettingsCard>

<wuc:SettingsExpander
IsEnabled="{Binding ElementName=Enabled, Path=IsOn}"
Description="After releasing the three fingers, using only one or two fingers will instantly release the click. However, if you hang on, the click will be released only after a short delay. This feature allows you to reposition your fingers on the touchpad without releasing the click."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public bool EnabledProperty {
set{ App.SettingsData.ThreeFingerDrag = value; }
}

public int ButtonTypeProperty {
get{ return (int) App.SettingsData.ThreeFingerDragButton; }
set{ App.SettingsData.ThreeFingerDragButton = (SettingsData.ThreeFingerDragButtonType) value; }
}

public bool AllowReleaseAndRestartProperty {
get{ return App.SettingsData.ThreeFingerDragAllowReleaseAndRestart; }
set{ App.SettingsData.ThreeFingerDragAllowReleaseAndRestart = value; }
Expand Down
4 changes: 2 additions & 2 deletions ThreeFingerDragOnWindows/threefingerdrag/ThreeFingerDrag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void OnTouchpadContact(TouchpadContact[] oldContacts, TouchpadContact[] c
// Start dragging
_isDragging = true;
Logger.Log(" START DRAG, Left click down");
MouseOperations.MouseClick(MouseOperations.MOUSEEVENTF_LEFTDOWN);
MouseOperations.ThreeFingersDragMouseDown();
} else if(_isDragging &&
(shortDelayMovingFingersCount < 2 || (originalFingersCount != 3 && originalFingersCount >= 2))){
// Stop dragging
Expand Down Expand Up @@ -90,7 +90,7 @@ private void OnTimerElapsed(object source, ElapsedEventArgs e){

private void StopDrag(){
_isDragging = false;
MouseOperations.MouseClick(MouseOperations.MOUSEEVENTF_LEFTUP);
MouseOperations.ThreeFingersDragMouseUp();
}

private int GetReleaseDelay(){
Expand Down
33 changes: 32 additions & 1 deletion ThreeFingerDragOnWindows/utils/MouseOperations.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using ThreeFingerDragOnWindows.settings;

namespace ThreeFingerDragOnWindows.utils;

Expand All @@ -10,6 +11,8 @@ public class MouseOperations {
public const int MOUSEEVENTF_LEFTUP = 0x0004;
public const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
public const int MOUSEEVENTF_RIGHTUP = 0x0010;
public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
public const int MOUSEEVENTF_MIDDLEUP = 0x0040;

// moving the cursor does not work with floating point values
// decimal parts are kept and then added to be taken in account
Expand Down Expand Up @@ -72,6 +75,34 @@ public static void MouseClick(int mouseEventFlag){
if(result == 0) Console.WriteLine("Failed to send mouse click. Error code: " + Marshal.GetLastWin32Error());
}

public static void ThreeFingersDragMouseDown(){
switch (App.SettingsData.ThreeFingerDragButton){
case SettingsData.ThreeFingerDragButtonType.LEFT:
MouseClick(MOUSEEVENTF_LEFTDOWN);
break;
case SettingsData.ThreeFingerDragButtonType.RIGHT:
MouseClick(MOUSEEVENTF_RIGHTDOWN);
break;
case SettingsData.ThreeFingerDragButtonType.MIDDLE:
MouseClick(MOUSEEVENTF_MIDDLEDOWN);
break;
}
}

public static void ThreeFingersDragMouseUp(){
switch (App.SettingsData.ThreeFingerDragButton){
case SettingsData.ThreeFingerDragButtonType.LEFT:
MouseClick(MOUSEEVENTF_LEFTUP);
break;
case SettingsData.ThreeFingerDragButtonType.RIGHT:
MouseClick(MOUSEEVENTF_RIGHTUP);
break;
case SettingsData.ThreeFingerDragButtonType.MIDDLE:
MouseClick(MOUSEEVENTF_MIDDLEUP);
break;
}
}


[StructLayout(LayoutKind.Sequential)]
private struct Input {
Expand All @@ -98,4 +129,4 @@ public IntMousePoint(int x, int y){
this.x = x;
this.y = y;
}
}
}

0 comments on commit a2cce51

Please sign in to comment.