Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

click theRunIsStuck #613

Open
chenyuxingkong opened this issue Oct 14, 2024 · 1 comment
Open

click theRunIsStuck #613

chenyuxingkong opened this issue Oct 14, 2024 · 1 comment

Comments

@chenyuxingkong
Copy link

chenyuxingkong commented Oct 14, 2024

from pynput import mouse
from pynput.mouse import Button

isRun = False
ctr = pynput.mouse.Controller()


def g36k():
    global isRun
    isRun = True
    print("start")
    # 运行卡死
    # theRunIsStuck
    ctr.click(Button.left)
    print("end")


def on_click(x, y, button, pressed):
    print(f'鼠标按下: {button} at ({x}, {y})')
    global isRun
    # 鼠标松开时触发
    if not pressed:
        if button == Button.x1 and not isRun:
            # 将 g36k 的调用调度到事件循环
            g36k()
            isRun = False


if __name__ == '__main__':
    # 创建鼠标监听器
    listener = mouse.Listener(on_click=on_click)
    listener.start()
    listener.join()

The above is my py code. When I run to ctr.click(Button.left), I can't output print("end "), and my mouse is stuck.

@moses-palmer
Copy link
Owner

Thank you for your report.

As you do not state what platform you are running this on I can only speculate. However, I think the issue is that these functions are not invoked sequentially; when a mouse button is clicked, on_click is eventually called and the check whether isRun is True is performed. Since it is not, g36k is called, a fake event is emitted and isRun is reset to False. Some time later, the fake event will cause on_click to be called again, but isRun has already been reset.

I suggest you check out the feature-injected branch. On that branch, you get an extra argument to on_click: injected. This argument signals that the current event was not generated by the user, but rather some other controller; this appears to be what you are after.

I would love some feedback on that branch; it has remained unmerged for six years already, but I have at least kept it up-to-date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants