bchao1 / bullet

🚅 Interactive prompts made simple. Build a prompt like stacking blocks.
https://pypi.org/project/bullet/
MIT License
3.55k stars 113 forks source link

How to quit a SlidePrompt ? #83

Open pommepommee opened 3 years ago

pommepommee commented 3 years ago

Hi, I'm currently trying to understand how to quit a SlidePrompt during the inputs. I saw you did a keyhandler.register but I can't implement it.

Does it have to be in the SlidePrompt class ? Can I put an keyhandler on my functions ?

For example, I have a login "form" and I want to cancel the process

def logIn():
    form = SlidePrompt([
        Input("Enter your username: ",
            word_color=_YELLOW
        ),
        Input("Enter your password: ",
            word_color=_YELLOW
        ),
    ])
    result = form.launch()

I tested to add @keyhandler.register(charDef.INTERRUPT_KEY) above the function but it didn't work, so maybe a clearer explanation on how to achieve this could be great !

Thanks 😄

bchao1 commented 2 years ago

Hi @pommepommee , you could refer to DOCUMENTATION.md and ./examples/check.py to see how keyhandler works in the general case.

However, after playing with your example for a while, I found that the keyhandler could not solve your case.
The main reason is that Input calls myInput function, and myInput calls utils.getchar. The getchar function essentially captures all keyboard events.

# in client.myInput
def input(self):
        while True:
            c = utils.getchar()
            i = c if c == UNDEFINED_KEY else ord(c)

            if i == NEWLINE_KEY:
                utils.forceWrite('\n')
                return self.getInput()

I believe the only thing you could do (for now) is modify the input function of myInput so that it handles charDef.INTERRUPT_KEY, probably like the following:

# in client.myInput
def input(self):
        while True:
            c = utils.getchar()
            i = c if c == UNDEFINED_KEY else ord(c)

            if i == INTERRUPT_KEY:
                #do something
            if i == NEWLINE_KEY:
                utils.forceWrite('\n')
                return self.getInput()
bchao1 commented 2 years ago

I plan to completely rewrite the myInput function to solve your issue for good. Please stay tuned for the latest release.