0

I want to create multiline HotKeys in python with one single for loop. I have a list looking like this:

import keyboard

HotKey = []

while True:
   list = []
   key = input()
   word = input()
   list .append(key,word)
   if key == -1:
       break
   else:
       HotKey.append(list)

for list in HotKey:
   keyboard.add_hotkey(list[0], lambda: print(list[1]))

my 2 issues are:

  • the list is not fixed and both of her lengh and keys can changed ny the user input.
  • when Im using a for loop and using for loop and the function: keyboard.addhotkey(TheKey, lambda: print(customWord) whenever I will press any of those keys I added as hotkeys, this will print the word from the last hotkey in my list

Im looking for a way to fix it with/without for loop

yoav s
  • 23
  • 4
  • 1
    Show the code you have already properly formatted in the question. – Michael Butscher Sep 03 '21 at 20:40
  • (1) First issue is that the code would end with an error because "append" method of list only accepts one argument. By the way: Don't use names of builtin functions or types like "list" as variable names. (2) The "lambda" uses the value of "list" it has when "lambda" is executed, not when it is created. But there is a trick: Replace it by `lambda word=list[1]: print(word)`. – Michael Butscher Sep 04 '21 at 02:38

0 Answers0