prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.1k stars 717 forks source link

TextArea and key bindings #1862

Open vallsv opened 3 months ago

vallsv commented 3 months ago

Hi,

I have a text area inside an application. This application contains key bindings like "0", "1" as kind of short cut.

As a text area is a user input i would expect it to consume the input chars first when it is active. And when the input is not consumed, to be propagated at the parent component... until the application key bindings.

But what i have noticed is that the key binding are triggered first.

I think i haven't found documentation about event propagation. But I feel like it's a problem, because it's the only lib handling widget this way.

Do you consider it as a bug? In this case i could maybe find a way to change this behaviour?

On my side, i think i will try to filter some bindings, depending on the active widget.

vallsv commented 3 months ago

For now i will use locally this filter.

    @Condition
    def not_on_an_active_input_control() -> bool:
        """
        True only if the active control is not a text input.
        """
        current = get_app().layout.current_control
        try:
            # Implementation specific of the TextArea widget
            return "text-area" not in current.input_processors[2].style
        except Exception:
            return True

    bindings.add("0", filter=not_on_an_active_input_control)(my_action)