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

Implement From<KeyEvent> for String #792

Open
danielo515 opened this issue Jul 8, 2023 · 3 comments
Open

Implement From<KeyEvent> for String #792

danielo515 opened this issue Jul 8, 2023 · 3 comments

Comments

@danielo515
Copy link

Is your feature request related to a problem? Please describe.
I’m implementing a system to automatically build help panes from a contextual action structure. I have everything I need already except the ability to turn the key event into a string for displaying in a nice way.

Describe the solution you'd like
I would love to see the from keyevent trait for string,so it can be used for this purposes. It could be behind a feature flag so only those needing it get it.

Describe alternatives you've considered if any
I can build a wrapper type in my own codebase and implement it there.

Additional context
Anything else?
Nope

@Piturnah
Copy link
Contributor

Does the fmt::Debug implementation suit your needs? E.g.

use crossterm::event::{self, Event};

fn main() {
    loop {
        if let Ok(Event::Key(ev)) = event::read() {
            println!("{ev:?}");
            break;
        }
    }
}
KeyEvent { code: Char('a'), modifiers: NONE, kind: Press, state: NONE }

@danielo515
Copy link
Author

danielo515 commented Aug 7, 2023 via email

@joshka
Copy link
Collaborator

joshka commented Feb 15, 2024

I think this probably should be impl Display for KeyEvent and I want it for similar reasons (help text in TUI apps). That said, the fact that the event has kindand state fields makes any implementation a bit weird - they would either show too much information for help text, or have to throw away information and be ambiguous when displaying a key repeat event or disambiguating whether the event came from a key on the keypad / whether caps / num lock are on.

I think it's probably reasonable to implement Display for KeyCode and KeyModifiier, but I'd consider not using the event for help text and instead using custom struct that just worries about the code and modifier in your app.

joshka added a commit to joshka/crossterm that referenced this issue Feb 17, 2024
TimonPost pushed a commit that referenced this issue May 3, 2024
Partially addresses #792
* Implement Display for KeyCode and KeyModifiers
* Add demo for Display implementation
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

No branches or pull requests

3 participants