python - 在 Python 中使用 pynput 检查特定键

标签 python key pynput

dpressed = 0

def on_press(key):

    if key == ('d'):
        global dpressed
        dpressed+=1
        logging.info("D: %s" % dpressed)

当我运行此代码并按 d 时,没有任何反应,我怀疑这是因为在检查时需要将该键称为其他名称。有人知道它应该是什么吗?

最佳答案

您需要将 key 格式化为字符格式,否则它将不等于特定字符。

尝试

if key.char == ('d'):

完整代码为:

dpressed = 0

def on_press(key):

    if key.char == ('d'):
        global dpressed
        dpressed+=1
        logging.info("D: %s" % dpressed)

关于python - 在 Python 中使用 pynput 检查特定键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53693820/

相关文章:

python - 时间序列预测: need help using series with different periods of days

python - 为什么当我导入 pynput 并使用它时它会删除我的 python 文件?

python - 如何抓取属于特定类的 <li> 元素

oracle - 外键是否总是引用另一个表中的唯一键?

python - 是否可以使用可以接受多种形式的键创建一个 python 字典?

c# - 用户可以使用哪些键离开 WPF 文本框?

python-3.x - 如何在Python中运行的另一个程序上模拟Python中的按键事件

用于选择特定窗口的 Python 参数

python - 值错误: Failed to commit transaction in 5 attempts

Python:对定义的间隔内的值进行计数