单击按钮序列,如shift+a并打开亚马逊网站. 单击另一系列按钮,如shift+e并打开ebay.com
。
代码适用于amazon.com
,但当我单击热键shift+e时,它没有打开易趣网.
当您打印正在单击的键时,您意识到程序正在按Shift键、e键和a键。
我不知道为什么程序会打印一个我没有点击的文件?你知道吗
import time
from pynput import keyboard
COMBINATIONS = [
{keyboard.Key.shift, keyboard.KeyCode(char='a')},
{keyboard.Key.shift, keyboard.KeyCode(char='A')},
{keyboard.Key.shift, keyboard.KeyCode(char='e')},
{keyboard.Key.shift, keyboard.KeyCode(char='E')}
]
current = set()
def execute(url):
keyboard_ctrl = keyboard.Controller()
keyboard_ctrl.press(keyboard.Key.ctrl_l)
keyboard_ctrl.press('l')
keyboard_ctrl.release(keyboard.Key.ctrl_l)
keyboard_ctrl.release('l')
time.sleep(0.2)
for i in url:
keyboard_ctrl.press(i)
keyboard_ctrl.release(i)
# keyboard_ctrl.type(url)
time.sleep(0.2)
keyboard_ctrl.press(keyboard.Key.enter)
keyboard_ctrl.release(keyboard.Key.enter)
def on_press(key):
if any([key in COMBO for COMBO in COMBINATIONS]):
current.add(key)
print(key)
if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS):
if keyboard.KeyCode(char='a') in current and keyboard.Key.shift in current:
execute('https://www.amazon.com/')
if keyboard.KeyCode(char='e') in current and keyboard.Key.shift in current:
execute('https://www.ebay.com/')
def on_release(key):
if any([key in COMBO for COMBO in COMBINATIONS]):
if key in current:
current.remove(key)
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
首先,在组合后清除
current
比在释放键后删除项更好更简单。第二个all(k in current for k in COMBO)
只检查COMBO
中的所有元素是否都存在于current
,而不是方向。例如,如果按a+Shift它将变成True
,这在这里是错误的。 解决方法是使用list
而不是set
,并检查COMBO
是否存在于current
中(作为片)。我使用this example检查切片列表是否存在。你知道吗相关问题 更多 >
编程相关推荐