0

Click the sequence of buttons, like shift+a and open amazon.com. Click another sequence of buttons, like shift+e and open ebay.com. The code works for amazon.com but when I click hotkey shift+e it is not open ebay.com. When you print key which you are clicking you realize that program is pressing Shift, e and a. I don't know why program prints a which I do not click?

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()
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
John Friday
  • 115
  • 1
  • 10

1 Answers1

0

First, it would be better and simpler to just clear current after a combination instead of removing item after key release. Second all(k in current for k in COMBO) would only check if all elements in COMBO exist in current, not the orientation. For example, if you press a+Shift it would become True which is wrong in here. A solution is to use list instead of set and check if the COMBO exists (as a slice) in current. I used this example for checking the existence of a slice list.

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()
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67