M
M
Mikhail2018-01-20 12:37:39
Python
Mikhail, 2018-01-20 12:37:39

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

1 answer(s)
T
TomasHuk, 2018-01-22
@TheMaxai

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

Your code for example:
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 question

Ask a Question

731 491 924 answers to any question