listenTest.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import time
  2. from pynput import mouse
  3. from pynput.mouse import Controller, Button
  4. import asyncio
  5. import win32Test
  6. mouse_controller = Controller()
  7. middle_point, base_width, base_height = win32Test.arrange_windows()
  8. print(middle_point)
  9. print(base_width)
  10. print(base_height)
  11. def in_monitorZone():
  12. now_position = mouse_controller.position
  13. if now_position < (base_width, base_height):
  14. return True
  15. else:
  16. return False
  17. def on_click(x, y, button, pressed):
  18. # function:0
  19. # x:
  20. # y:
  21. # button
  22. # pressed
  23. #
  24. # function:1
  25. # x,
  26. # y,
  27. # dx,
  28. # dy,
  29. print('{0} at {1}'.format(
  30. 'Pressed' if pressed else 'Released',
  31. (x, y)))
  32. print(button)
  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. print('Scrolled {0} at {1}'.format(
  44. 'down' if dy < 0 else 'up',
  45. (x, y)))
  46. if in_monitorZone():
  47. origin = mouse_controller.position
  48. for i in range(1, len(middle_point)):
  49. print('scrolled')
  50. mouse_controller.position = middle_point[i]
  51. mouse_controller.click(Button.right, 1)
  52. time.sleep(0.5)
  53. mouse_controller.position = origin
  54. # Collect events until released
  55. with mouse.Listener(
  56. on_click=on_click,
  57. on_scroll=on_scroll) as listener:
  58. listener.join()
  59. # ...or, in a non-blocking fashion:
  60. listener = mouse.Listener(
  61. on_click=on_click,
  62. on_scroll=on_scroll)
  63. listener.start()