1

I am building a mobile (client) app that can control the mouse on a PC (server).

So I build a mobile app using expo that send the touches position to the server so I could control the mouse movement there.

The issue is the mouse movement is too laggy and jumpy. I tried ChatGPT, but still facing the issue.

Here is my code:

JS code

 const singlePan = Gesture.Pan() .onBegin((e) => { lastTranslationX = 0; lastTranslationY = 0; cursorVisible.value = 1; cursorX.value = e.absoluteX; cursorY.value = e.absoluteY; }) .onUpdate((e) => { // Calculate delta const dx = e.translationX - lastTranslationX; const dy = e.translationY - lastTranslationY; cursorX.value = e.x; cursorY.value = e.y; const now = Date.now(); if (now - lastSentTime > 16) { // ~60fps throttle if (onMove) runOnJS(onMove)(dx, dy); } lastTranslationX = e.translationX; lastTranslationY = e.translationY; }) .onEnd((e) => { // Stop velocity and cursor movement immediately (no inertia) velocityX.value = 0; velocityY.value = 0; cursorVisible.value = withSpring(0, { damping: 20 }); /* velocityX.value = e.velocityX; velocityY.value = e.velocityY; cursorX.value = withDecay({ velocity: velocityX.value, deceleration: 1 - (1 - friction) }); cursorY.value = withDecay({ velocity: velocityY.value, deceleration: 1 - (1 - friction) }); cursorVisible.value = withSpring(0, { damping: 20 });*/ }) .maxPointers(1); 

C# code

 private void MoveMouseBy(int dx, int dy) { INPUT[] inputs = new INPUT[1]; inputs[0].type = INPUT_MOUSE; inputs[0].mi.dx = dx; inputs[0].mi.dy = dy; inputs[0].mi.mouseData = 0; inputs[0].mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_MOVE_NOCOALESCE; inputs[0].mi.time = 0; inputs[0].mi.dwExtraInfo = IntPtr.Zero; uint result = SendInput(1, inputs, Marshal.SizeOf(typeof(INPUT))); if (result == 0) { Console.WriteLine("SendInput failed with error: " + Marshal.GetLastWin32Error()); } } public bool MoveMouseRelative(double dx, double dy, MousePadSize padSize) { double mobileWidth = padSize?.Width ?? 300; // fallback if missing double mobileHeight = padSize?.Height ?? 500; int screenWidth = Screen.PrimaryScreen.Bounds.Width; int screenHeight = Screen.PrimaryScreen.Bounds.Height; // Scale deltas: map mobile movement fractionally to PC pixels double scaleX = (double)screenWidth / mobileWidth; double scaleY = (double)screenHeight / mobileHeight; double scaledDx = dx * scaleX; double scaledDy = dy * scaleY; accumX += scaledDx * sensitivity; accumY += scaledDy * sensitivity; // <-- fix here int moveX = (int)Math.Round(accumX); int moveY = (int)Math.Round(accumY); if (moveX == 0 && moveY == 0) return true; accumX -= moveX; accumY -= moveY; MoveMouseBy(moveX, moveY); Console.WriteLine($"Moving mouse by: ({moveX},{moveY})"); return true; } 

I am using websocket to get the data, so it's pretty fast.

0

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.