-1

I was occupied on a project and I found a code that was perfectly what I was searching for, but there are parts that I don't understand could some of you explain me it please? This is the code that I found:

from pynput import keyboard
from pynput.keyboard import Controller

keypress = Controller()

COMBINATIONS = [
    {keyboard.KeyCode(char='a'), keyboard.KeyCode(char='z')},
]

current = set()

def execute():
    for loop in range(0, 10):
        keypress.press('w')
        keypress.release('w')

def on_press(key):
    if any([key in COMBO for COMBO in COMBINATIONS]): # THIS PART
        current.add(key)
        print(current)
        if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS): #THIS PART
           execute()

def on_release(key):
    if any([key in COMBO for COMBO in COMBINATIONS]): #THIS PART
        current.remove(key)

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

I commented the parts that I don't understand with #THIS PART commend Thanks for helping:)

Anwarvic
  • 12,156
  • 4
  • 49
  • 69
i799
  • 1
  • 2

2 Answers2

0

The answer to this question can be divided into two parts:

1. List Comprehension:

First, list comprehension: which is a way to write a for loop in just one line. So, instead of writing:

result = []
for COMBO in COMBINATIONS:
    result.append(key in COMBO)

You can write it in one line like so:

result = [key in COMBO for COMBO in COMBINATIONS]

As you can see, result is a list of boolean values... either True or False. Which brings the second part;

2. any() function

The second part is the function any(). This function returns True if one of the given items in True. And False if non of the given items is True.


So, the answer to your question is that this part:

any([key in COMBO for COMBO in COMBINATIONS])

It checks if the key exists in any of COMBO of the given COMBINATION.

Hope this answers your question!

Anwarvic
  • 12,156
  • 4
  • 49
  • 69
  • I knew how the for loop works also the append function but what happens with the results.append**(key in COMBO)** I don't understand this part? What does COMBO precisely? – i799 Apr 29 '20 at 17:12
  • `COMBO` is just an iterator over every item in `COMBINATION`. It works the same as `loop` in `for loop in range(0, 10)`. Running `for COMBO in COMBINATION: print(COMBO)` will show you what `COMBO` looks like – Anwarvic Apr 29 '20 at 17:27
  • Thank you so much for your answers! – i799 Apr 29 '20 at 22:21
  • To what would you change the title of this explanation so that others could find it easily. Thank you again! – i799 Apr 29 '20 at 22:24
0

I understood better when I tried to apply it in a easy code. Thanks for your great explanation:)

import os
Passwords = ["abcd", "efgh"]
inpt = input()


for List in Passwords:
    if (inpt in List) == True:
        print("Correct password! : True!")
        os.startfile('Chrome.exe')


if any([inpt in List for List in Passwords]):
    print("Correct password! : True!")
    os.startfile('Chrome.exe')
i799
  • 1
  • 2