You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac下使用pynput开发自动点击器遇权限问题求助

问题

在Mac系统上使用pynput开发自动点击器程序时,始终弹出提示:

'This process is not trusted! Input event monitoring will not be possible until it is added to accessibility clients.'

使用的代码如下:

import time
import threading
from pynput.mouse import Button, Controller
from pynput.keyboard import Listener, KeyCode


delay = 0.001
button = Button.left
start_stop_key = KeyCode(char='s')
exit_key = KeyCode(char='e')


class ClickMouse(threading.Thread):
    def __init__(self, delay, button):
        super(ClickMouse, self).__init__()
        self.delay = delay
        self.button = button
        self.running = False
        self.program_running = True

    def start_clicking(self):
        self.running = True

    def stop_clicking(self):
        self.running = False

    def exit(self):
        self.stop_clicking()
        self.program_running = False

    def run(self):
        while self.program_running:
            while self.running:
                mouse.click(self.button)
                time.sleep(self.delay)
            time.sleep(0.1)


mouse = Controller()
click_thread = ClickMouse(delay, button)
click_thread.start()


def on_press(key):
    if key == start_stop_key:
        if click_thread.running:
            click_thread.stop_clicking()
        else:
            click_thread.start_clicking()
    elif key == exit_key:
        click_thread.exit()
        listener.stop()


with Listener(on_press=on_press) as listener:
    listener.join()

已为PyCharm和Terminal授予输入监控权限,但问题依旧,请问还需要为哪些文件或应用授予权限?

解决建议
  • 检查Python解释器本身的权限:找到当前使用的Python安装路径(比如/usr/bin/python3或虚拟环境内的Python),将该可执行文件添加到系统设置 > 隐私与安全性 > 辅助功能列表中。
  • 若使用虚拟环境,需添加虚拟环境内的Python可执行文件:比如虚拟环境路径为~/venv/bin/python,要把这个路径下的Python程序加入权限列表。
  • 添加权限后重启相关进程:关闭PyCharm、Terminal以及正在运行的Python程序,重新打开后测试。
  • 检查终端工具:如果使用iTerm2而非系统自带Terminal,需要把iTerm2也加入辅助权限列表。
  • 重置辅助权限(极端情况):若以上方法无效,可通过终端执行sudo tccutil reset Accessibility重置所有辅助权限,之后重新添加所需应用/程序的权限(注意:此操作会清空所有已配置的辅助权限,需重新设置)。

内容的提问来源于stack exchange,提问作者DeLawyer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 03:27:17