prompt-toolkit / prompt-toolkit/python-prompt-toolkit

Escape behaves strange the first time it's captured raw

Open
#1,655 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
10.6k
Forks
815
PR merge metrics
No merged PRs in 30d

Description

I followed the tutorial at the end of this page to capture output without printing:
https://python-prompt-toolkit.readthedocs.io/en/master/pages/asking_for_input.html
With some small modifications like making it a async context manager that don't matter for the problem and test.


#! /usr/bin/env python3

import asyncio

from prompt_toolkit.input import create_input
from prompt_toolkit.keys import Keys


async def main() -> None:
    done = asyncio.Event()
    input = create_input()
    def keys_ready():
        print('was called')
        for key_press in input.read_keys():
            print(key_press)
            if key_press.key == 'escape' or key_press.key == 'c-c': #esc or control-c
                print('asked for exit')
                done.set()
    
    with input.raw_mode():
        with input.attach(keys_ready):
            await done.wait()


if __name__ == "__main__":
    asyncio.run(main())

you can see the problem here, at least for me. When i press 'ESCAPE' the first time, the function keys_ready is called but doesn't print a keypress. The second time it works.

So the iterator is returning a empty iterator on the first press of escape (only as far as i can tell, other keys work).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the provided asyncio script and reproduce the first Escape press producing no keypress. Start by tracing create_input(), raw_mode(), attach(), and read_keys() in prompt_toolkit.input, then verify that the first Escape is delivered and the existing control-C behavior still works.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.