Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle control chars in menus #282

Closed
bit-willi opened this issue Oct 23, 2024 · 1 comment · Fixed by #283
Closed

Handle control chars in menus #282

bit-willi opened this issue Oct 23, 2024 · 1 comment · Fixed by #283

Comments

@bit-willi
Copy link
Contributor

Hello everyone,

I've been trying to handle the 'ESC' key in a menu I'm working on, but I've been having trouble. I don't know if I'm missing something in the documentation, but while exploring the code, I found a way to achieve this with some changes.

Here's the modified code snippet from php-school/cli-menu/src/CliMenu.php:300-321:

switch ($char->getControl()) {
    case InputCharacter::UP:
    case InputCharacter::DOWN:
        $this->moveSelectionVertically($char->getControl());
        $this->draw();
        break;
    case InputCharacter::LEFT:
    case InputCharacter::RIGHT:
        $this->moveSelectionHorizontally($char->getControl());
        $this->draw();
        break;
    case InputCharacter::ENTER:
        $this->executeCurrentItem();
        break;
    default:
        $controlChar = $char->getControl();

        if (isset($this->customControlMappings[$controlChar])) {
            $this->customControlMappings[$controlChar]($this);
        }
        break;
}

With the 'default' case and by using addCustomControlMapping('ESC', $callable), I was able to handle the 'ESC' key effectively.

I believe this change could be useful. Should I open a PR for this, or do you think it might break something else?

Thanks!

@AydinHassan
Copy link
Member

AydinHassan commented Oct 23, 2024

hey @bit-willi makes sense to me :) - go ahead and send a PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants