单击按钮序列,例如shift+a并打开 amazon.com。单击另一个按钮序列,例如shift+e和 open ebay.com
。该代码适用,amazon.com
但是当我单击热键时shift+e,它没有打开 ebay.com。当您打印您正在单击的键时,您会意识到程序正在按下Shift,e并且a。我不知道为什么程序打印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()