touch-event.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { ipcMain } from 'electron';
  2. import { exec } from 'child_process';
  3. import { promisify } from 'util';
  4. import { getCachedAdbPath } from '../config.js';
  5. const execAsync = promisify(exec);
  6. // 发送 tap 事件到设备
  7. export async function sendTap(ipPort, x, y) {
  8. if (!ipPort) {
  9. return { success: false, error: '缺少设备 ID' };
  10. }
  11. if (typeof x !== 'number' || typeof y !== 'number') {
  12. return { success: false, error: '坐标必须是数字' };
  13. }
  14. try {
  15. const adbPath = getCachedAdbPath();
  16. const command = `${adbPath} -s ${ipPort} shell input tap ${x} ${y}`;
  17. await execAsync(command, {
  18. timeout: 5000,
  19. maxBuffer: 1024 * 1024
  20. });
  21. return { success: true };
  22. } catch (error) {
  23. return { success: false, error: error.message };
  24. }
  25. }
  26. // 发送 swipe 事件到设备
  27. export async function sendSwipe(ipPort, x1, y1, x2, y2, duration = 300) {
  28. if (!ipPort) {
  29. return { success: false, error: '缺少设备 ID' };
  30. }
  31. if (typeof x1 !== 'number' || typeof y1 !== 'number' || typeof x2 !== 'number' || typeof y2 !== 'number') {
  32. return { success: false, error: '坐标必须是数字' };
  33. }
  34. try {
  35. const adbPath = getCachedAdbPath();
  36. const command = `${adbPath} -s ${ipPort} shell input swipe ${x1} ${y1} ${x2} ${y2} ${duration}`;
  37. await execAsync(command, {
  38. timeout: 5000,
  39. maxBuffer: 1024 * 1024
  40. });
  41. return { success: true };
  42. } catch (error) {
  43. return { success: false, error: error.message };
  44. }
  45. }
  46. // 注册 IPC 处理器
  47. export function registerIpcHandlers() {
  48. // IPC 处理程序:发送 tap 事件到设备
  49. ipcMain.handle('send-tap', async (event, ipPort, x, y) => {
  50. return await sendTap(ipPort, x, y);
  51. });
  52. // IPC 处理程序:发送 swipe 事件到设备
  53. ipcMain.handle('send-swipe', async (event, ipPort, x1, y1, x2, y2, duration = 300) => {
  54. return await sendSwipe(ipPort, x1, y1, x2, y2, duration);
  55. });
  56. }