listenTest.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import asyncio
  2. import pika
  3. from pynput import mouse
  4. from pynput.mouse import Controller
  5. import messageClass
  6. import screen
  7. connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
  8. channel = connection.channel()
  9. channel.exchange_declare("sync", "fanout", durable=True)
  10. mouse_controller = Controller()
  11. screen.arrange_master()
  12. def in_monitorZone():
  13. now_position = mouse_controller.position
  14. if now_position < (500, 600):
  15. return True
  16. else:
  17. return False
  18. def on_click(x, y, button, pressed):
  19. # function:0
  20. # x:
  21. # y:
  22. # button
  23. # pressed
  24. #
  25. # function:1
  26. # x,
  27. # y,
  28. # dx,
  29. # dy,
  30. if in_monitorZone():
  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. if in_monitorZone():
  44. message = messageClass.ScrollMessage(x, y, dx, dy).getMessage()
  45. channel.basic_publish(exchange='sync', routing_key='', body=message)
  46. # print('Scrolled {0} at {1}'.format(
  47. # 'down' if dy < 0 else 'up',
  48. # (x, y)))
  49. # if in_monitorZone():
  50. # origin = mouse_controller.position
  51. # for i in range(1, len(middle_point)):
  52. # print('scrolled')
  53. # mouse_controller.position = middle_point[i]
  54. # mouse_controller.click(Button.right, 1)
  55. # time.sleep(0.5)
  56. # mouse_controller.position = origin
  57. # Collect events until released
  58. with mouse.Listener(
  59. on_click=on_click,
  60. on_scroll=on_scroll) as listener:
  61. listener.join()
  62. # ...or, in a non-blocking fashion:
  63. listener = mouse.Listener(
  64. on_click=on_click,
  65. on_scroll=on_scroll)
  66. listener.start()