touch-event.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. console.error('Tap 失败:', error.message);
  24. return { success: false, error: error.message };
  25. }
  26. }
  27. // 发送 swipe 事件到设备
  28. export async function sendSwipe(ipPort, x1, y1, x2, y2, duration = 300) {
  29. if (!ipPort) {
  30. return { success: false, error: '缺少设备 ID' };
  31. }
  32. if (typeof x1 !== 'number' || typeof y1 !== 'number' || typeof x2 !== 'number' || typeof y2 !== 'number') {
  33. return { success: false, error: '坐标必须是数字' };
  34. }
  35. try {
  36. const adbPath = getCachedAdbPath();
  37. const command = `${adbPath} -s ${ipPort} shell input swipe ${x1} ${y1} ${x2} ${y2} ${duration}`;
  38. await execAsync(command, {
  39. timeout: 5000,
  40. maxBuffer: 1024 * 1024
  41. });
  42. return { success: true };
  43. } catch (error) {
  44. console.error('Swipe 失败:', error.message);
  45. return { success: false, error: error.message };
  46. }
  47. }
  48. // 注册 IPC 处理器
  49. export function registerIpcHandlers() {
  50. // IPC 处理程序:发送 tap 事件到设备
  51. ipcMain.handle('send-tap', async (event, ipPort, x, y) => {
  52. return await sendTap(ipPort, x, y);
  53. });
  54. // IPC 处理程序:发送 swipe 事件到设备
  55. ipcMain.handle('send-swipe', async (event, ipPort, x1, y1, x2, y2, duration = 300) => {
  56. return await sendSwipe(ipPort, x1, y1, x2, y2, duration);
  57. });
  58. }