Josiahiscool73

Simulate mouse movements in xcloud(no controller emulation)

May 10th, 2025 (edited)
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. // ts took me 2 weeks to figure out
  2.  
  3. function simulateMouseMovementOnXCloud() {
  4. // Define start and end coordinates in viewport pixels
  5. const startX = window.innerWidth / 4;
  6. const startY = window.innerHeight / 4;
  7. const endX = window.innerWidth / 2;
  8. const endY = window.innerHeight / 2;
  9.  
  10. console.log(`Simulating mouse movement from viewport coordinates (${Math.round(startX)}, ${Math.round(startY)}) to (${Math.round(endX)}, ${Math.round(endY)})`);
  11.  
  12. // 1. Simulate a pointermove to the starting position.
  13. // This helps establish an initial point, especially if absolute mouse mode is active.
  14. // The MouseManager's internal `lastAbsoluteX` and `lastAbsoluteY` might be updated.
  15. const initialMoveEvent = new PointerEvent('pointermove', {
  16. bubbles: true, // Should bubble up the DOM
  17. cancelable: true, // Can be canceled
  18. view: window, // The window context
  19. clientX: startX,
  20. clientY: startY,
  21. movementX: 0, // No movement relative to a "previous" point for this first event
  22. movementY: 0,
  23. pointerType: 'mouse', // Critical, as MouseManager checks e.pointerType === 'mouse'
  24. buttons: 0 // No mouse buttons pressed
  25. });
  26. window.dispatchEvent(initialMoveEvent);
  27. console.log("Dispatched initial 'pointermove' to start position.");
  28.  
  29. // 2. Simulate a pointermove to the ending position.
  30. setTimeout(() => {
  31. const moveEvent = new PointerEvent('pointermove', {
  32. bubbles: true,
  33. cancelable: true,
  34. view: window,
  35. clientX: endX,
  36. clientY: endY,
  37. movementX: endX - startX, // Relative movement from the startX
  38. movementY: endY - startY, // Relative movement from the startY
  39. pointerType: 'mouse',
  40. buttons: 0 // No mouse buttons pressed
  41. });
  42. window.dispatchEvent(moveEvent);
  43. console.log("[XCloud Sim] Dispatched 'pointermove' to end position.");
  44. console.log("[XCloud Sim] Mouse movement simulation attempt complete.");
  45. }, 100); // 100ms delay before the main move
  46. }
  47.  
  48. console.log("this is an example so it waits 5 seconds so you have enough time to go into Fullscreen and test this out, Waiting 5 seconds to simulate mouse movement...");
  49. setTimeout(simulateMouseMovementOnXCloud, 5000);
Advertisement
Add Comment
Please, Sign In to add comment