D
D
Denis Titusov2014-02-12 12:55:02
Python
Denis Titusov, 2014-02-12 12:55:02

How to teach Python to recognize ESC sequences (in particular, pressing arrows) and not print their code, but do what is intended?

Good day!
I'm writing a small console utility that will run mostly on Linux machines.
The question arose: how to teach Python to recognize ESC sequences (in particular, pressing arrows) and not display their code, but do what is intended.
For example, pressing the left key prints the corresponding ESC sequence instead of moving the cursor:

Type any symbol to break input: ^[[D

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
T
tsarevfs, 2014-02-12
@tsarevfs

You can count like this .

D
dotcar, 2014-02-14
@dotcar

Everyone recommends via msvcrt.getch():

from msvcrt import getch()
while True:
  key = ord(getch())
  if key == 32:
    jump()
...
  if key == 27: #ESC
    break

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question