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

capslock crshed on macOS #596

Closed
Iris-Neko opened this issue Apr 4, 2024 · 2 comments
Closed

capslock crshed on macOS #596

Iris-Neko opened this issue Apr 4, 2024 · 2 comments

Comments

@Iris-Neko
Copy link

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
Copy link
Author

I find it's tkinter's bug.

@fizzi01
Copy link

fizzi01 commented May 26, 2024

I find it's tkinter's bug.

Did you find a fix somewhere?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants