AskGPT icon indicating copy to clipboard operation
AskGPT copied to clipboard

The Python `keyboard` module is incompatible with macOS Ventura

Open zhuiyue132 opened this issue 1 year ago • 15 comments

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

但就是没响应;

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

image

zhuiyue132 avatar Apr 18 '23 10:04 zhuiyue132

image

zhuiyue132 avatar Apr 18 '23 10:04 zhuiyue132

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

RogerLiNing avatar Apr 18 '23 15:04 RogerLiNing

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

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

phguo avatar Apr 19 '23 00:04 phguo

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

zhuiyue132 avatar Apr 19 '23 02:04 zhuiyue132

@zhuiyue132 谢谢!

能否帮忙尝试下

import subprocess

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

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

能否在 Ventura 下运行。

会尽快修复。

phguo avatar Apr 19 '23 02:04 phguo

@zhuiyue132 谢谢!

能否帮忙尝试下

import subprocess

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

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

能否在 Ventura 下运行。

会尽快修复。

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

zhuiyue132 avatar Apr 19 '23 05:04 zhuiyue132

@zhuiyue132 好的,会尽快修复。

phguo avatar Apr 19 '23 06:04 phguo

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 avatar Apr 23 '23 06:04 phguo

@zhuiyue132 谢谢!

能否帮忙尝试下

import subprocess

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

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

能否在 Ventura 下运行。

会尽快修复。

这个方案不支持中文。

phguo avatar Apr 23 '23 06:04 phguo

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?

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

https://user-images.githubusercontent.com/26954775/234206988-82063a56-9b9e-46ef-bb6c-c4eecfbeca4f.mp4

zhuiyue132 avatar Apr 25 '23 07:04 zhuiyue132

@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 外,还有一个可能的解决方案是,用 https://github.com/boppreh/keyboard/pull/587 替换默认的 keyboard

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

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

phguo avatar Apr 25 '23 08:04 phguo

@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 芯片的问题?

zhuiyue132 avatar Apr 25 '23 08:04 zhuiyue132

我也 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

phguo avatar Apr 25 '23 08:04 phguo

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

YDKD avatar Apr 26 '23 01:04 YDKD

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

longxiyang avatar Oct 11 '23 06:10 longxiyang