我正在尝试使用 pyautogui 与 Arduino 连接到运行 Sonoma (14.0) 的 Macbook Air M1 上的 Paj7620 手势传感器模块。问题是 pyautogui 似乎没有触发该操作,尽管正确的值已传递给 Python 代码。我已经在隐私和安全 -> 可访问性中允许终端。
例如,这是Python代码:
import serial #Serial imported for Serial communication
import time #Required to use delay functions
import pyautogui
ArduinoSerial = serial.Serial('/dev/cu.usbmodem11401',9600) #Create Serial port object called arduinoSerialData
time.sleep(2) #wait for 2 seconds for the communication to get established
while 1:
incoming = str(ArduinoSerial.readline()) #read the serial data and print it as line
print(incoming)
if 'Right' in incoming:
pyautogui.press('nexttrack')
if 'Left' in incoming:
pyautogui.press('prevtrack')
if 'Up' in incoming:
pyautogui.press('volumeup')
if 'Down' in incoming:
pyautogui.press('volumedown')
if 'Forward' in incoming:
pyautogui.press('pause')
if 'Backward' in incoming:
pyautogui.press('playpause')
incoming = "";
这是程序输出:
b'PAJ7620U2 TEST DEMO: Recognize 9 gestures.\r\n'
b'INIT SENSOR...\r\n'
b'Addr0 =20, Addr1 =76\r\n'
b'wake-up finish.\r\n'
b'Paj7620 initialize register finished.\r\n'
b'INIT OK\r\n'
b'Please input your gestures:\n'
b'\r\n'
b'Right\r\n'
b'Left\r\n'
b'Forward\r\n'
b'Backward\r\n'
b'Up\r\n'
b'Forward\r\n'
...
使用相同的代码,调用spotify(https://github.com/hnarayanan/shpotify)效果非常好,但如果我可以控制任何媒体播放器而不仅仅是spotify,那就太好了。
最近遇到了 pyautogui 的一些问题。尝试使用 root 用户运行脚本,如下所示:
sudo python your_script.py
Mac os 有不同的块,通过 python 进行系统交互。