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

The Python keyboard module is incompatible with macOS Ventura #8

Open
zhuiyue132 opened this issue Apr 18, 2023 · 15 comments
Open

The Python keyboard module is incompatible with macOS Ventura #8

zhuiyue132 opened this issue Apr 18, 2023 · 15 comments
Labels
bug Something isn't working

Comments

@zhuiyue132
Copy link

run anywhere也能正常调起Alfred,gpt关键词也没问题。

但就是没响应;

实测api-key和代理接口没问题。

image

@zhuiyue132
Copy link
Author

image

@RogerLiNing
Copy link

这个脚本使用的是stream模式,你要确保你的代理支持stream。

@phguo
Copy link
Owner

phguo commented Apr 19, 2023

可以尝试在 Terminal 中运行 <Alfred 环境变量中的 Python 路径> -c "import keyboard; keyboard.write('test')", 看 'test' 有没有被输出。

可能是因为 Python 的 keyboard 包太久没有更新,不兼容新版本的 macOS

@phguo phguo added the bug Something isn't working label Apr 19, 2023
@zhuiyue132
Copy link
Author

终端中运行确实不能正确打印 “test”。应该是这个原因了。我的系统是macOS Ventura 13.1;

@phguo
Copy link
Owner

phguo commented Apr 19, 2023

@zhuiyue132 谢谢!

能否帮忙尝试下

import subprocess

applescript = """
tell application "System Events" to keystroke "test"
"""

subprocess.call(['osascript', '-e', applescript])

能否在 Ventura 下运行。

会尽快修复。

@zhuiyue132
Copy link
Author

@zhuiyue132 谢谢!

能否帮忙尝试下

import subprocess

applescript = """
tell application "System Events" to keystroke "test"
"""

subprocess.call(['osascript', '-e', applescript])

能否在 Ventura 下运行。

会尽快修复。

这段代码可以在Ventura 下运行。运行效果如图:
image

@phguo
Copy link
Owner

phguo commented Apr 19, 2023

@zhuiyue132 好的,会尽快修复。

@phguo
Copy link
Owner

phguo commented Apr 23, 2023

There are two alternatives for the Python keyboard module: pynput and PyAutoGUI.

Is there anyone who can test which one is compatible with macOS Ventura?

@phguo phguo changed the title 按照步骤配置后,无响应 The Python keyboard module is incompatible with macOS Ventura Apr 23, 2023
@phguo
Copy link
Owner

phguo commented Apr 23, 2023

@zhuiyue132 谢谢!

能否帮忙尝试下

import subprocess

applescript = """
tell application "System Events" to keystroke "test"
"""

subprocess.call(['osascript', '-e', applescript])

能否在 Ventura 下运行。

会尽快修复。

这个方案不支持中文。

@zhuiyue132
Copy link
Author

There are two alternatives for the Python keyboard module: pynput and PyAutoGUI.

Is there anyone who can test which one is compatible with macOS Ventura?

两个包我都试了一下,存在一个相同的问题:输出结果包含同时包含中英文时,输入法不能及时切换,导致输出乱七八糟,见下方视频。输出纯英文或纯中文(需要把自带的打印关掉)无此问题。

Screen-2023-04-25-153508.1.mp4

@phguo
Copy link
Owner

phguo commented Apr 25, 2023

@zhuiyue132 谢谢!

在 macOS Monterey 使用 keyboard + 系统自带输入法 没有类似问题,输出示例如下:

>>> js 冒泡排序带中文注释
function bubbleSort(arr) {
  const len = arr.length; // 获取数组长度,用于控制循环次数
  for (let i = 0; i < len - 1; i++) { // 外层循环控制排序次数,每次循环可以确保一个元素位置正确
    for (let j = 0; j < len - 1 - i; j++) { // 内层循环用于比较相邻的两个元素的大小,并交换位置
      if (arr[j] > arr[j + 1]) { // 如果当前元素比下一位元素大,则交换位置
        const temp = arr[j]; // 用临时变量存储当前元素的值
        arr[j] = arr[j + 1]; // 将下一位元素的值赋给当前元素
        arr[j + 1] = temp; // 将临时变量中存储的当前元素的值赋给下一位元素
      }
    }
  }
  return arr; // 返回排序后的数组
} 

// 示例用法:
const arr = [3, 5, 1, 2, 4];
console.log(bubbleSort(arr)); // 输出 [1, 2, 3, 4, 5]
[Generated by ChatGPT, 2023-04-25 15:48:51]

除了 pynput 和 PyAutoGUI 外,还有一个可能的解决方案是,用 boppreh/keyboard#587 替换默认的 keyboard

从 GitHub Repository 安装 Python 包的命令如下:

pip uninstall keyboard
pip install git+https://github.com/0xGosu/keyboard.git

@zhuiyue132
Copy link
Author

@zhuiyue132 谢谢!

在 macOS Monterey 使用 keyboard + 系统自带输入法 没有类似问题,输出示例如下:

>>> js 冒泡排序带中文注释
function bubbleSort(arr) {
  const len = arr.length; // 获取数组长度,用于控制循环次数
  for (let i = 0; i < len - 1; i++) { // 外层循环控制排序次数,每次循环可以确保一个元素位置正确
    for (let j = 0; j < len - 1 - i; j++) { // 内层循环用于比较相邻的两个元素的大小,并交换位置
      if (arr[j] > arr[j + 1]) { // 如果当前元素比下一位元素大,则交换位置
        const temp = arr[j]; // 用临时变量存储当前元素的值
        arr[j] = arr[j + 1]; // 将下一位元素的值赋给当前元素
        arr[j + 1] = temp; // 将临时变量中存储的当前元素的值赋给下一位元素
      }
    }
  }
  return arr; // 返回排序后的数组
} 

// 示例用法:
const arr = [3, 5, 1, 2, 4];
console.log(bubbleSort(arr)); // 输出 [1, 2, 3, 4, 5]
[Generated by ChatGPT, 2023-04-25 15:48:51]

除了 pynput 和 PyAutoGUI 外,还有一个可能的解决方案是,用 boppreh/keyboard#587 替换默认的 keyboard

从 GitHub Repository 安装 Python 包的命令如下:

pip uninstall keyboard
pip install git+https://github.com/0xGosu/keyboard.git

image
修复版的keyboard安装失败。会不会因为我是Apple 芯片的问题?

@phguo
Copy link
Owner

phguo commented Apr 25, 2023

我也 Apple 芯片。URL 似乎不太对?我的输出前几行是这样的。

(base) name@mac ~> pip install git+https://github.com/0xGosu/keyboard.git

Collecting git+https://github.com/0xGosu/keyboard.git
  Cloning https://github.com/0xGosu/keyboard.git to /private/var/folders/0r/10nyc1zs4md_ddh4570xms4c0000gn/T/pip-req-build-mm6jqs9p
  Running command git clone -q https://github.com/0xGosu/keyboard.git /private/var/folders/0r/10nyc1zs4md_ddh4570xms4c0000gn/T/pip-req-build-mm6jqs9p

@YDKD
Copy link

YDKD commented Apr 26, 2023

麻烦问下,这个大概多久能支持Ventura系统呀

@longxiyang
Copy link

将python升级到Python 3.11.5后,可以在Ventura上正常使用。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants