How to wait for any keypress (global), or mouse click (on certain application windows) - Python

140 Views Asked by At

I have a simple python script that goes something like this:

user_input = ''
application = ''

print('press any key or click on any application...')

WAIT_FOR_INPUTS() # this is the part I'm stuck on

if key:
     print(f'You pressed {key}.')
if click:
     print(f'You clicked on {application}')

I'm lost in pygame, pyautogui, win32gui, pynput and I don't know which module will solve my problem. I'm in python3 so I can't use raw_input(). If anybody knows how to help me I'd really appreciate it since this is one of the last hurdles to a project I'm excited about.

1

There are 1 best solutions below

0
Pascal Vallaster On

You could use the keyboard module:

import keyboard

def detect_pressed_key():
    print("Press any key to continue...")
    keyboard.read_key()  # Waits for a key press

detect_pressed_key()
print("Key pressed!")