如何在python中单击热键并打开特定的url?

2024-04-19 01:50:32 发布

您现在位置:Python中文网/ 问答频道 /正文

单击按钮序列,如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()

Tags: keyinforreleaseifshiftoncurrent
1条回答
网友
1楼 · 发布于 2024-04-19 01:50:32

首先,在组合后清除current比在释放键后删除项更好更简单。第二个all(k in current for k in COMBO)只检查COMBO中的所有元素是否都存在于current,而不是方向。例如,如果按a+Shift它将变成True,这在这里是错误的。 解决方法是使用list而不是set,并检查COMBO是否存在于current中(作为片)。我使用this example检查切片列表是否存在。你知道吗

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 = []


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 contains_sublist(lst, sublst):
    n = len(sublst)
    return any((sublst == lst[i:i+n]) for i in range(len(lst)-n+1))


def on_press(key):
    global current
    if any([key in COMBO for COMBO in COMBINATIONS]):
        current.append(key)
        print(key)
        if any((contains_sublist(current, COMBO)) for COMBO in COMBINATIONS):
            print("Combination pressed!")
            if current[-1] == keyboard.KeyCode(char='a'):
                execute('https://www.amazon.com/')
            if current[-1] == keyboard.KeyCode(char='e'):
                execute('https://www.ebay.com/')
            current = []


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

相关问题 更多 >