-1
How can I access the function’s Return on_press(key)
?
The program below captures keystrokes. Open, for example, the notepad and type test and teclhe Enter
The result in the list should be: lista = ['t','e','s','t','e','<key.esc>']
Can you give me a hint? Thanks.
from pynput import keyboard
def on_press(key):
lista = []
try:
texto = '{0}'.format(key.char)
lista.append(texto)
except AttributeError:
texto = '<{0}>'.format(key)
lista.append(texto)
return lista
def on_release(key):
if key == keyboard.Key.esc:
# Stop listener
return False
# Collect events until released
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
Why don’t you leave the variable
lista
in the overall scope?– gato
I was making a mistake. I had tried. Well, the answer from the colleague below worked and even accepted (it worked) in putting the variable in the global scope.
– Wilson Junior