0

I trying to make a code to wait until i press [ or ], where [ make a specific thing and ] make other.

But i can't cuz i need to make both waits in the same time.

import keyboard
import threading


def listener():
    keyboard.wait(']')
    print('] pressed')


while True:
    threading.Thread(target=listener()).start()
    keyboard.wait('[')
    print('[ pressed')
    print('\nLoop\n')

This way the code wait ] and only when i press this key pass to next key [

I want to press any key and return print('key pressed') regardless of the order in which I press

  • 1
    Do you have to wait until the key is pressed? If not, could you just make the program check what key is pressed and then print your desired output if it is the right key instead of telling the program to wait until the specific key is pressed? – Nathan Roberts Nov 03 '21 at 13:10
  • I didn't think that, i'll make your way just checking what key is pressed, and if the key that i want make something. –  Nov 03 '21 at 14:20
  • If don't works i edit this post XD, but thanks <3 –  Nov 03 '21 at 14:21

1 Answers1

-1

I saw some listeners in web and i found a code that solved my problem

from pynput import keyboard


def on_press(key):
    if key == keyboard.Key.esc:
        return False
    try:
        k = key.char
    except:
        k = key.name
    if k == '[':
        print('Key pressed: ' + k)
        print('continuing...')
    if k == ']':
        print('Key pressed: ' + k)
        print('stoping')
        return False

Link: https://stackoverflow.com/a/43106497/15785950