Mac OS下Python监听输入事件报错,添加Input Monitoring无效求助
问题:Python触控板监听脚本权限报错无法解决
我之前查过相关旧帖,但里面的解决方法都无效。我编写了一个Python测试脚本,通过触控板输入向调音台发送MIDI控制变更信息,但运行时始终出现报错:
This process is not trusted! Input event monitoring will not be possible until it is added to accessibility clients.
我认为Accessibility是旧系统遗留功能,当前应使用「隐私与安全性」中的「Input Monitoring」权限。我已添加Python实例、脚本本身、Terminal,甚至升级到Python 3.12并添加对应实例,但报错仍未解决,恳请各位提供建议。
相关代码如下:
import mido from pynput import mouse, keyboard from pynput.keyboard import Key, Listener as KeyboardListener from pynput.mouse import Listener as MouseListener # Constants MAX_X_RESOLUTION = 127 # MIDI control change max value # MIDI setup outport = mido.open_output('USB Midi Cable', virtual=True) def send_program_change(): """Send a MIDI program change message.""" program_change = mido.Message('program_change', program=1, channel=0) outport.send(program_change) def send_control_change(value): """Send a MIDI control change message based on the x position of the trackpad.""" control_change = mido.Message('control_change', control=1, value=value, channel=0) outport.send(control_change) def on_move(x, y): """Handle mouse movement.""" # Scale the x position to MIDI values (0-127) width = 1920 midi_value = int((x / width) * MAX_X_RESOLUTION) send_control_change(midi_value) def on_press(key): """Terminate the program if escape key is pressed.""" if key == Key.esc: return False # Returning False stops the listener if __name__ == "__main__": send_program_change() # Start listening to the keyboard and mouse with MouseListener(on_move=on_move) as listener: with KeyboardListener(on_press=on_press) as listener: listener.join()
内容的提问来源于stack exchange,提问作者user25045976
相关产品推荐
相关产品推荐

