moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.78k stars 247 forks source link

capslock crshed on macOS #596

Closed Iris-Neko closed 6 months ago

Iris-Neko commented 6 months ago

I write a program listening keyboard event on macos ,when i press caps lock the app crashed.

from pynput import keyboard
import time
import tkinter as tk
# 定义计数器
counter = 0
# 标记空格是否被按下
is_space_pressed = False
# 标记是否按下 Command+R
is_command_pressed = False
is_r_pressed = False
# 定义按键事件回调函数
def on_press(key):
    global counter, is_space_pressed, is_command_pressed, is_r_pressed

    if key == keyboard.Key.space:
        if not is_space_pressed:
            is_space_pressed = True
            counter += 1
            update_counter_label()
    elif key == keyboard.Key.cmd:
        is_command_pressed = True
    elif hasattr(key, 'char') and key.char == 'r':
        is_r_pressed = True
        if is_command_pressed:
            reset_counter()
def on_release(key):
    global is_space_pressed, is_command_pressed, is_r_pressed
    if key == keyboard.Key.space:
        is_space_pressed = False
    elif key == keyboard.Key.cmd:
        is_command_pressed = False
    elif hasattr(key, 'char') and key.char == 'r':
        is_r_pressed = False
    # 忽略 Caps Lock 键的释放事件
    if key == keyboard.Key.caps_lock:
        return
# 更新计数器标签
def update_counter_label():
    counter_label.config(text="Counter: " + str(counter))
# 重置计数器
def reset_counter():
    global counter
    counter = 0
    update_counter_label()
# 创建 GUI 窗口
window = tk.Tk()
window.title("Counter")
window.geometry("300x200")  # 设置窗口大小为 300x200
# 创建计数器标签
counter_label = tk.Label(window, text="Counter: 0", font=("Arial", 24))
counter_label.pack(pady=20)
# 创建退出按钮
import sys
def quit():
    sys.exit(0)
quit_button = tk.Button(window, text="Quit", command=quit)
quit_button.pack(pady=10)
# 监听键盘事件
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
    window.mainloop()
    listener.join()

Process finished with exit code 133 (interrupted by signal 5:SIGTRAP)

Iris-Neko commented 6 months ago

I find it's tkinter's bug.

fizzi01 commented 4 months ago

I find it's tkinter's bug.

Did you find a fix somewhere?

Fyime commented 1 month ago

same problem appeared on pyside6