Мне нужно прервать Quartz.CoreGraphics.CGPostMouseEvent
for loop
нажатием клавиатуры, но я, похоже, не могу обнаружить нажатие клавиши, когда нахожусь внутри цикла for с CGPostMouseEvent
. Я пробовал тот же код с time.sleep
(нижняя часть кода), который обнаруживает нажатую клавишу. Я не эксперт по Quartz, поэтому прошу прощения, если я сделал что-то не так, но я не могу использовать ни CGEventCreateMouseEvent, ни pyautogui, ни pynput, потому что они не работают в 3D-окне просмотра CGEventPost не всегда перемещает мышь macOS
import math, time
from Quartz.CoreGraphics import CGPostMouseEvent
from pynput import keyboard
def orbit(x, y, r=10, loops=10, speed=10):
"""
This method updates the mouse position in a 3D Viewport. It is necessary to use CGPostMouseEvent:
https://stackoverflow.com/questions/45313432/cgeventpost-does-not-always-move-the-mouse-macos
"""
r = 10
for i in range(1,360*loops,speed):
# if keyboard.is_pressed('space'):
# print('something')
# sys.exit()
# break
x = x + r * math.cos(math.radians(i))
y = y + r * math.sin(math.radians(i))
CGPostMouseEvent((x, y), True, 1, True)
time.sleep(0.02)
def on_key_press(key):
"""
Method to quit the App while reproducting a macro
"""
print(key)
if key == keyboard.Key.esc:
print('ESC KEY PRESSED')
if __name__ == "__main__":
"""
When the orbit method is exectued the mouse moves away form the the python terminal loosing focus,
therefore Ctrl-C cannot interrupt the program.
I would like to allow the user to interrupt the execution by pressing ESC at any time, so I have created a listnener which works in any portion of the screen,
but it stops working when I inside the "orbit".
"""
listener_thread = keyboard.Listener(on_press=on_key_press, deamon=True)
listener_thread.start()
# listener_thread.join()
# LOOP RUNNING MOUSE MACRO. This code doesn't work with the keyboard listener
orbit(1744*0.5,1344*0.5)
# This code works with tle listener
# for i in range(10):
# time.sleep(1)
# print(i)