Answer the question
In order to leave comments, you need to log in
How to catch pressing the ESC key in the console?
I have tried different modules. I stopped at from pynput import keyboard
But this module catches pressing ESC even if another window is activated...
Approximate view of the final program:
check=False
def key_check():
global check
if key == esc: check=True
timer = 30
key_check.start()
while 1:
if check and timer > 10: sys.exit(1)
timer-=1
key_check.stop()
Answer the question
In order to leave comments, you need to log in
You need to add a check on the window class:
import win32gui
def now():
class_activ_window=win32gui.GetClassName(win32gui.GetForegroundWindow())
if class_activ_window=='my_console': # изменить на реальный класс окна
activ_window='my_consoler'
else:
pass
return activ_window
import get_activ_window
if key == esc and get_activ_window.now() == 'my_console':
check=True
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question