【发布时间】:2020-04-18 19:43:47
【问题描述】:
我正在尝试使用 python 自动化一些东西。我使用 pynput 来监听组合键。我正在尝试听 ctrl+shift+alt 的组合。我对修饰键没有问题,但只有字母键。我查看了 python 文档页面并尝试了以下操作:
from pynput import keyboard
from subprocess import Popen, PIPE
from evdev import uinput, ecodes as e
import os
# The key combination to check
COMBINATION = {keyboard.Key.shift, keyboard.Key.ctrl, keyboard.Key.alt, keyboard.KeyCode.from_char('k')}
# The currently active modifiers
current = set()
def on_press(key):
if key in COMBINATION:
current.add(key)
if all(k in current for k in COMBINATION):
print("x")
if key == keyboard.Key.esc:
listener.stop()
def on_release(key):
try:
current.remove(key)
except KeyError:
pass
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
从终端运行 python 文件后,脚本无法检测到我的组合键。
【问题讨论】: