如何解决用ESC键中断CGPostMouseEvent循环
我需要通过按下键盘来中断Quartz.CoreGraphics.CGPostMouseEvent
for loop
,但是当我进入CGPostMouseEvent
的for循环时,似乎无法检测到按键。我尝试使用time.sleep
(代码的底部)尝试相同的代码来检测按下的键。我不是Quartz的专家,所以如果我做错了这件事,我深表歉意,但是我不能使用CGEventCreateMouseEvent,pyautogui和pynput,因为它们不能在3D视口CGEventPost does not always move the mouse 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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。