listenTest.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import time
  2. from pynput import mouse
  3. from pynput.mouse import Controller, Button
  4. import asyncio
  5. import win32Test
  6. import pika
  7. import messageClass
  8. connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
  9. channel = connection.channel()
  10. channel.exchange_declare("sync", "fanout", durable=True)
  11. mouse_controller = Controller()
  12. middle_point, base_width, base_height = win32Test.arrange_windows()
  13. def in_monitorZone():
  14. now_position = mouse_controller.position
  15. if now_position < (base_width, base_height):
  16. return True
  17. else:
  18. return False
  19. def on_click(x, y, button, pressed):
  20. # function:0
  21. # x:
  22. # y:
  23. # button
  24. # pressed
  25. #
  26. # function:1
  27. # x,
  28. # y,
  29. # dx,
  30. # dy,
  31. message = messageClass.ClickMessage(x, y, button).getMessage()
  32. channel.basic_publish(exchange='sync', routing_key='', body=message)
  33. # if not pressed:
  34. # # Stop listener
  35. # return False
  36. async def scroll_to_next_point(middle_point, mouse_controller, dx, dy):
  37. for i in range(1, len(middle_point)):
  38. print('Scrolled to next point')
  39. mouse_controller.position = middle_point[i]
  40. await asyncio.sleep(1)
  41. mouse_controller.scroll(dx, dy)
  42. def on_scroll(x, y, dx, dy):
  43. message = messageClass.ScrollMessage(x, y, dx, dy).getMessage()
  44. channel.basic_publish(exchange='sync', routing_key='', body=message)
  45. # print('Scrolled {0} at {1}'.format(
  46. # 'down' if dy < 0 else 'up',
  47. # (x, y)))
  48. # if in_monitorZone():
  49. # origin = mouse_controller.position
  50. # for i in range(1, len(middle_point)):
  51. # print('scrolled')
  52. # mouse_controller.position = middle_point[i]
  53. # mouse_controller.click(Button.right, 1)
  54. # time.sleep(0.5)
  55. # mouse_controller.position = origin
  56. # Collect events until released
  57. with mouse.Listener(
  58. on_click=on_click,
  59. on_scroll=on_scroll) as listener:
  60. listener.join()
  61. # ...or, in a non-blocking fashion:
  62. listener = mouse.Listener(
  63. on_click=on_click,
  64. on_scroll=on_scroll)
  65. listener.start()