From b1071e0cd8ac03ba8d490c83ac2646ee8b628b55 Mon Sep 17 00:00:00 2001 From: toucheres <761844639@qq.com> Date: Wed, 12 Nov 2025 16:53:47 +0800 Subject: [PATCH 1/3] updata logic --- MapTP.App/MainWindow.xaml.cs | 60 ++++++++++++++++++---- MapTP.App/MouseProcessor.cs | 99 ++++++++++++++++++++++++++++-------- 2 files changed, 127 insertions(+), 32 deletions(-) diff --git a/MapTP.App/MainWindow.xaml.cs b/MapTP.App/MainWindow.xaml.cs index c677f2f..58130f6 100644 --- a/MapTP.App/MainWindow.xaml.cs +++ b/MapTP.App/MainWindow.xaml.cs @@ -565,6 +565,12 @@ private void OnWindowCloses(object sender, System.ComponentModel.CancelEventArgs } private bool lastTip; + // Tap/Drag detection state (improves separating discrete taps from drags) + private DateTime fingerDownTime; + private int fingerStartAbsX, fingerStartAbsY; // starting absolute (0..65535) mapped coordinates + private bool mouseDownSent; // whether we already issued a real left down (drag scenario) + private const int TapDurationThresholdMs =180; // max duration to still count as a tap before auto-convert to drag + private const int TapMovementThresholdAbs =1200; // movement threshold in absolute coordinate units (~2% of65535 width) /// /// This method is for processing touchpad inputs @@ -588,7 +594,7 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b { if (x.Identifier == 0) // limiting ContactId(Identifier) to 0 is to read the first finger { - var curTip = x.IsButtonDown.Value; + var curTip = x.IsButtonDown.Value; // finger touching surface if (started) { try @@ -596,28 +602,60 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b int X, Y; X = (tpsx <= x.X ? (tpex >= x.X ? - (int)Math.Floor((((decimal)(x.X - tpsx) / tpgx * scgx) + scsx) / ScreenSizeX * 65535) - : (int)Math.Floor((decimal)scex / ScreenSizeX * 65535)) - : (int)Math.Floor((decimal)scsx / ScreenSizeX * 65535)); + (int)Math.Floor((((decimal)(x.X - tpsx) / tpgx * scgx) + scsx) / ScreenSizeX *65535) + : (int)Math.Floor((decimal)scex / ScreenSizeX *65535)) + : (int)Math.Floor((decimal)scsx / ScreenSizeX *65535)); Y = (tpsy <= x.Y ? (tpey >= x.Y ? - (int)Math.Floor((((decimal)(x.Y - tpsy) / tpgy * scgy) + scsy) / ScreenSizeY * 65535) - : (int)Math.Floor((decimal)scey / ScreenSizeY * 65535)) - : (int)Math.Floor((decimal)scsy / ScreenSizeY * 65535)); + (int)Math.Floor((((decimal)(x.Y - tpsy) / tpgy * scgy) + scsy) / ScreenSizeY *65535) + : (int)Math.Floor((decimal)scey / ScreenSizeY *65535)) + : (int)Math.Floor((decimal)scsy / ScreenSizeY *65535)); + + // Always move pointer (for visual feedback) mouseProcessor.MoveCursor(X, Y); + + // Enhanced tap vs drag logic only when turtle mode is enabled (click simulation enabled) if (turtle) { - if (lastTip != curTip) + if (!lastTip && curTip) { - if (curTip) + // Finger just touched: start possible tap + fingerDownTime = DateTime.UtcNow; + fingerStartAbsX = X; + fingerStartAbsY = Y; + mouseDownSent = false; // not yet a drag + } + else if (lastTip && curTip) + { + // Finger is still down; decide if we should convert to drag + if (!mouseDownSent) { + double elapsed = (DateTime.UtcNow - fingerDownTime).TotalMilliseconds; + int deltaAbs = Math.Max(Math.Abs(X - fingerStartAbsX), Math.Abs(Y - fingerStartAbsY)); + if (elapsed > TapDurationThresholdMs || deltaAbs > TapMovementThresholdAbs) + { + // Became a drag: send down now + mouseProcessor.MouseDown(); + mouseDownSent = true; + } + } + } + else if (lastTip && !curTip) + { + // Finger lifted + if (!mouseDownSent) + { + // Treat as discrete tap: emit down+up quickly mouseProcessor.MouseDown(); + mouseProcessor.MouseUp(); } else { + // End drag mouseProcessor.MouseUp(); } - } + mouseDownSent = false; + } } } catch (Exception e) @@ -625,7 +663,7 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b HandyControl.Controls.MessageBox.Show(e.ToString()); } } - lastTip = curTip; + lastTip = curTip; // update last state after processing } } /* diff --git a/MapTP.App/MouseProcessor.cs b/MapTP.App/MouseProcessor.cs index 611aa06..ca0ec82 100644 --- a/MapTP.App/MouseProcessor.cs +++ b/MapTP.App/MouseProcessor.cs @@ -63,29 +63,86 @@ struct MOUSEINPUT [DllImport("user32.dll")] static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize); - //[DllImport("user32.dll")] - //private static extern int SetCursorPos(int x, int y); + // Useful constants for readability + const uint INPUT_MOUSE =0; + const uint MOUSEEVENTF_MOVE =0x0001; + const uint MOUSEEVENTF_LEFTDOWN =0x0002; + const uint MOUSEEVENTF_LEFTUP =0x0004; + const uint MOUSEEVENTF_MOVE_NOCOALESCE =0x2000; // May be ignored on older Windows + const uint MOUSEEVENTF_VIRTUALDESK =0x4000; + const uint MOUSEEVENTF_ABSOLUTE =0x8000; #endregion + + // Simple smoothing to improve perceived pointer stability + private int _lastRawX = -1; + private int _lastRawY = -1; + private int _lastOutX = -1; + private int _lastOutY = -1; + + // Public knobs (can be wired to settings later if needed) + public bool EnableSmoothing { get; set; } = true; + //0..1, closer to0 = smoother/slower, closer to1 = snappier + public double SmoothFactor { get; set; } =0.35; + // Deadzone in normalized absolute units (0..65535). Small deltas are ignored. + public int Deadzone { get; set; } =1; + public void MoveCursor(int x, int y) { - //SetCursorPos(x, y); + // Remember raw (requested) position + int rawX = x; + int rawY = y; + + // Initialize last values on first call + if (_lastOutX <0 || _lastOutY <0) + { + _lastRawX = rawX; + _lastRawY = rawY; + _lastOutX = rawX; + _lastOutY = rawY; + } + + // Early out if movement is within deadzone (reduces jitter) + if (Math.Abs(rawX - _lastRawX) <= Deadzone && Math.Abs(rawY - _lastRawY) <= Deadzone) + { + _lastRawX = rawX; + _lastRawY = rawY; + return; + } + + int outX = rawX; + int outY = rawY; + + if (EnableSmoothing) + { + // Exponential moving average + double a = Math.Min(1.0, Math.Max(0.0, SmoothFactor)); + outX = (int)Math.Round(_lastOutX + (rawX - _lastOutX) * a); + outY = (int)Math.Round(_lastOutY + (rawY - _lastOutY) * a); + } + INPUT[] _input = new INPUT[1]; _input[0] = new INPUT { - type = 0, // INPUT_MOUSE + type = INPUT_MOUSE, mkhi = new MOUSEKEYBDHARDWAREINPUT { mi = new MOUSEINPUT { - dx = x, - dy = y, - mouseData = 0, - dwFlags = 0x8000 | 0x0001 | 0x4000, // MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVEMENT | MOUSEEVENTF_VIRTUALDESK - time = 0 // Windows will provide this + dx = outX, + dy = outY, + mouseData =0, + // Absolute movement across the virtual desktop; avoid OS coalescing if possible + dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK | MOUSEEVENTF_MOVE_NOCOALESCE, + time =0 // Windows will provide this } } }; SendInput((uint)1, _input, Marshal.SizeOf(typeof(INPUT))); + + _lastRawX = rawX; + _lastRawY = rawY; + _lastOutX = outX; + _lastOutY = outY; return; } @@ -95,16 +152,16 @@ public void MouseDown() INPUT[] _input = new INPUT[1]; _input[0] = new INPUT { - type = 0, // INPUT_MOUSE + type = INPUT_MOUSE, // INPUT_MOUSE mkhi = new MOUSEKEYBDHARDWAREINPUT { mi = new MOUSEINPUT { - dx = 0, - dy = 0, - mouseData = 0, - dwFlags = 0x0002, // MOUSEEVENTF_LEFTDOWN - time = 0 // Windows will provide this + dx =0, + dy =0, + mouseData =0, + dwFlags = MOUSEEVENTF_LEFTDOWN, // MOUSEEVENTF_LEFTDOWN + time =0 // Windows will provide this } } }; @@ -117,16 +174,16 @@ public void MouseUp() INPUT[] _input = new INPUT[1]; _input[0] = new INPUT { - type = 0, // INPUT_MOUSE + type = INPUT_MOUSE, // INPUT_MOUSE mkhi = new MOUSEKEYBDHARDWAREINPUT { mi = new MOUSEINPUT { - dx = 0, - dy = 0, - mouseData = 0, - dwFlags = 0x0004, // MOUSEEVENTF_LEFTUP - time = 0 // Windows will provide this + dx =0, + dy =0, + mouseData =0, + dwFlags = MOUSEEVENTF_LEFTUP, // MOUSEEVENTF_LEFTUP + time =0 // Windows will provide this } } }; From 26047911fd798d4d767e1a83912977ff618a85bf Mon Sep 17 00:00:00 2001 From: toucheres <761844639@qq.com> Date: Wed, 12 Nov 2025 17:38:43 +0800 Subject: [PATCH 2/3] add Sensitivity settings --- .gitignore | 2 + MapTP.App/MainWindow.xaml | 17 +++++ MapTP.App/MainWindow.xaml.cs | 140 ++++++++++++++++++++++++----------- 3 files changed, 116 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index 426d76d..84c2f88 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.vscode + ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. ## diff --git a/MapTP.App/MainWindow.xaml b/MapTP.App/MainWindow.xaml index c4112aa..36e9cf7 100644 --- a/MapTP.App/MainWindow.xaml +++ b/MapTP.App/MainWindow.xaml @@ -101,6 +101,7 @@ + @@ -114,6 +115,22 @@ + + + + + + + + + + + + + + +