0

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

1 回答 1

0

current首先,在组合之后只清除而不是在释放键后删除项目会更好更简单。第二个all(k in current for k in COMBO)只会检查是否所有元素都COMBO存在于 中current,而不是方向。例如,如果您按a+Shift它将成为True这里的错误。一种解决方案是使用list代替并set检查. 我用这个例子来检查切片列表的存在。COMBOcurrent

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()
于 2019-04-28T05:31:58.407 回答