为什么用 pynput 无法检测到特定按键?

发布于 2025-01-15 13:26:56 字数 370 浏览 5 评论 0原文

我正在尝试制作一个在后台运行并检测按下某个键以创建一种热键的程序,但由于某种原因我无法检测到一个特定的键。

from pynput.keyboard import Key, Listener


def on_press(key):
    print(key)
    if str(key) == str('4'):
        print("test")

with Listener(on_press=on_press) as listener:
    listener.join()

预期产出:

'4'
test

实际产出:

'4'

I'm trying to make a program which runs in the background and detects a certain key being pressed to create a kind of hotkey, but for some reason I can't detect one specific key.

from pynput.keyboard import Key, Listener


def on_press(key):
    print(key)
    if str(key) == str('4'):
        print("test")

with Listener(on_press=on_press) as listener:
    listener.join()

Expected Output:

'4'
test

Actual Output:

'4'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习惯成性 2025-01-22 13:26:56

以下内容应该有所帮助:

from pynput.keyboard import Key, Listener
from pynput import keyboard


def on_press(key):
    print(key)
    #print(type(str(key)))
    if key.char == '4':
        print("test")

with Listener(on_press=on_press) as listener:
    listener.join()

输出如下:

'f'
'g'
'j'
'd'
'你'
'j'
'k'
'e'
'd'
'r'
't'
'j'
‘6’
‘6’
‘4’
测试
‘4’
测试
‘4’
测试
‘4’
测试
‘4’
测试

我使用了以下指南:

https://buildmedia.readthedocs。 org/media/pdf/pynput/latest/pynput.pdf

the following should help:

from pynput.keyboard import Key, Listener
from pynput import keyboard


def on_press(key):
    print(key)
    #print(type(str(key)))
    if key.char == '4':
        print("test")

with Listener(on_press=on_press) as listener:
    listener.join()

the output is following:

'f'
'g'
'j'
'd'
'y'
'j'
'k'
'e'
'd'
'r'
't'
'j'
'6'
'6'
'4'
test
'4'
test
'4'
test
'4'
test
'4'
test

I have used the following guide:

https://buildmedia.readthedocs.org/media/pdf/pynput/latest/pynput.pdf

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文